Linux匿名管道操作
匿名管道(pipe)的作用
父子进程之间的通信管道的创建
#include
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* End of #ifdef __cplusplus */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <termio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/shm.h>
#include "sample_comm.h"
#include"socket.h"
#include<unistd.h>
int mypipe[2]; //创建管道
extern VDA_OBJ_S stDetectArea2;
extern int iTest;
static char recv_buf[16];
extern pthread_mutex_t Dctmutex ;
int make_socket(int type, unsigned short int port)
{
int sock;
struct sockaddr_in name;
//创建套接字
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
perror("socket");
}
//命名套接字
bzero(&name,sizeof(name));
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
//int opt =SO_REUSEADDR;
// setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if(bind(sock,(struct sockaddr *)&name,sizeof(name)) == -1)
{
perror("bind");
exit(1);
}
return sock;
}
void doit(int client_sockfd )
{
char buf[255];
int n;
int rv;
/* 接收并回应客户消息 */
char* strtest ="fuck you ";
strcpy(buf,strtest);
int ret=recv(client_sockfd, recv_buf, sizeof(recv_buf),0);
if(ret < 0)
{
printf("mgs:%s recv fail!errno is:%d,error info is:%s\n", buf, errno, strerror(errno));
//break;
}else
{
unsigned short m_left;
unsigned short m_right;
unsigned short m_top;
unsigned short m_bottom;
m_left =*((unsigned short*)(recv_buf+2));
m_right = *((unsigned short*)(recv_buf+4));
m_top = *((unsigned short*)(recv_buf+6));
m_bottom = *((unsigned short*)(recv_buf+8));
write(mypipe[1],recv_buf,sizeof(recv_buf));
}
n = send(client_sockfd, buf, strlen(buf)-1 , 0);
if(n < 0){
printf("mgs:%s send fail!errno is:%d,error info is:%s\n", buf, errno, strerror(errno));
//break;
}else{
printf("msg: %s\t send success, send %d bytes!\n", buf, n);
}
sleep(1);
}
int cyf_socketServer()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in client_address;
int n;
char pipe_buf[16];
server_sockfd = make_socket(SOCK_STREAM,8888);
/* 创建连接队列,忽略子进程终止信号*/
listen(server_sockfd, 5);
//siganl(SIGCHLD, SIG_IGN);
while(1)
{
printf("server waiting\n");
/*接收连接 */
client_len = sizeof (client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address,&client_len);
//创建管道,
if(pipe(mypipe))
{
printf("Pipe failed\n");
}
/*为这个客户建立一个子进程*/
if( fork() == 0 )
{
close(mypipe[0]); //子进程关闭管道输入端(需要些写管道)
close(server_sockfd);
doit(client_sockfd);
close(client_sockfd);
close(mypipe[1]); //关闭写管道
exit(0);
}
else //这里是父进程
{
close(mypipe[1]); //父进程关闭管道输出端(需要读操作)
n=read(mypipe[0],pipe_buf,16);
close(mypipe[0]);
unsigned short m_left;
unsigned short m_right;
unsigned short m_top;
unsigned short m_bottom;
m_left =*((unsigned short*)(pipe_buf+2));
m_right = *((unsigned short*)(pipe_buf+4));
m_top = *((unsigned short*)(pipe_buf+6));
m_bottom = *((unsigned short*)(pipe_buf+8));
stDetectArea2.u16Left = m_left;
stDetectArea2.u16Right = m_right;
stDetectArea2.u16Top = m_top;
stDetectArea2.u16Bottom = m_bottom;
printf("m_left is %d m_top is %d m_right is %d m_bottom is %d \n", m_left,m_top,m_right,m_bottom);
close(client_sockfd);
}
}
return 1;
}
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */