#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 = ",xiebuf); read(cfd,buf,sizeof(buf)); printf("buf : %s ",buf); } close(cfd); return 0; } } close(lfd); return 0; }