- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的项目是关于以混杂模式嗅探来自以太网的数据。即客户端接收数据包并将其保存在名为“sniff_data.bin”的二进制文件中。并将其发送到服务器。服务器然后处理它(区分 tcp、udp、icmp)。我已经实现了这个,但问题是客户端发送的文件大小与服务器接收到的文件大小不匹配。我的意思是说我给了 10 个计数来嗅探数据包最多 10 个计数。但在服务器端只显示 3 个数据包。有人能帮我解决为什么会出现这个问题吗?我的客户端代码是:
int main( int argc,char *argv[])
{
int infosockfd,cont,cont2;
int len,fh;
struct sockaddr_in address;
int result;
int buffsize=1024;
char buffer[1024];
char *fname = "/home/shishira/Desktop/packet_capture/sniff_data.bin";
/* Create a socket for the client. */
if((infosockfd = socket(AF_INET,SOCK_STREAM,0))>0)
printf("\n -------------------------Information Agent-------------------------\n");
printf("\n Socket was created\n");
/* Name the socket, as agreed with the server. */
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = ntohs(9734);
len = sizeof(address);
data_capture(); //program included for capturing the data from ethernet
printf("\n 'sniff_data' binary file has been created\n");
/* Create a socket for the client. */
if((infosockfd = socket(AF_INET,SOCK_STREAM,0))>0)
printf("\n -------------------------Information Agent-------------------------\n");
printf("\n Socket was created\n");
/* Name the socket, as agreed with the server. */
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = ntohs(9734);
len = sizeof(address);
data_capture();
printf("\n 'sniff_data' binary file has been created\n");
/* Now connect the socket to the task_agents socket. */
if((result = connect(infosockfd, (struct sockaddr *)&address, len))==0)
printf("\n Connecting to the Task agent\n");
if(result == -1)
{
perror("Error in connection\n");
exit(1);
}
fh = open(fname , O_RDONLY);
if(fh==-1)
{
perror("sniff_data File not opened!!\n");
return(1);
}
int total=0;
// int fff=0;
do
{
cont=read(fh, buffer, buffsize);
total=total+cont; //this is used to debug
printf(" data read=%d\n",total);
cont2=write(infosockfd,buffer,cont);
}
while (cont>0);
close(fh);
printf("\n Information agent has sent 'sniff_data' binary file to the Task agent\n\n");
close(infosockfd);
exit(0);
}
以下是终端中的输出,其中 info agent 是客户端,task agent 是服务器。客户端收到 11 个数据包,因为我给了 count=10;但是当服务器接收并处理它时它只收到 3 个数据包?为什么。我觉得从二进制文件中读取数据有问题?是吗?如果是这样如何解决它。?请有人指导我
-------------------------Information Agent-------------------------
Socket was created
Entered Promiscuous Mode Successfully
Client Receiving the Packets...
total recieved packets are 156
total recieved packets are 305
total recieved packets are 367
total recieved packets are 459
total recieved packets are 640
total recieved packets are 807
total recieved packets are 972
total recieved packets are 1151
total recieved packets are 1237
total recieved packets are 1323
total recieved packets are 1409
Done
'sniff_data' binary file has been created
Connecting to the Task agent
data read=1024
data read=1409
data read=1409
Information agent has sent 'sniff_data' binary file to the Task agent
---------------------------Task Agent---------------------------
Socket was created
Task agent waiting...
Information agent is connected
Starting..
TCP : 0 UDP : 0 ICMP : 0 Others : 3 Total : 3
Finished
Task agent processed the contents and saved it in 'info_agent_report' file
最佳答案
TCP 是流协议(protocol),而不是消息协议(protocol)。这意味着无论您在套接字上调用send(2)
(或等效的write(2)
)的次数如何,也无论传递的缓冲区大小如何对于这些调用,线路上的数据在语义上只是一个连续的字节流。消息之间没有界限。
因此,当接收方读取 TCP 流时,它只会看到相同的字节流。无法保证每次调用 recv(2)
(或等同于 read(2)
)都会准确读取通过调用 send( 2)
。您可能会收到一条消息的一半,消息的一部分是碎片化的,或者您可能会在一次调用中收到多条消息。你永远无法确定。
如果你想在 TCP 之上有一个基于消息的协议(protocol),你需要自己构建那个层。一种非常简单的方法是在每条消息前加上长度。这样,接收方就知道每条消息何时结束以及下一条消息从何处开始。还有其他更复杂的方案。
关于c - 为什么使用 C 套接字程序发送和接收时文件大小不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20015005/
我有 table 像这样 -------------------------------------------- id size title priority
我的应用在不同的 Activity (4 个 Activity )中仅包含横幅广告。所以我的疑问是, 我可以对所有横幅广告使用一个广告单元 ID 吗? 或者 每个 Activity 使用不同的广告单元
我有任意(但统一)数字列表的任意列表。 (它们是 n 空间中 bin 的边界坐标,我想绘制其角,但这并不重要。)我想生成所有可能组合的列表。所以:[[1,2], [3,4],[5,6]] 产生 [[1
我刚刚在学校开始学习 Java,正在尝试自定义控件和图形。我目前正在研究图案锁,一开始一切都很好,但突然间它绘制不正确。我确实更改了一些代码,但是当我看到错误时,我立即将其更改回来(撤消,ftw),但
在获取 Distinct 的 Count 时,我在使用 Group By With Rollup 时遇到了一个小问题。 问题是 Rollup 摘要只是所有分组中 Distinct 值的总数,而不是所有
这不起作用: select count(distinct colA, colB) from mytable 我知道我可以通过双选来简单地解决这个问题。 select count(*) from (
这个问题在这里已经有了答案: JavaScript regex whitespace characters (5 个回答) 2年前关闭。 你能解释一下为什么我会得到 false比较 text ===
这个问题已经有答案了: 奥 git _a (56 个回答) 已关闭 9 年前。 我被要求用 Javascript 编写一个函数 sortByFoo 来正确响应此测试: // Does not cras
所以,我不得不说,SQL 是迄今为止我作为开发人员最薄弱的一面。也许我想要完成的事情很简单。我有这样的东西(这不是真正的模型,但为了使其易于理解而不浪费太多时间解释它,我想出了一个完全模仿我必须使用的
这个问题在这里已经有了答案: How does the "this" keyword work? (22 个回答) 3年前关闭。 简而言之:为什么在使用 Objects 时,直接调用的函数和通过引用传
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: what is the difference between (.) dot operator and (-
我真的不明白这里发生了什么但是: 当我这样做时: colorIndex += len - stopPos; for(int m = 0; m < len - stopPos; m++) { c
思考 MySQL 中的 Group By 函数的最佳方式是什么? 我正在编写一个 MySQL 查询,通过 ODBC 连接在 Excel 的数据透视表中提取数据,以便用户可以轻松访问数据。 例如,我有:
我想要的SQL是这样的: SELECT week_no, type, SELECT count(distinct user_id) FROM group WHERE pts > 0 FROM bas
商店表: +--+-------+--------+ |id|name |date | +--+-------+--------+ |1 |x |Ma
对于 chrome 和 ff,当涉及到可怕的 ie 时,这个脚本工作完美。有问题 function getY(oElement) { var curtop = 0; if (oElem
我现在无法提供代码,因为我目前正在脑海中研究这个想法并在互联网上四处乱逛。 我了解了进程间通信和使用共享内存在进程之间共享数据(特别是结构)。 但是,在对保存在不同 .c 文件中的程序使用 fork(
我想在用户集合中使用不同的功能。在 mongo shell 中,我可以像下面这样使用: db.users.distinct("name"); 其中名称是用于区分的集合字段。 同样我想要,在 C
List nastava_izvjestaj = new List(); var data_context = new DataEvidencijaDataContext();
我的 Rails 应用程序中有 Ransack 搜索和 Foundation,本地 css 渲染正常,而生产中的同一个应用程序有一个怪癖: 应用程序中的其他内容完全相同。 我在 Chrome 和 Sa
我是一名优秀的程序员,十分优秀!