socketpair理解
来源:互联网 发布:网络教育 入学考试 编辑:程序博客网 时间:2024/06/10 18:51
- #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
- [leetcode]14. Longest Common Prefix
- Android判断当前系统使用的语言
- [leetcode] 422. Valid Word Square 解题报告
- 复习iOS动画-Transaction
- android jni学习
- socketpair理解
- 函数
- php中inclued、include_once和require、require_once
- 获取系统版本
- Oracle如何把数据库表迁移到指定表空间
- Java之CountDownLatch使用
- Hadoop的单机模式、伪分布式模式和完全分布式模式
- linux: 几个常用makefile模板
- C++ multimap的简单使用