Linux网络编程-本地socket通信

本地socket通信

服务端

  • unix_server.c
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    /*
    服务端的开发流程:
    1.创建socket(基于TCP协议)
    lfd = socket(AF_UNIX, SOCK_STREAM, 0);
    2.绑定--bind()
    struct sockaddr_un serv;
    bzero(&serv, sizof(serv));
    serv.sun_family = AF_UNIX;
    strcpy(serv.sun_path, "./serv.sock");
    bind(lfd, (struct sockaddr *)&serv, sizeof(serv));
    3.监听--listen()
    4.接受新的客户端连接
    cfd = accept();
    5.传输数据
    while(1)
    {
    //读取数据--read()
    if(errno)
    {
    close(cfd);
    break;
    }
    //发送数据--write()
    }
    6.关闭socket套接字
    close(cfd);
    close(lfd);
    */
    //本地socket通信服务端
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/un.h>

    int main()
    {
    //创建socket
    int lfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (lfd < 0)
    {
    perror("socket error");
    return -1;
    }

    //删除socket文件,避免bind失败
    unlink("./server.sock");

    //绑定bind
    struct sockaddr_un serv;
    bzero(&serv, sizeof(serv));
    serv.sun_family = AF_UNIX;
    strcpy(serv.sun_path, "./server.sock");
    int ret = bind(lfd, (struct sockaddr *)&serv, sizeof(serv));
    if (ret < 0)
    {
    perror("bind error");
    return -1;
    }

    //监听listen
    listen(lfd, 10);

    //接收新的客户端连接accept
    struct sockaddr_un client;
    bzero(&client, sizeof(client));
    int len = sizeof(client);
    int cfd = accept(lfd, (struct sockaddr *)&client, &len);
    if (cfd < 0)
    {
    perror("accept error");
    return -1;
    }
    printf("client->[%s]\n", client.sun_path);

    int n;
    char buf[1024];

    while (1)
    {
    //读数据
    memset(buf, 0x00, sizeof(buf));
    n = read(cfd, buf, sizeof(buf));
    if (n <= 0)
    {
    printf("read error or client close, n=[%d]\n", n);
    break;
    }
    printf("n=[%d], buf=[%s]\n", n, buf);

    //发送数据
    write(cfd, buf, n);
    }
    //关闭文件描述符
    close(cfd);
    close(lfd);

    return 0;
    }

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
客户端的开发流程:
1.创建socket(基于TCP协议)
cfd = socket(AF_UNIX, SOCK_STREAM, 0);
2.连接服务端--connect()
3.传输数据
while(1)
{
//发送数据--write()

//读取数据--read()
if(errno)
{
close(cfd);
break;
}
}
4.关闭socket套接字
close(cfd);
*/
//本地socket通信客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/un.h>

int main()
{
//创建socket
int cfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (cfd < 0)
{
perror("socket error");
return -1;
}

//删除socket文件,避免bind失败
unlink("./client.sock");

//绑定bind
struct sockaddr_un client;
bzero(&client, sizeof(client));
client.sun_family = AF_UNIX;
strcpy(client.sun_path, "./client.sock");
int ret = bind(cfd, (struct sockaddr *)&client, sizeof(client));
if (ret < 0)
{
perror("bind error");
return -1;
}

//连接服务端
struct sockaddr_un serv;
bzero(&serv, sizeof(serv));
serv.sun_family = AF_UNIX;
strcpy(serv.sun_path, "./server.sock");
ret = connect(cfd, (struct sockaddr *)&serv, sizeof(serv));
if (ret < 0)
{
perror("connect error");
return -1;
}

int n;
char buf[1024];

while (1)
{
//读标准输入
memset(buf, 0x00, sizeof(buf));
n = read(STDIN_FILENO, buf, sizeof(buf));

//发送数据
write(cfd, buf, n);

//读取数据
memset(buf, 0x00, sizeof(buf));
n = read(cfd, buf, sizeof(buf));
if (n <= 0)
{
printf("read error or client close, n=[%d]\n", n);
break;
}
printf("n=[%d], buf=[%s]\n", n, buf);
}
//关闭文件描述符
close(cfd);

return 0;
}

Linux网络编程-本地socket通信
https://lcf163.github.io/2021/05/05/Linux网络编程-本地socket通信/
作者
乘风的小站
发布于
2021年5月5日
许可协议