-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
64 lines (62 loc) · 1.81 KB
/
server.c
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
55
56
57
58
59
60
61
62
63
64
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#define PORT "8888"
int main(int argc, char const* argv[])
{
int server_fd, new_socket;
ssize_t valread;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[1024] = { 0 };
char* hello = "Hello from server";
struct addrinfo hints;
struct addrinfo *servinfo;
char sendbuff[2048] = {0};
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL,PORT,&hints, &servinfo);
int count = 0;
char zeros[1024] = {0};
if ((server_fd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (bind(server_fd, servinfo->ai_addr, servinfo->ai_addrlen) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr*)&address, &addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
while(1){
memset(buffer,0,sizeof(buffer));
valread = read(new_socket, buffer, 1023);
printf("%s\n",buffer);
if(strcmp(buffer,"exit") == 0){
printf("exit received\n");
close(new_socket);
close(server_fd);
return 0;
}
if(memcmp(buffer, zeros,sizeof(buffer))) {
memset(sendbuff,0,sizeof(sendbuff));
sprintf(sendbuff,"%s rx ", buffer);
send(new_socket, sendbuff, sizeof(sendbuff), 0);
}
}
close(new_socket);
close(server_fd);
return 0;
}