进程间通信,双向通信,1. 管道,使用管道需要开两个,一个用于收,一个用于发,另外就是需要设置成非阻塞,不然read, write函数都会挂住等待对方读或写,还有数据无法清空,如果有数据未读完,读进程一启动就会收到旧的数据。
2.dbus通信,也是要开两路,还有就是与环境相关,遇到过在pc端正常通信,在arm端无法通信的问题,可能是某些配置文件需要修改
最后找到了unix domin socket来进行进程间IPC通信,与网络socket不同,它不需要占用端口,只需绑定一个文件
NAME(名称)
unix, PF_UNIX, AF_UNIX, PF_LOCAL, AF_LOCAL - 用于本地内部进程通讯的套接字。
SYNOPSIS(总览)
#include
#include
unix_socket = socket(PF_UNIX, type, 0);
error = socketpair(PF_UNIX, type, 0, int *sv);
DESCRIPTION(描述)
PF_UNIX (也称作 PF_LOCAL ) 套接字族用来在同一机器上的提供有效的进程间通讯.Unix 套接字可以是匿名的(由 socketpair(2) 创建), 也可以与套接字类型文件相关联. Linux
还支持一种抽象名字空间, 它是独立于文件系统的.
有效的类型有: SOCK_STREAM 用于面向流的套接字, SOCK_DGRAM 用于面向数据报的套接字,其可以保存消息界限. Unix 套接字总是可靠的,而且不会重组数据报.
Unix 套接字支持把文件描述符或者进程的信用证明作为数据报的辅助数据 传递给其它进程.
server.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_COUNT 5 // how many pending connections queue will hold
#define BUF_SIZE 256
static int s_clientFds[MAX_COUNT]; // accepted connection fd
static int conn_amount = 0; // current connection amount
static int s_sock_fd = -1;
int init_unix_socket(const char * name)
{
int sock_fd;
struct sockaddr_un server_addr;
int yes = 1;
if ((sock_fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
return -1;
}
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
close(sock_fd);
return -1;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sun_family = AF_UNIX;
strcpy(server_addr.sun_path, name);
unlink(name);
if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
close(sock_fd);
return -1;
}
if (listen(sock_fd, MAX_COUNT) == -1) {
perror("listen");
close(sock_fd);
return -1;
}
return sock_fd;
}
int poll_data(int sock_fd, int *maxsock, void (*read_data_callback)(int, char *))
{
int new_fd;
struct sockaddr_in client_addr;
socklen_t sin_size = sizeof(client_addr);
char buf[BUF_SIZE];
int ret;
int i;
fd_set fdsr;
struct timeval tv;
// initialize file descriptor set
FD_ZERO(&fdsr);
FD_SET(sock_fd, &fdsr);
// timeout setting
tv.tv_sec = 1;
tv.tv_usec = 0;
// add active connection to fd set
for (i = 0; i < MAX_COUNT; i++) {
if (s_clientFds[i] != 0) {
FD_SET(s_clientFds[i], &fdsr);
}
}
ret = select(*maxsock + 1, &fdsr, NULL, NULL, &tv);
if (ret < 0) {
perror("select");
return -1;
} else if (ret == 0) {
printf("timeout\n");
return 0;
}
// check every fd in the set
for (i = 0; i < conn_amount; i++) {
if (FD_ISSET(s_clientFds[i], &fdsr)) {
ret = recv(s_clientFds[i], buf, sizeof(buf), 0);
if (ret <= 0) { // client close
printf("client[%d] close\n", i);
close(s_clientFds[i]);
FD_CLR(s_clientFds[i], &fdsr);
s_clientFds[i] = 0;
} else { // receive data
if (ret < BUF_SIZE)
memset(&buf[ret], '\0', 1);
//printf("client[%d] send:%s\n", i, buf);
read_data_callback(s_clientFds[i], buf);
}
}
}
// check whether a new connection comes
if (FD_ISSET(sock_fd, &fdsr)) {
new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size);
if (new_fd <= 0) {
perror("accept");
return 0;
}
// add to fd queue
if (conn_amount < MAX_COUNT) {
s_clientFds[conn_amount++] = new_fd;
printf("new connection client[%d] %s:%d\n", conn_amount,
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
if (new_fd > *maxsock)
*maxsock = new_fd;
}
else {
printf("max connections arrive, exit\n");
send(new_fd, "bye", 4, 0);
close(new_fd);
return -1;
}
}
return 0;
}
int write_unix_socket(int sock, const char *data)
{
printf("write:%s", data);
if (sock != -1) {
uint32_t size = strlen(data);
ssize_t c = write(sock, data, size);
if (c < 0) {
printf("write to socket failed: %s", strerror(errno));
return c;
}
if (c == 0) {
printf("write to socket failed: remote closed\n");
return c;
}
return c;
} else {
return -1;
}
}
void data_callback(int clientSock, char *buf)
{
printf("get:%s\n", buf);
write_unix_socket(clientSock, "replay haha");
}
int main(void)
{
char * name = "/tmp/unix.sock.bt";
s_sock_fd = init_unix_socket(name);
if (s_sock_fd == -1)
return -1;
conn_amount = 0;
int maxsock = s_sock_fd;
while (1) {
if (0 != poll_data(s_sock_fd, &maxsock, data_callback))
break;
}
// close other connections
for (int i = 0; i < MAX_COUNT; i++) {
if (s_clientFds[i] != 0) {
close(s_clientFds[i]);
}
}
shutdown(s_sock_fd, SHUT_RDWR);
close(s_sock_fd);
return 0;
}
client.cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int ReadData(int fd, void *rcv_buf, int len, int sec, int usec)
{
int retval;
fd_set rfds;
struct timeval tv;
if (fd < 0)
return -1;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = sec; // set the rcv wait time
tv.tv_usec = usec; // 100000us = 0.1s
retval = select(fd + 1, &rfds, NULL, NULL, &tv);
if (retval == -1)
{
printf("select failed:%s", strerror(errno));
}
else if (retval)
{
retval = read(fd, rcv_buf, len);
}
else
{
// timeout
}
return retval;
}
int main(int argc, char** argv)
{
int sockfd, n;
char sendline[4096];
struct sockaddr_un servaddr;
if ((sockfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
{
printf("create socket error: %s(errno: %d)\n", strerror(errno),errno);
return -1;
}
std::string name = "/tmp/unix.sock.bt";
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sun_family = AF_UNIX;
strcpy(servaddr.sun_path, name.c_str());
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
{
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
return -1;
}
char buf[256];
while (1)
{
strcpy(sendline, "client....");
if (send(sockfd, sendline, strlen(sendline), 0) < 0)
{
printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
return -1;
}
if (ReadData(sockfd, buf, sizeof(buf), 3, 0) > 0)
{
printf("get:%s\n", buf);
}
sleep(1);
}
shutdown(sockfd, SHUT_RDWR);
//close(sockfd);
return 0;
}
执行:
client端,不停发消息client...
./2.out
get:replay haha
get:replay haha
get:replay haha
get:replay haha
server 收到消息就回复haha
./1.out
timeout
timeout
new connection client[1] 0.0.0.0:0
get:client....
write:replay hahaget:client....
write:replay hahaget:client....
write:replay hahaget:client....
write:replay hahaget:client....
write:replay hahaget:client....
write:replay hahaclient[0] close // 客户端程序强制杀掉或者退出后,服务端会自动释放
timeout
timeout
timeout
作者:帅得不敢出门