前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >socketpair的使用

socketpair的使用

作者头像
全栈程序员站长
发布2022-07-14 15:24:52
4950
发布2022-07-14 15:24:52
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

socketpair函数概要例如以下: #include <sys/types.h> #include <sys/socket.h> int socketpair(int domain, int type, int protocol, int sv[2]); sys/types.h文件须要用来定义一些C宏常量。sys/socket.h文件必须包括进来定义socketpair函数原型。 socketpair函数须要四个參数。他们是: 套接口的域 套接口类型 使用的协议 指向存储文件描写叙述符的指针

类型參数声明了我们希望创建哪种类型的套接口。socketpair函数的选择例如以下: SOCK_STREAM SOCK_DGRAM 对于socketpair函数,protocol參数必须提供为0。 參数sv[2]是接收代表两个套接口的整数数组。每个文件描写叙述符代表一个套接口,而且与还有一个并没有差别。 假设函数成功,将会返回0值。否则将会返回-1表明创建失败,而且errno来表明特定的错误号。

关于流程。socketpair()函数创建出两个进程,fork()之后这两个进程都会运行主程序中的代码,这个一定要注意!尤其是bind的时候,假设bind两次的话,那就会出错了。通常会在子进程里调用一个带死循环的函数,这样就好了。(这个情况的样例会在综合运用中解说)

一下给出个简单的样例。

// 建立socket对 #include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <stdio.h> int main () { int fd[2]; int r = socketpair( AF_UNIX, SOCK_STREAM, 0, fd ); if ( r < 0 ) { perror( “socketpair()” ); exit( 1 ); } if ( fork() ) { /* Parent process: echo client */ int val = 0; close( fd[1] ); while ( 1 ) { sleep( 1 ); ++val; printf( “Sending data: %d/n”, val ); write( fd[0], &val, sizeof(val) ); read( fd[0], &val, sizeof(val) ); printf( “Data received: %d/n”, val ); } } else { /* Child process: echo server */ int val; close( fd[0] ); while ( 1 ) { read( fd[1], &val, sizeof(val) ); ++val; write( fd[1], &val, sizeof(val) ); } } }

在给出一个用sendmsg来传递数据的样例

/***************************************** * * Listing 1.2 * * Example performing I/O on s socket pair: * * ******************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h>

int main(int argc,char **argv) { int z; /* Status return code */ int s[2]; /* Pair of sockets */ struct msghdr msg; struct iovec iov[1]; char send_buf[100] = “TEST”; struct msghdr msgr; struct iovec iovr[1]; char recv_buf[100];

/* * Create a pair of local sockets: */ z = socketpair(AF_LOCAL,SOCK_STREAM,0,s);

if(z == -1) { fprintf(stderr, “%s:socketpair(AF_LOCAL,SOCK_STREAM,””0)/n”,strerror(errno)); return 1; /* Failed */ }

/* * Sendmsg s[1]: */

bzero(&msg, sizeof(msg)); msg.msg_name = NULL; /* attention this is a pointer to void* type */ msg.msg_namelen = 0; iov[0].iov_base = send_buf; iov[0].iov_len = sizeof(send_buf); msg.msg_iov = iov; msg.msg_iovlen = 1;

printf(“sendmsg begin./n”); z = sendmsg( s[1], &msg, 0 ); if(z == -1 ) { fprintf(stderr,”Sendmsg failed. errno : %s/n”,strerror(errno)); return -1; } printf(“Sendmsg Success!/n”);

/* * Read from socket s[0]: */

bzero(&msg, sizeof(msg)); msgr.msg_name = NULL; /* attention this is a pointer to void* type */ msgr.msg_namelen = 0; iovr[0].iov_base = &recv_buf; iovr[0].iov_len = sizeof(recv_buf); msgr.msg_iov = iovr; msgr.msg_iovlen = 1;

z = recvmsg( s[0], &msgr, 0); if(z == -1 ) { fprintf(stderr,”Recvmsg failed. errno : %s/n”,strerror(errno)); return -1; } printf(“Recvmsg Success!/n”); printf(“recvmsg : %s/n”, recv_buf);

/* * Close the sockets: */ close(s[0]); close(s[1]);

puts(“Done”); return 0; }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/118044.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年12月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档