-
Notifications
You must be signed in to change notification settings - Fork 1
/
learner1.c
54 lines (49 loc) · 1.04 KB
/
learner1.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
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
int address_len;
struct sockaddr_in address;
int listenfd;
fd=socket(AF_INET, SOCK_STREAM ,0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(8888);
address_len = sizeof(address);
bind(fd,(struct sockaddr *)&address, address_len);
listenfd = listen(fd,100);
while(1)
{
struct sockaddr_in client_address;
int len;
int client_sockfd;
printf("Learner waiting...\n");
fflush(stdout);
len = sizeof(client_address);
client_sockfd = accept(fd,(struct sockaddr *)&client_address, &len);
int pid;
if(pid = fork()==0)
{
close(listenfd);
while(1)
{
char data2[100];
memset((void *)data2,0,100);
read(client_sockfd, (void *)data2,100);
if(strlen(data2)==0)
{
close(client_sockfd);
exit(0);
}
printf("Server read line:%s\n",data2+4);
}
close(client_sockfd);
exit(0);
}
}
}