gpt4 book ai didi

c - 为什么使用 C 套接字程序发送和接收时文件大小不同?

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:14 24 4
gpt4 key购买 nike

我的项目是关于以混杂模式嗅探来自以太网的数据。即客户端接收数据包并将其保存在名为“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com