socketpair理解
来源:互联网 发布:打破刚性兑付 知乎 编辑:程序博客网 时间:2024/06/08 14:04
- #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 30
- int main(){
- int s[2];
- int w,r;
- char * string = "This is a test string";
- char * buf = (char*)calloc(1 , BUF_SIZE);
- if( socketpair(AF_UNIX,SOCK_STREAM,0,s) == -1 ){
- printf("create unnamed socket pair failed:%s\n",strerror(errno) );
- exit(-1);
- }
- /*******test in a single process ********/
- if( ( w = write(s[0] , string , strlen(string) ) ) == -1 ){
- printf("Write socket error:%s\n",strerror(errno));
- exit(-1);
- }
- /*****read*******/
- if( (r = read(s[1], buf , BUF_SIZE )) == -1){
- printf("Read from socket error:%s\n",strerror(errno) );
- exit(-1);
- }
- printf("Read string in same process : %s \n",buf);
- if( (r = read(s[0], buf , BUF_SIZE )) == -1){
- printf("Read from socket s0 error:%s\n",strerror(errno) );
- exit(-1);
- }
- printf("Read from s0 :%s\n",buf);
- printf("Test successed\n");
- exit(0);
- }
- #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 30
- int main(){
- int s[2];
- int w,r;
- char * string = "This is a test string";
- char * buf = (char*)calloc(1 , BUF_SIZE);
- pid_t pid;
- if( socketpair(AF_UNIX,SOCK_STREAM,0,s) == -1 ){
- printf("create unnamed socket pair failed:%s\n",strerror(errno) );
- exit(-1);
- }
- /***********Test : fork but don't close any fd in neither parent nor child process***********/
- if( ( pid = fork() ) > 0 ){
- printf("Parent process's pid is %d\n",getpid());
- close(s[1]);
- if( ( w = write(s[0] , string , strlen(string) ) ) == -1 ){
- printf("Write socket error:%s\n",strerror(errno));
- exit(-1);
- }
- }else if(pid == 0){
- printf("Fork child process successed\n");
- printf("Child process's pid is :%d\n",getpid());
- close(s[0]);
- }else{
- printf("Fork failed:%s\n",strerror(errno));
- exit(-1);
- }
- /*****read***In parent and child****/
- if( (r = read(s[1], buf , BUF_SIZE )) == -1){
- printf("Pid %d read from socket error:%s\n",getpid() , strerror(errno) );
- exit(-1);
- }
- printf("Pid %d read string in same process : %s \n",getpid(),buf);
- printf("Test successed , %d\n",getpid());
- exit(0);
- }
0 0
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair理解
- socketpair的用法和理解
- socketpair
- socketpair
- socketpair
- VS设置代码背景
- Android ViewPager + Fragment懒加载
- MATLAB第二次作业 例1.4-1~2
- 7层网络结构分析
- 人月神话--焦油坑
- socketpair理解
- Lootcode 1. Two Sum The Solution of Python and Javas
- python基础之函数
- shell小白 想玩自动化部署,无密码ssh 登录远程服务器,执行scp命令传送war包 并重启tomcat。
- rman恢复数据库二
- Ajax 传递数组问题
- 如何让Tomcat以Debug模式启动
- 31. Next Permutation
- C++Primer第五版 第十八章习题答案(11~20)