1  /* tcpcli.c */
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h> 
  5 #include <unistd.h> 
  6 #include <sys/types.h> 
  7 #include <sys/socket.h>
  8 #include <netinet/in.h>
  9 #include <arpa/inet.h>
 10 
 11 #define DEFAULT_PORT 8800
 12 
 13 int main(int argc, char** argv)
 14 {
 15     int cPort = DEFAULT_PORT;
 16     int cClient = 0;
 17     int cLen = 0;
 18     struct sockaddr_in cli;
 19     char cbuf[4096] = {0};
 20     
 21     if(argc < 2)
 22     {
 23         printf("Uasge: client[server IP address]\n");
 24         return -1;
 25     }
 26     
 27     memset(cbuf, 0, sizeof(cbuf));
 28     
 29     cli.sin_family = AF_INET;
 30     cli.sin_port = htons(cPort);
 31     cli.sin_addr.s_addr = inet_addr(argv[1]);
 32     
 33     cClient = socket(AF_INET, SOCK_STREAM, 0);
 34     if(cClient < 0)
 35     {
 36         printf("socket() failure!\n");
 37         return -1; 
 38     }
 39     
 40     if(connect(cClient, (struct sockaddr*)&cli, sizeof(cli)) < 0)
 41     {
 42         printf("connect() failure!\n");
 43         return -1;
 44     }
 45     
 46     cLen = recv(cClient, cbuf, sizeof(cbuf),0);    
 47     if((cLen < 0)||(cLen == 0))
 48     {
 49           printf("recv() failure!\n");
 50         return -1;
 51     }
 52     printf("recv() Data From Server: [%s]\n", cbuf);
 53     
 54     close(cClient);
 55     
 56     return 0;
 57 }
 58 
 59 //编译代码:gcc -o tcp_clt  client_tcp.c
 60 
 61 //执行命令:./tcp_clt 192.168.0.230
 62 
 63 //TCP scoket服务端程序代码
 64 
 65 #include<sys/types.h>
 66 #include<sys/socket.h>
 67 #include<netinet/in.h>
 68 #include<arpa/inet.h>
 69 #include<unistd.h>
 70 #include <stdio.h>
 71 #include <stdlib.h>
 72 #include <strings.h>
 73 #include<sys/wait.h>
 74 #include <string.h>
 75 
 76 /********************************************************************* 
 77 *filename: tcpserver.c 
 78 *purpose:tcp服务端程序 
 79 ********************************************************************/
 80 int main(int argc, char ** argv) 
 81 { 
 82     int sockfd,new_fd;     /* 监听socket: sock_fd,数据传输socket: new_fd */ 
 83     struct sockaddr_in my_addr; /* 本机地址信息 */ 
 84     struct sockaddr_in their_addr; /* 客户地址信息 */ 
 85     unsigned int sin_size, myport, lisnum; 
 86  
 87     if(argv[1])  myport = atoi(argv[1]); 
 88     else myport = 8800; 
 89  
 90     if(argv[2])  lisnum = atoi(argv[2]); 
 91     else lisnum = 2; 
 92  
 93     if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 
 94         perror("socket"); 
 95         exit(1); 
 96     } 
 97  printf("socket %d ok \n",myport);
 98 
 99     my_addr.sin_family=PF_INET; 
100     my_addr.sin_port=htons(myport); 
101     my_addr.sin_addr.s_addr = INADDR_ANY; 
102     bzero(&(my_addr.sin_zero), 0); 
103     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { 
104         perror("bind"); 
105         exit(1); 
106     } 
107  printf("bind ok \n");
108  
109     if (listen(sockfd, lisnum) == -1) { 
110         perror("listen"); 
111         exit(1); 
112     }
113  printf("listen ok \n");
114  
115  /*
116     while(1) { 
117         sin_size = sizeof(struct sockaddr_in); 
118         if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { 
119             perror("accept"); 
120             continue; 
121         }
122 
123         printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr)); 
124         if (!fork()) { //子进程代码段 
125             if (send(new_fd, "Hello, world!\n", 14, 0) == -1) { 
126                 perror("send"); 
127                 close(new_fd); 
128                 exit(0); 
129             } 
130         } 
131         close(new_fd); //父进程不再需要该socket
132         waitpid(-1,NULL,WNOHANG);//等待子进程结束,清除子进程所占用资源
133     } 
134  */
135 
136     sin_size = sizeof(struct sockaddr_in); 
137     if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { 
138         perror("accept"); 
139   exit(0); 
140     } 
141     printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
142 
143  int   step = 0;
144  while(1) {
145   char  szSnd[63] = {0};
146   sprintf(szSnd,"i am server [%d]\n",step);
147   step++;
148         if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) { 
149             perror("send"); 
150             close(new_fd); 
151             break;
152         }
153 
154   printf("send msg: %s \n",szSnd);
155 
156   sleep(1);
157  }
158 
159  exit(0); 
160 }
161 }
162 
163 //编译指令:gcc -o tcp_srv tcpserver.c 
164 
165 //执行服务器端程序:./tcp_srv
166 
167 //创建成功,绑定端口成功,监听成功后,循环发送i am server字符串。

 

posted on 2016-07-28 10:08  BarneyX  阅读(212)  评论(0编辑  收藏  举报