Linux socket本地进程间通信之TCP

当套接字用于本地通信时,可以使用结构体struct sockaddr_un描述一个本地地址。

1 struct sockaddr_un{
2      unsigned short sun_family;/*协议类型*/
3      char sun_path[108];          /*套接字文件路径*/
4 }

在本地通信中,每个套接字文件代表一个本地地址。

UNIX域流式套接字服务器端流程如下:

(1)创建UNIX域流式套接字;socket(AF_UNIX, SOCK_STREAM, 0)

(2)填充本地信息结构体(服务器);struct sockaddr_un

(3)绑定本地地址(服务器的地址信息);bind( )

(4)设置监听模式;listen( )

(5)接收客服端的连接请求;accept( )

(6)接收客户端的数据;recv( )

(7)发送数据给客户端;send( )

服务器端代码如下:

server.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<sys/types.h>
 4 #include<unistd.h>
 5 #include<sys/socket.h>
 6 #include<netinet/in.h>
 7 #include<arpa/inet.h>
 8 #include<string.h>
 9 #include<errno.h>
10 #include<sys/un.h>
11 #include<stdio.h>
12 
13 #define N 64
14 
15 int main(int argc, const char *argv[])
16 {
17     int sockfd, connectfd;
18     char buf[N];
19     struct sockaddr_un serveraddr, clientaddr;
20     socklen_t len = sizeof(clientaddr);
21 
22     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
23     if(sockfd < 0)
24     {
25         perror("fail to socket");
26         return -1;
27     }
28 
29     serveraddr.sun_family = AF_UNIX;
30     strcpy(serveraddr.sun_path, "mysocket");
31 
32     if(bind(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
33     {
34         perror("fail to bind");
35         return -1;
36     }
37 
38     if(listen(sockfd, 5) < 0)
39     {
40         perror("fail to listen");
41         return -1;
42     }
43 
44     if((connectfd = accept(sockfd, (struct sockaddr*)&clientaddr, &len)) < 0)
45     {
46         perror("fail to accept");
47         return -1;
48     }
49 
50     while(1)
51     {
52         if(recv(connectfd, buf, N, 0) < 0)
53         {
54             perror("fail to recv");
55             return -1;
56         }
57         if(strncmp(buf, "quit", 4) == 0)
58         {
59             break;
60         }
61         buf[strlen(buf) - 1] = '\0';
62         printf("buf:%s\n", buf);
63         strcat(buf, "+++***---");
64         if(send(connectfd, buf, N, 0) < 0)
65         {
66             perror("fail to send");
67             return -1;
68         }
69     }
70     close(sockfd);
71     return 0;
72 }

UNIX域流式套接字客户端流程如下:

(1)创建UNIX域流式套接字;socket(AF_UNIX, SOCK_STREAM, 0)

(2)填充本地信息结构体(服务器);struct sockaddr_un

(3)建立与服务器的连接;connect( )

(4)发送数据给服务器端;send( )

(5)接收服务器端的数据;recv( )

client.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<sys/types.h>
 4 #include<unistd.h>
 5 #include<sys/socket.h>
 6 #include<arpa/inet.h>
 7 #include<netinet/in.h>
 8 #include<string.h>
 9 #include<sys/un.h>
10 
11 #define N 64
12 
13 int main(int argc, const char *argv[])
14 {
15     int sockfd;
16     struct sockaddr_un serveraddr;
17     char buf[N];
18 
19     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
20     if(sockfd < 0)
21     {
22         perror("fail to sockfd");
23         return -1;
24     }
25 
26     serveraddr.sun_family = AF_UNIX;
27     strcpy(serveraddr.sun_path, "mysocket");
28 
29     if(connect(sockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr)) < 0)
30     {
31         perror("fail to connect");
32         return -1;
33     }
34 
35     while(1)
36     {
37         printf("<client>");
38         fgets(buf, N, stdin);
39         if(send(sockfd, buf, N, 0) < 0)
40         {
41             perror("fail to send");
42             return -1;
43         }
44         if(strncmp(buf, "quit", 4) == 0)
45         {
46             break;
47         }
48         if(recv(sockfd, buf, N, 0) < 0)
49         {
50             perror("fail to recv");
51             return -1;
52         }
53         printf("buf:%s\n", buf);
54     }
55     close(sockfd);
56     return 0;
57 }

服务器端运行结果如下:

 客户端运行结果如下:

 

posted @ 2015-12-07 09:37  文纸  阅读(1276)  评论(0编辑  收藏  举报