OOB套接字传输实例(达不到预期结果)

参考资料:<<Linux网络编程.pdf>>page119-205

代码本来是全照书上抄的,后来发现编译不成功,所以就稍微改了下。下面是我修改后的代码:

server.c

// OOB套接字传输服务端(Server.c)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <fcntl.h>
#define MYPORT 4000
#define BACKLOG 10
int new_fd;
void sig_urg(int signo)
{
int n;
char buf[100];
printf("SIGURG received\n");
n = recv(new_fd, buf, sizeof(buf)-1, MSG_OOB);
buf[n] = 0;
printf("recv %d OOB bytes: %s\n", n, buf);
}
int main(int argc, char *argv[])
{
int sock_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size, n;
char buf[100];
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return 1;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;	// 自动设置为自己的IP
bzero(&(my_addr.sin_zero), 8);			// 将结构的其余空间清零
if(bind(sock_fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) == -1)
{
perror("bind");
return 1;
}
if(listen(sock_fd, BACKLOG) == -1)
{
perror("listen");
return 1;
}
void *old_sig_urg_handle;				// 保存系统默认的信号处理函数
if((old_sig_urg_handle = signal(SIGURG, sig_urg)) == SIG_ERR)
{
perror("signal");
return 1;
}
fcntl(sock_fd, F_SETOWN, getpid());
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((new_fd = accept(sock_fd, (struct sockaddr*)&their_addr, &sin_size)) == -1)
{
perror("accept");
continue;
}
printf("server: got connection from [%s]\n", inet_ntoa(their_addr.sin_addr));
if(!fork())
{
while(1)
{
if((n = recv(new_fd, buf, sizeof(buf)-1, 0)) <= 0)		// 接收正常数据
{
printf("recv EOF\n");
break;
}
buf[n] = 0;
printf("Recv: %d bytes: %s\n", n, buf);
close(new_fd);
}
exit(0);
}
}
while(waitpid(-1, NULL, WNOHANG) > 0);
signal(SIGURG, old_sig_urg_handle);		// 恢复系统默认的信号处理函数
return 0;
}


Client.c

// OOB套接字传输客户端(Client.c)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define MYPORT 4000
#define MAXDATASIZE 100
int main(int argc, char *argv[])
{
int sock_fd;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr;
if(argc < 2)
{
fprintf(stderr, "Usage: %s <hostname>\n", argv[0]);
return 1;
}
if((he = gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
return 1;
}
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
return 1;
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(MYPORT);
their_addr.sin_addr = *((struct in_addr*)he->h_addr);
bzero(&(their_addr.sin_zero), 8);
if(connect(sock_fd, (struct sockaddr*)&their_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect");
return 1;
}
if(send(sock_fd, "123", 3, 0) == -1)
{
perror("send");
close(sock_fd);
return 0;
}
printf("Send 3 bytes of normal data\n");
sleep(1);
if(send(sock_fd, "4", 1, MSG_OOB) == -1)
{
perror("send");
close(sock_fd);
return 0;
}
printf("Send 1 byte of OOB data\n");
sleep(1);
if(send(sock_fd, "56", 2, 0) == -1)
{
perror("send");
close(sock_fd);
return 0;
}
printf("Send 2 bytes of normal data\n");
sleep(1);
if(send(sock_fd, "7", 1, MSG_OOB) == -1)
{
perror("send");
close(sock_fd);
return 0;
}
printf("Send 1 byte of OOB data\n");
sleep(1);
if(send(sock_fd, "89", 2, MSG_OOB) == -1)
{
perror("send");
close(sock_fd);
return 0;
}
printf("Send 2 bytes of OOB data\n");
sleep(1);
close(sock_fd);
return 0;
}


运行结果:

1. Server端:

[zcm@socket #39]$./myserver 
server: got connection from [127.0.0.1]
Recv: 3 bytes: 123
recv EOF

2. 客户端:

[zcm@socket #25]$./myclient localhost
Send 3 bytes of normal data
Send 1 byte of OOB data
Send 2 bytes of normal data
Send 1 byte of OOB data
Send 2 bytes of OOB data
[zcm@socket #26]$

 

现在的疑问是:我感觉signal函数虽然调用成功了,但是没起到作用。我认为正常情况下,sig_urg()应该会被调用,但结果是好像他没有收到SIGURG信号,所以没有调用sig_urg这个函数。

 

若有高人能解决,小弟万分感激!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407262.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sql,dateadd,datediff

本文转载自MSDN&#xff0c;为了方便访问&#xff0c;谢谢&#xff01;DATEADD (Transact-SQL)http://msdn.microsoft.com/zh-cn/library/ms186819.aspx将指定 number 时间间隔&#xff08;有符号整数&#xff09;与指定 date 的指定 datepart 相加后&#xff0c;返回该 date。…

公众号H5 VUE获取CODE

回调地址设置页面向导&#xff1a;开发>接口权限>网页服务>网页授权>修改。开发的项目需要放到已经解析好服务器域名的服务器下&#xff0c;同时把Mp***.text文件放到服务器根目录下&#xff0c;此时你的服务器必须能联通外网也就是有公网IP,并且80端口是打开的&am…

javascript学习系列(18):数组中的include方法

最好的种树是十年前,其次是现在。歌谣 每天一个前端小知识 提醒你改好好学习了 知乎博主 csdn博主 b站博主 放弃很容易但是坚持一定很酷 我是歌谣 喜欢就一键三连咯 你得点赞是对歌谣最大的鼓励 1前言 在我们的日常开发中 不免会有很多需要处理数据的方法 本节主要说一说…

curPos和tgtPos

curpos tgtpos 乍一看以为是当前位置和目标位置&#xff0c;但在项目里面这两个位置有点坑 当客户端玩家移动或者AI里面的位置&#xff0c;会把获得的位置付给tgtpos 而以前的tgtpos会付给curpos 所以这个tgtpos是当前玩家或者怪物站立的位置&#xff0c;而curpos是上一个位置 …

JS之获取指定位置Unicode的charCodeAt()方法

用法&#xff1a;charCodeAt() 方法可返回指定位置的字符的 Unicode 编码。这个返回值是 0 - 65535 之间的整数 语法&#xff1a;stringObject.charCodeAt(index) 参数&#xff1a;必需。表示字符串中某个位置的数字&#xff0c;即字符在字符串中的下标 注:1&#xff1a;方法…

利用ioctl获取本机指定设备的MAC地址

// 利用ioctl获取本机指定设备的MAC地址#include<stdio.h>#include<string.h>#include<stdlib.h>#include<errno.h>#include<unistd.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/i…

日本語趣味読み 一 星とり

星とり ある夜のこと、お寺の庭で、小僧さんが、長い竹竿を、あっちこっち振り回りしておりました。 和尚さんがこれを見つけて、 「これこれ、そこで何をしているのじゃ。」 と聞きますと、小僧さんは、 「お空の星がほしくって、打ち落とそうとしているのでございますが、 ひ…

usaco-sprime-superprime-pass

这个题目开始真正用C&#xff0b;&#xff0b;了&#xff0c;因为&#xff0c;数组的分配有限制了&#xff0c;只好用c中的vector: /* ID: qq104801 LANG: C TASK: sprime */#include <iostream> #include <cstdio> #include <string> #include <cstring&…

vue-router使用next()跳转到指定路径时会无限循环

解释1 beforeRouteLeave (to, from, next) {console.log(离开路路由)if(to.fullPath/home){next();}else{next(/home)}这个是组件路由&#xff0c;我想实现的效果是在这个页面点击浏览器的返回按钮后要返回 /home页面而不是上一个页面&#xff0c;上面的代码是没问题的&#x…

JS之按照Unicode返回指定字符串

用法&#xff1a;fromCharCode() 可接受一个指定的 Unicode 值&#xff0c;然后返回一个字符串 语法&#xff1a;String.fromCharCode(numX,numX,…,numX) 参数&#xff1a;必需。一个或多个 Unicode 值&#xff0c;即要创建的字符串中的字符的 Unicode 编码 注意&#xff1…

顺序容器STL::list用法

C Code: // 顺序容器STL::list用法#include<iostream>#include <stdio.h>#include <stdlib.h>#include <list>using namespace std;void appendItems(list<int> &li, int n){for(int i 0; i < n; i){li.push_back(i1);}}void appendIte…

搭建自己的base.js(2)-其他事件方法

获取鼠标按键 // 获取鼠标按键,getButton:function(event) {//DOM,先检测是否支持DOM鼠标事件if(document,implementation.hasFeature("MouseEvents","2.0")) {return event.button; //0主键&#xff0c;1滚轮&#xff0c;2次键} else { //IE8及之前switc…

Linux下的压缩zip,解压缩unzip命令详解及实例

zip all.zip *.jpg #将所有.jpg的文件压缩成一个zip包unzip all.zip #将all.zip中的所有文件解压到当前目录中unzip all.zip -d all #将all.zip 中的所有文件解压到当前目录中的all文件夹中zip -r hy.zip hy #将当前目录下的hy文件夹压缩为hy.zipzip -r hy.zip hy 123.tx…

JSP标签中不要省略引号

<th> 输入po:<input name"po" value"<%po%>"/></th> <th>输入ip:<input name"ip" value"<%ip%>"/></th> 打算出来的界面如图 这个里面如果&#xff0c;"<%ip%>" 不…

JS之返回字符首次出现位置的indexOf

作用&#xff1a;indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置 语法&#xff1a;stringObject.indexOf(searchvalue,fromindex) 参数1&#xff1a;必需。规定需检索的字符串值 参数2&#xff1a;可选的整数参数。规定在字符串中开始检索的位置。它的合法…

vsftpd默认用户名/密码

我的Linux系统是Fedora12&#xff0c;在它上面安装vsftpd的步骤是&#xff1a; yum install vsftpd 安装好后&#xff0c;要启动vsftpd服务&#xff1a;service vsftpd start 停止服务&#xff1a;service vsftpd stop 重启服务&#xff1a;service vsftpd restart 查看状态…

怎样设计接口?

怎样设计接口&#xff1f; 众所周知&#xff0c;接口是提供给其它模块或者系统使用的一种约定或者规范。因此接口必需要保证足够的稳定性和易用性。这是设计接口的基本要求。 1.稳定性 接口必须相对稳定&#xff0c;否则将导致接口的使用者和提供者为了适应新接口而不断改动接口…

软件工程心理学之9----乙方如何面对甲方2

(本文同步发布在javaeye.com上,转载请保留出处) 在http://www.cnblogs.com/jackyrong/archive/2006/11/08/554694.html中, 大概谈了下如何应对甲方的领导。这次要谈的就是很重要的如何应对甲方的信息项目的负责人了。这其实也是相当重要的一部分&#xff0c;在打通了和甲方领导…