码迷,mamicode.com
首页 > 其他好文 > 详细

[001]socket通信--server和client实现迭代的简单例子

时间:2014-11-18 15:52:05      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   sp   for   div   

server端:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<unistd.h>
 5 #include<arpa/inet.h>
 6 #include<sys/socket.h>
 7 
 8 #define BUFF_SIZE 1024
 9 void error_handling(char* message);
10 
11 void error_handling(char* message) {
12     fputs(message, stderr);
13     fputc(\n, stderr);
14     exit(1);
15 }
16 
17 int main(int argc, char **argv)
18 {
19     int serv_sock;
20      int clnt_sock;
21     char message[BUFF_SIZE];
22     int i,str_len;
23 
24     //设置一个server地址serv_addr,client地址clnt_addr
25     struct sockaddr_in serv_addr;
26     struct sockaddr_in clnt_addr;
27     socklen_t clnt_addr_size = sizeof(clnt_addr);
28 
29     memset(&serv_addr, 0, sizeof(serv_addr));
30     memset(&serv_addr, 0, sizeof(serv_addr));
31 
32     if (argc != 2) {
33         printf("Usage : %s <port>\n", argv[0]);
34         exit(1);
35     }
36 
37     //创建用于internet的流协议(TCP)socket
38     serv_sock = socket(PF_INET, SOCK_STREAM, 0);
39     if (serv_sock == -1) {
40         error_handling("socket() error");
41     }
42  
43     serv_addr.sin_family = AF_INET;
44     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
45     serv_addr.sin_port = htons(atoi(argv[1]));
46 
47     //把socket和socket地址结构联系起来
48     if( bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1) {
49         error_handling("bind() error");
50     }
51 
52     //server_socket用于监听
53     if ( listen(serv_sock, 5) == -1) {
54         error_handling("lisen() error");
55     }
56 
57     // 受理连接请求
58     for (i = 0; i < 5; i++) {
59         clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&clnt_addr_size);
60         if ( clnt_sock == -1) {
61             error_handling("accept() error");
62         } else {
63             printf("connected client %d \n", i+1);
64         }
65 
66         while((str_len = read(clnt_sock, message, BUFF_SIZE)) != 0) {
67             write(clnt_sock, message, str_len);
68         }
69 
70         close(clnt_sock);
71     }
72     close(serv_sock);
73 
74     return 0;
75 }

client端:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<unistd.h>
 5 #include<arpa/inet.h>
 6 #include<sys/socket.h>
 7 
 8 #define BUFF_SIZE 1024
 9 void error_handling(char* message);
10 
11 void error_handling(char* message) {
12     fputs(message, stderr);
13     fputc(\n, stderr);
14     exit(1);
15 }
16 
17 int main(int argc, char **argv) {
18     int sock;
19     struct sockaddr_in serv_addr;
20     char message[BUFF_SIZE];
21     int str_len;
22     memset(&serv_addr, 0, sizeof(serv_addr));
23 
24     if (argc != 3)
25     {
26         printf("Usage: /%s <IP> <Port>\n",argv[0]);
27         exit(1);
28     }
29 
30     //创建用于internet的流协议(TCP)socket
31     sock = socket(PF_INET, SOCK_STREAM, 0);
32     if (sock == -1) {
33         error_handling("socket() error");
34     }
35 
36     //设置一个socket地址结构client_addr,代表客户机internet地址, 端口
37     serv_addr.sin_family = AF_INET;
38     serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
39     serv_addr.sin_port = htons(atoi(argv[2]));
40 
41     //把socket和socket地址结构联系起来
42     if( connect(sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1) {
43         error_handling("connect() error");
44     } else {
45         printf("connected.......\n");
46     }
47 
48     while(1) {
49         fputs("Input mesage(Q to quit) : ", stdout);
50         fgets(message, BUFF_SIZE, stdin);
51 
52         if (!strcmp(message, "q\n") || !strcmp(message, "Q\n")) {
53             break;
54         }
55 
56         write(sock, message, strlen(message));
57         str_len = read(sock, message, BUFF_SIZE - 1);
58         message[str_len] = 0;
59         printf("Message from server : %s \n", message);
60     }
61     close(sock);
62     return 0;
63 }

 

[001]socket通信--server和client实现迭代的简单例子

标签:style   blog   io   color   ar   os   sp   for   div   

原文地址:http://www.cnblogs.com/hustcser/p/4105962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!