Beginner
Beginner

Reputation: 286

why is the size of the file differs while sending and receiving using c socket program?

My project is about sniffing the data from Ethernet in promiscuous mode. That is Client receives the packets and saves it in a binary file called "sniff_data.bin". and sends it to the server. The server then processes it(distinguishes between tcp,udp,icmp). I have implemented this but the problem is the size of the file which has been sent by client does not match with the file received in the server. I mean to say i have given count of 10 to sniff the packets upto 10 count. but in the server side only 3 packets are being displayed.can anybody help me out why is this problem arising? My client code is:

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);
}

The following is the output in the terminal where info agent is the client and task agent is server. client receives 11 packets because i have given count=10; but when server receives and processes it it receives only 3 packets? why is it. I feel there is problem in reading the data from the binary file? is it? if so how to solve it .? please somebody guide me

 -------------------------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

Upvotes: 1

Views: 115

Answers (1)

Adam Rosenfield
Adam Rosenfield

Reputation: 400146

TCP is a stream protocol, not a message protocol. What that means is that regardless of the number of times you call send(2) (or equivalently write(2)) on the socket, and regardless of the buffer sizes passed to those calls, the data on the wire is semantically just a single continuous stream of bytes. There are no boundaries between messages.

Therefore, when the receiver reads a TCP stream, it only sees that same stream of bytes. There's no guarantee that each call to recv(2) (or equivalently read(2)) will read exactly one message that was sent with a call to send(2). You might get half of a message, of the message was fragmented, or you may get multiple messages in a single call. You can never be sure.

If you want to have a message-based protocol on top of TCP, you need to build in that layer yourself. One very simple way to do that is to prefix each message with its length. That way, the receiver knows when each message ends and where the next begins. There are other, more complicated schemes as well.

Upvotes: 2

Related Questions