Linux socket 进程间通信
Socket进程间通信
服务端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <unistd.h>
#include <pthread.h>
#define MAX_CONNECT_NUM 2
#define BUFFER_SIZE 1024
const char *filename = "uds-tmp";
static pthread_t th;
static pthread_t th1;
void *thread_recv(void *ptr)
{
int fd = *(int *)ptr;
char buffer[BUFFER_SIZE];
bzero(buffer, BUFFER_SIZE);
while (1)
{
printf("waiting recv...\n");
int ret = recv(fd, buffer, BUFFER_SIZE, 0);
if (ret <= 0)
{
printf("recv failed\n");
close(fd);
pthread_cancel(th1);
return NULL;
}
printf("%s\n", buffer);
if (strncmp("end", buffer, 3) == 0)
{
close(fd);
exit(0);
}
}
}
void *thread_send(void *ptr)
{
int fd = *(int *)ptr;
while(1)
{
char buff[128] = {0};
fgets(buff, 128, stdin);
int ret = send(fd, buff, 128, 0);
if (ret <= 0)
{
close(fd);
pthread_cancel(th);
return NULL;
}
}
}
int main()
{
int fd, new_fd, len, i;
struct sockaddr_un un;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
{
printf("Request socket failed!\n");
return -1;
}
un.sun_family = AF_UNIX;
unlink(filename);
strcpy(un.sun_path, filename);
if (bind(fd, (struct sockaddr *)&un, sizeof(un)) < 0)
{
printf("bind failed!\n");
return -1;
}
if (listen(fd, MAX_CONNECT_NUM) < 0)
{
printf("listen failed!\n");
return -1;
}
while (1)
{
printf("wait to accept...\n");
new_fd = accept(fd, NULL, NULL);
printf("new accept.\n");
if (new_fd < 0)
{
printf("accept failed\n");
return -1;
}
pthread_create(&th, NULL, thread_recv, (void *)(&new_fd));
pthread_create(&th1, NULL, thread_send, (void *)(&new_fd));
}
close(fd);
}
客户端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <errno.h>
#include <stddef.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFER_SIZE 1024
const char *filename = "uds-tmp";
void *thread_recv(void *ptr)
{
int sock_fd = *(int *)ptr;
char buffer[128] = {0};
while (1)
{
int ret = recv(sock_fd, buffer, 128, 0);
if (ret <= 0)
{
printf("recv error: %s\n", strerror(ret));
close(sock_fd);
//exit(EXIT_FAILURE);
} else {
printf("%s\n", buffer);
}
}
}
int main()
{
while (1)
{
struct sockaddr_un un;
int sock_fd;
char buffer[BUFFER_SIZE] = "hello world";
un.sun_family = AF_UNIX;
strcpy(un.sun_path, filename);
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0)
{
printf("Request socket failed\n");
return -1;
}
if (connect(sock_fd, (struct sockaddr *)&un, sizeof(un)) < 0)
{
printf("connect socket failed\n");
return -1;
}
pthread_t pth;
pthread_create(&pth, NULL, thread_recv, (void *)(&sock_fd));
while (1)
{
memset(buffer, 0, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
printf("send data: %s\n", buffer);
int error = 0;
socklen_t len = sizeof(error);
int retval = getsockopt(sock_fd, SOL_SOCKET, SO_ERROR, &error, &len);
if (retval != 0)
{
/* there was a problem getting the error code */
fprintf(stderr, "error getting socket error code: %s\n", strerror(retval));
return 0;
}
if (error != 0)
{
/* socket has a non zero error status */
fprintf(stderr, "socket error: %s\n", strerror(error));
return 0;
}
ssize_t size = send(sock_fd, buffer, BUFFER_SIZE, MSG_NOSIGNAL);
if (size <= 0)
{
// close(sock_fd);
// return 0;
printf("send data failed.\n");
}
}
}
return 0;
}
测试
先打开服务端,再打开客户端。
服务端发送:socket server send data
。
客户端发送:socket client send data
和 test
。
关闭客户端后,重新打开客户端。
客户端发送:client send data
。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?