-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
54 lines (48 loc) · 1.47 KB
/
server.cpp
File metadata and controls
54 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
//SERVER
int PORT = 8080;
int main(void)
{
//CREATING SOCKET
int server = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(PORT);
//BINDING SOCKET
if ((bind(server, (struct sockaddr*) &servAddr, sizeof(servAddr))) < 0){
printf("Uh oh, socket not binding\n");
return 1;
}
//LISTENING FOR CLIENTS
listen(server,9);
sockaddr_in clientSockAddr;
socklen_t clientSockAddrSize = sizeof(clientSockAddr);
//ACCEPTING CLIENTS
int client = accept(server, (sockaddr *)&clientSockAddr, &clientSockAddrSize);
printf("Client connected at port %i\n", PORT);
char clientMessage[3000];
while(true)
{
memset(clientMessage, 0, sizeof(clientMessage));
recv(client, clientMessage, sizeof(clientMessage),0);
printf("Client: %s\n", clientMessage);
printf("Server: ");
//SERVER RESPONSE
std::string input;
getline(std::cin, input);
memset(&clientMessage, 0, sizeof(clientMessage));
strcpy(clientMessage, input.c_str());
send(client, clientMessage, strlen(clientMessage), 0);
}
close(client);
close(server);
printf("Socket closed\n");
return 0;
}