Socket编程:之双机通信

服务端:

 1 #include<sys/socket.h>
 2 #include<sys/types.h>
 3 #include<stdio.h>
 4 #include<unistd.h>
 5 #include<stdlib.h>
 6 #include<string.h>
 7 #include<netdb.h>
 8 #include<netinet/in.h>
 9 #include<arpa/inet.h>
10 #include<pthread.h>
11 
12 
13 void recv_data(void* fd)
14 {
15     int sockfd = *((int *)fd);
16     char buf[200];
17     int  renum;
18     while(1)
19     {
20     bzero(buf, 200);
21 
22     if((renum = recv(sockfd, buf, 200, 0)) > 0)
23     {
24         printf("recv: %s\n", buf);
25     }
26     else if(renum == -1)
27     {
28         printf("recv -1");
29         exit(-1);
30     }
31     }
32 }
33 int main(void)
34 {
35     pthread_t pid;
36     int sockfd;
37     struct sockaddr_in serveraddr;
38 
39     char buf[200];
40     sockfd = socket(AF_INET, SOCK_STREAM, 0);
41     bzero(&serveraddr, sizeof(struct sockaddr));
42     serveraddr.sin_family = AF_INET;
43     serveraddr.sin_port = htons(4444);
44     serveraddr.sin_addr.s_addr = inet_addr("192.168.0.100");
45 
46 
47     if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr)) == -1)
48     {
49         printf("connect failed\n");
50         exit(-1);
51     }
52     else
53         printf("connect succeed \n");
54     pthread_create(&pid, NULL, (void*)recv_data,(void*)&sockfd);
55     while(1)
56     {
57         memset(buf, 0, 200);
58         gets(buf);
59 
60         send(sockfd, buf, strlen(buf), 0);
61         if(strncmp(buf, "quit", 4) == 0)
62         {
63             close(sockfd);
64             return 0;
65         }
66 
67     }
68     close(sockfd);
69     return 0;
70 }

 

客户端:

  1 #include<sys/socket.h>
  2 #include<sys/types.h>
  3 #include<stdio.h>
  4 #include<unistd.h>
  5 #include<stdlib.h>
  6 #include<string.h>
  7 #include<netdb.h>
  8 #include<netinet/in.h>
  9 #include<arpa/inet.h>
 10 #include<pthread.h>
 11 
 12 void recv_data(void* fd)
 13 {
 14     int sockfd = *((int *)fd);
 15     char buf[200];
 16     int  renum;
 17     while(1)
 18     {
 19         bzero(buf, 200);
 20         
 21         //recv(sockfd, buf, 200, 0);//如果断开 ,recv不再阻塞  返回值 0
 22         //printf("recv: %s\n", buf);
 23 
 24         if((renum = recv(sockfd, buf, 200, 0)) > 0)
 25             printf("recv: %s\n", buf);
 26         else if(renum == -1)
 27         {
 28             printf("recv -1");
 29             exit(-1);
 30         }
 31         else 
 32         {
 33             close(sockfd);
 34             exit(1);
 35             
 36         }
 37         
 38     
 39     }
 40 
 41 }
 42 int main(void)
 43 {
 44     pthread_t pid;
 45     int sockfd, clientfd;
 46     int sinsize;
 47     char buf[200];
 48     struct sockaddr_in myaddr, clientaddr;
 49     sockfd = socket(AF_INET, SOCK_STREAM, 0);    
 50     if(sockfd == -1)
 51     {
 52         printf("socket failed\n");
 53         exit(-1);
 54     }
 55     printf("%d\n", sockfd);
 56 
 57     bzero(&myaddr, sizeof(struct sockaddr));
 58     myaddr.sin_family = AF_INET;
 59     myaddr.sin_port = htons(4444);
 60     myaddr.sin_addr.s_addr =inet_addr("192.168.0.100");
 61     sinsize = 1;
 62     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sinsize, sizeof(int));
 63     if(bind(sockfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr)) == -1)
 64     {
 65         printf("bind failed\n");
 66         exit(-1);
 67     }
 68 
 69     if(listen(sockfd, 1) == -1)
 70     {
 71         printf("listen failed\n");
 72         exit(-1);
 73     }
 74     sinsize = sizeof(struct sockaddr);
 75     if((clientfd = accept(sockfd, (struct sockaddr *)&clientaddr, &sinsize)) == -1)
 76     {
 77         printf("accept failed\n");
 78         exit(-1);
 79     }
 80     else
 81         printf("accept succeed\n");
 82     pthread_create(&pid, NULL, (void*)recv_data,(void*)&clientfd);
 83     while(1)
 84     {
 85         memset(buf, 0, 200);
 86         gets(buf);
 87         if(strncmp(buf, "quit", 4) == 0)
 88         {
 89             close(clientfd);
 90             close(sockfd);
 91             return 0;
 92         }
 93         send(clientfd, buf, strlen(buf), 0);
 94     }
 95     sleep(1);
 96 
 97     close(clientfd);
 98     close(sockfd);
 99 
100     return 0;
101 }

 

posted @ 2016-08-04 22:41  极客先锋  阅读(937)  评论(0编辑  收藏  举报