linux unix domain,socket unix domain IPC的实例代码

仅供参考:

服务端:socket->bind->listen->send/recv->close

客户端:socket->bind->connect->send/recv->close

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define filename "test.socket"

void setnonblocking(int fd)

{

fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);

}

void *client_func(void *arg)

{

sleep(3);

int fd = socket(AF_UNIX, SOCK_STREAM, 0);

setnonblocking(fd);

sockaddr_un un;

memset(&un, 0, sizeof(un));

un.sun_family = AF_UNIX;

sprintf(un.sun_path, "file_%d.socket", (int)getpid());

unlink(un.sun_path);

socklen_t len = sizeof(un);

bind(fd, (sockaddr *)&un, sizeof(un));

strcpy(un.sun_path, filename);

int ret = connect(fd, (sockaddr *)&un, len);

if (ret == -1)

{

printf("connect server failed...\n");

close(fd);

return NULL;

}

char buf[256];

memset(buf, 0, sizeof(buf));

strcpy(buf, "hello world");

int n = send(fd, buf, strlen(buf)+1, 0);

printf("send data, %d bytes..\n", n);

sleep(5);

close(fd);

return NULL;

}

int main()

{

unlink(filename);

signal(SIGPIPE, SIG_IGN);

int fd = socket(AF_UNIX, SOCK_STREAM, 0);

int yes = 1;

setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

setnonblocking(fd);

sockaddr_un un;

memset(&un, 0, sizeof(un));

un.sun_family = AF_UNIX;

strcpy(un.sun_path, filename);

bind(fd, (sockaddr *)&un, sizeof(un));

listen(fd, 100);

pthread_t pid;

pthread_create(&pid, NULL, client_func, NULL);

sockaddr_un uu;

socklen_t len = sizeof(uu);

while (true)

{

memset(&uu, 0, len);

int newfd = accept(fd, (sockaddr *)&uu, &len);

if (newfd != -1)

{

setnonblocking(newfd);

printf("newfd = %d, path = %s\n", newfd, uu.sun_path);

char buf[512];

memset(buf, 0, sizeof(buf));

int n = recv(newfd, buf, 512,0);

printf("recv %d bytes, contents is %s\n", n, buf);

}

usleep(100000);

}

close(fd);

return 0;

}

以上就是小编为大家带来的socket unix domain IPC的实例代码全部内容了,希望大家多多支持脚本之家~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 Unix Domain Socket 通信实例,包括了服务端和客户端代码: 服务端代码: ```cpp #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #define SOCK_PATH "/tmp/echo_socket" int main(int argc, char *argv[]) { int s, s2, len; struct sockaddr_un local, remote; char str[100]; if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } local.sun_family = AF_UNIX; strcpy(local.sun_path, SOCK_PATH); unlink(local.sun_path); len = strlen(local.sun_path) + sizeof(local.sun_family); if (bind(s, (struct sockaddr *)&local, len) == -1) { perror("bind"); exit(1); } if (listen(s, 5) == -1) { perror("listen"); exit(1); } printf("Waiting for a connection...\n"); while (1) { int done, n; unsigned int t; t = sizeof(remote); if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) { perror("accept"); exit(1); } printf("Connected.\n"); done = 0; do { n = recv(s2, str, 100, 0); if (n <= 0) { if (n < 0) perror("recv"); done = 1; } if (!done) { if (send(s2, str, n, 0) < 0) { perror("send"); done = 1; } } } while (!done); close(s2); } return 0; } ``` 客户端代码: ```cpp #include <sys/socket.h> #include <sys/un.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #define SOCK_PATH "/tmp/echo_socket" int main(int argc, char *argv[]) { int s, len; struct sockaddr_un remote; char *str = "hello, world!"; if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } remote.sun_family = AF_UNIX; strcpy(remote.sun_path, SOCK_PATH); len = strlen(remote.sun_path) + sizeof(remote.sun_family); if (connect(s, (struct sockaddr *)&remote, len) == -1) { perror("connect"); exit(1); } if (send(s, str, strlen(str), 0) == -1) { perror("send"); exit(1); } if (recv(s, str, strlen(str), 0) == -1) { perror("recv"); exit(1); } printf("Received: %s\n", str); close(s); return 0; } ``` 服务端代码创建了一个 Unix Domain Socket 并监听,一旦有客户端连接进来,服务端就接收客户端发送的数据,并将其原样返回。客户端代码连接到服务端,并发送一个字符串,然后等待服务端返回数据并打印。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值