socketpair初识
2015-06-01 15:20
483 查看
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <error.h>
#include <errno.h>
#include <sys/socket.h>
#include <stdlib.h>
#define BUF_SIZE 300
int main()
{
int s[2];
char *sBuffer = (char *)malloc(BUF_SIZE);
pid_t pid;
char sContent[200];
socketpair(AF_UNIX,SOCK_STREAM,0,s);
pid = fork();
if(pid>0)
{
close(s[1]);
printf("master的使用socket是%d,关掉是是%d\n",s[0],s[1]);
sprintf(sContent,"worker%d!开门!查水表!!!我是master%d",pid,getpid());
write(s[0],sContent,strlen(sContent));
sleep(1);
read(s[0], sBuffer , BUF_SIZE );
printf("master%d听到:%s\n",getpid(),sBuffer);
}
else
{
close(s[0]);
printf("worker的使用socket是%d,关掉是是%d\n",s[1],s[0]);
read(s[1], sBuffer , BUF_SIZE );
printf("worker%d听到:%s\n",getpid(),sBuffer);
sprintf(sContent,"master%d!我家水表在外面!!!我是worker%d",getppid(),getpid());
write(s[1],sContent,strlen(sContent));
}
return 0;
}
收藏于 2012-08-09
来自于百度空间
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <error.h>
#include <errno.h>
#include <sys/socket.h>
#include <stdlib.h>
#define BUF_SIZE 300
int main()
{
int s[2];
char *sBuffer = (char *)malloc(BUF_SIZE);
pid_t pid;
char sContent[200];
socketpair(AF_UNIX,SOCK_STREAM,0,s);
pid = fork();
if(pid>0)
{
close(s[1]);
printf("master的使用socket是%d,关掉是是%d\n",s[0],s[1]);
sprintf(sContent,"worker%d!开门!查水表!!!我是master%d",pid,getpid());
write(s[0],sContent,strlen(sContent));
sleep(1);
read(s[0], sBuffer , BUF_SIZE );
printf("master%d听到:%s\n",getpid(),sBuffer);
}
else
{
close(s[0]);
printf("worker的使用socket是%d,关掉是是%d\n",s[1],s[0]);
read(s[1], sBuffer , BUF_SIZE );
printf("worker%d听到:%s\n",getpid(),sBuffer);
sprintf(sContent,"master%d!我家水表在外面!!!我是worker%d",getppid(),getpid());
write(s[1],sContent,strlen(sContent));
}
return 0;
}
收藏于 2012-08-09
来自于百度空间
相关文章推荐
- LeetCode Container With Most Water
- http://blog.csdn.net/liuhe688/article/details/6715983/
- YT02-简单数学课堂题-1004 Fibonacci Again -(5.31日-烟台大学ACM预备队解题报告)
- Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的权利字符串" UserInfo=0x1bad30 {NSLocalizedDescription=未找到应用程序的“aps-environment”的权利字符串}
- Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER 解决方案
- HackerRank - "Detecting Valid Latitude and Longitude Pairs"
- http error 503. the service is unavailable. sharepoint
- Leetcode Problem.172—Factorial Trailing Zeroes
- AIX下的nmon的使用
- 在可以调用OLE之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute
- shrio 权限管理filterChainDefinitions过滤器配置
- httpd: Could not reliably determine the server's fully qualified domain name
- leetcode_Contains Duplicate II_easy
- EAI和ESB
- leetcode_Contains Duplicate_easy
- AEAI CRM客户关系管理V1.0.3版发版说明
- 【译】使用Rails 4.2+ 测试异步邮件系统
- CodeForces 538E Demiurges Play Again(博弈dp)
- mail 命令指定smtp服务器发送邮件
- hdu 1022 Train Problem I (栈的应用)