linux下socket做进程间通信的问题
这里的通信内容是一个守护进程的全局变量(表示守护进程进行到某个阶段,从1开始到6结束);大约server端每发送4次,client端收到1次;这样的后果是,client接...
这里的通信内容是一个守护进程的全局变量(表示守护进程进行到某个阶段,从1开始到6结束);
大约server端每发送4次,client端收到1次;这样的后果是,client接收滞后很多,server的状态已经到6了,client还在接收2。我不知道说清楚没有,用下面的图来表示或许更清楚:
server发送:111112222233333444445555566666
client接收: 1 1 1 1 1 2 2 2
//server.c
int server_sockfd, client_sockfd;
size_t server_len, client_len;
struct sockaddr_un server_address; //守护进程作为socket服务端
struct sockaddr_un client_address; //应用程序主进程作为客户端
unlink ("server_socket");
server_sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
if(server_sockfd < 0){
printf("socket %d\n", server_sockfd);
return ;
}
server_address.sun_family = AF_UNIX;
strcpy (server_address.sun_path, "/tmp/sync_query.sock");
server_len = sizeof (server_address);
if(bind (server_sockfd, (struct sockaddr *) &server_address, server_len) < 0){...}
if(listen (server_sockfd, 5) < 0){...}
char buf[200];
char buf_send[16];
while (1){
LABEL_ACCEPT:
client_sockfd = accept (server_sockfd, (struct sockaddr *) &client_address, &client_len);
if (-1 == client_sockfd){ ... }
memset(buf,0,sizeof(buf));
while(1){
if(recv(client_sockfd,buf,sizeof(buf),0) <= 0) {... }
if(strcmp(buf,"step") == 0){
memset(buf_send,0,sizeof(buf_send));
sprintf(buf_send,"%d",g_cur_step);
send(client_sockfd,buf_send,sizeof(buf_send),0);
}
}
close(client_sockfd);
}
close(server_sockfd);
return ;
百度太恶心了,50行代码都贴不下!!!已经去掉所有错误处理语句和打印语句。。。
client.c见楼下。。。
//client.c:
char command[16]={0};
int ret=0;
strcpy(command,"step");
int sock_fd;
sock_fd = local_connect("/tmp/sync_query.sock", 2000);
if(sock_fd<=0){...}
while(1){
send(sock_fd, (unsigned char *)&command, sizeof(command),0);
recv(sock_fd,(unsigned char*)&ret,sizeof(ret),0);
sleep(1);
}
close(sock_fd);
查阅了一些资料,说TCP协议是发送数据流,server发送的数据一定会按顺序被client收到,是这样吗?
据说有同步异步的问题。我这是异步吗?同步怎么做?? 展开
大约server端每发送4次,client端收到1次;这样的后果是,client接收滞后很多,server的状态已经到6了,client还在接收2。我不知道说清楚没有,用下面的图来表示或许更清楚:
server发送:111112222233333444445555566666
client接收: 1 1 1 1 1 2 2 2
//server.c
int server_sockfd, client_sockfd;
size_t server_len, client_len;
struct sockaddr_un server_address; //守护进程作为socket服务端
struct sockaddr_un client_address; //应用程序主进程作为客户端
unlink ("server_socket");
server_sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
if(server_sockfd < 0){
printf("socket %d\n", server_sockfd);
return ;
}
server_address.sun_family = AF_UNIX;
strcpy (server_address.sun_path, "/tmp/sync_query.sock");
server_len = sizeof (server_address);
if(bind (server_sockfd, (struct sockaddr *) &server_address, server_len) < 0){...}
if(listen (server_sockfd, 5) < 0){...}
char buf[200];
char buf_send[16];
while (1){
LABEL_ACCEPT:
client_sockfd = accept (server_sockfd, (struct sockaddr *) &client_address, &client_len);
if (-1 == client_sockfd){ ... }
memset(buf,0,sizeof(buf));
while(1){
if(recv(client_sockfd,buf,sizeof(buf),0) <= 0) {... }
if(strcmp(buf,"step") == 0){
memset(buf_send,0,sizeof(buf_send));
sprintf(buf_send,"%d",g_cur_step);
send(client_sockfd,buf_send,sizeof(buf_send),0);
}
}
close(client_sockfd);
}
close(server_sockfd);
return ;
百度太恶心了,50行代码都贴不下!!!已经去掉所有错误处理语句和打印语句。。。
client.c见楼下。。。
//client.c:
char command[16]={0};
int ret=0;
strcpy(command,"step");
int sock_fd;
sock_fd = local_connect("/tmp/sync_query.sock", 2000);
if(sock_fd<=0){...}
while(1){
send(sock_fd, (unsigned char *)&command, sizeof(command),0);
recv(sock_fd,(unsigned char*)&ret,sizeof(ret),0);
sleep(1);
}
close(sock_fd);
查阅了一些资料,说TCP协议是发送数据流,server发送的数据一定会按顺序被client收到,是这样吗?
据说有同步异步的问题。我这是异步吗?同步怎么做?? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询