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
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
#include<strings.h>
#define port 8000
//#include<ctypes.h>
#include<stdlib.h>
 
int main(int argc,char * argv[]){
    int lfd,cfd,len,i;
    char buf[1024];
    struct sockaddr_in server_addr,client_addr;
    socklen_t client_len = sizeof(client_addr);
 
    lfd = socket(AF_INET,SOCK_STREAM,0);
     
    bzero(&server_addr,sizeof(server_addr));
 
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons((short)port);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     
    bind(lfd,(struct sockaddr*)&server_addr,sizeof(server_addr));
     
    listen(lfd,10);
 
    printf("wait for connect>>>");
 
    while(1){
        cfd = accept(lfd,(struct sockaddr*)&client_addr,&client_len);
        pid_t pid = fork();
        if(pid<0){
            printf("fork error");
            exit(1);
        }
 
        else if(pid >0){
            close(cfd);
        }
 
        else{
            close(lfd);
            char xiebuf[1024];
            while(fgets(xiebuf,sizeof(xiebuf),stdin)!=NULL){
            printf("xie buf = \n",xiebuf);
            read(cfd,buf,sizeof(buf));
            printf("buf : %s \n",buf);
            }
            close(cfd);
            return 0;
        }
    }
    close(lfd);
    return 0;
}