???±??????????
1 #include <mylib.h>
2 int main(void)
3 {
4     int sock_fd;
5     sock_fd=socket(AF_INET??SOCK_STREAM??0);
6
7     struct sockaddr_in serv_add;
8     bzero(&serv_add??sizeof(serv_add));
9     serv_add.sin_family=AF_INET;
10     serv_add.sin_port=htons(SERV_PORT);
11     struct in_addr add;
12     inet_aton("192.168.1.105"??&add);
13     serv_add.sin_addr=add;
14
15     if(connect(sock_fd??(struct sockaddr *)&serv_add??sizeof(serv_add))<0)
16         sys_err("connect error! ");
17     char sendbuff[MAXLINE]??recvbuff[MAXLINE];
18     char *temp;
19     ssize_t n;
20     while ((temp=fgets(sendbuff??sizeof(sendbuff)??stdin)) !=NULL)
21     {
22         int k;
23         if((k=write(sock_fd??sendbuff??sizeof(sendbuff)))<0)
24         {
25
26         }
27         if((n=read(sock_fd??recvbuff??sizeof(recvbuff)))>0)
28             printf("SVR:%s"??recvbuff);
29         if(n<0)
30             printf("fail to get data from server %s "??inet_ntoa(serv_add.sin_addr));
31         if(n==0)
32         {
33             //broken pipe ??haha  sigpipe
34             printf("%s"??"server defunct closing the socket...");
35             close(sock_fd);
36         }
37     }
38     return 0;
39 }
??????????????????
??????5?У?sock_fd=socket(AF_INET??SOCK_STREAM??0); ??socket???????????sock?????????????Э???壬??????AF_INET????tcp/ipЭ???塣???????????????????????TCP ????????????????????????...ò???е???????????????????? If it is zero??  is chosen automatically.  ???????????????????????о???????????????...
??????7??struct sockaddr_in serv_add;  ???????sock ?????????????????????ip??????Э?????????
??????10??12????????????????????????ip????????????????“?????”?????????????????????????????????????????????£????
??????15?????????????sock??????????????????????????????sock??е??????????????????????????????????????????tcp????????sock??????????ip????????????Χ????????????????vip???(<1024)????????????????????????????????????????????????????й?????????????
??????20??23?????????豸??? ???????д????? socket ?????????????? д????? pipe???????write???????С??0?????????????????????????????????????С?????????????????????????FIN?????????????????????TCP???????????????????????????????????????????????TCP???????RST??????????????????????????????д??pipe??????????????? ?????????????SIGPIPE??????????????????????????????????????????????????write????С??0
??????27????????????????????????????С??0
??????31??????????????????????????FIN??????read????????0. ?????????ó???23?е?????????socket?????
?????±??????????
1 #include <mylib.h>
2
3 int main(void)
4 {
5     int listen_fd ?? connected_fd;
6     struct sockaddr_in serv_add;
7 //-----------------------------------------------------------------------------
8     listen_fd=socket(AF_INET??SOCK_STREAM??0);
9     serv_add.sin_family=AF_INET;
10     serv_add.sin_port=htons(SERV_PORT);
11     serv_add.sin_addr.s_addr=htonl(INADDR_ANY);
12 //------------------------------------------------------------------------------
13     if(bind(listen_fd??(struct sockaddr *)&serv_add??sizeof(serv_add))<0)
14         sys_err("bind error ");
15     if(listen(listen_fd??LISTENQ)<0)
16         sys_err("listen error ");
17
18     signal(SIGCHLD??sig_chld);
19
20     __pid_t pid;
21     while (1) {
22         connected_fd = accept(listen_fd??0??0);
23        if(connected_fd<0){
24         if(errno==EINTR)
25         {
26             printf("interrupt ");
27             continue;
28         }
29         else
30             sys_err("accept eero! ");
31        }
32         if((pid=fork())==0)
33         {
34             char recevBuff[MAXLINE];
35             int n;
36             close(listen_fd);
37             while ((n=read(connected_fd??recevBuff??MAXLINE))>0) {
38                 printf("Client:%s"??recevBuff);
39                 write(connected_fd??recevBuff??MAXLINE);
40             }
41             if(n<0)
42                 sys_err("read error ");
43             close(connected_fd);
44             exit(0);
45         }
46         close(connected_fd);
47     }
48     return 0;
49 }