# Incomplete socket recv()



## telltera (Sep 8, 2010)

Hi All,

I have to receive a byte array of 1690 bytes from a Java system client.
The server is written in C.

I send 1690bytes but receive only 1460 bytes.

I want to know if the data format somewhere should be changed. Do help me.

Here is part of code:


```
//GLOBALS
#define RCVBUFSIZE 3000  /* Size of receive buffer */	
unsigned char * publickey = NULL;

//part of code
	char echoBuffer[RCVBUFSIZE];        /* Buffer for echo string */
	int recvMsgSize;                    /* Size of received message */
	char replyBuffer[32]; 
	strcpy("hi", replyBuffer);
    /* Receive message from client */
    if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
        DieWithError("recv() failed");

    /* Send received string and receive again until end of transmission */
    if (recvMsgSize > 0)      /* zero indicates end of transmission */
    {
	echoBuffer[recvMsgSize] = '\0';        
	printf("Message received from Client is : %s", echoBuffer);
	int indicator =  0;

		
			if (send(clntSocket, replyBuffer, strlen(replyBuffer), 0) < 0)
            printf("send() failed\n");
			/* See if there is more data to receive */
			if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
            printf("recv() failed\n");

			if (recvMsgSize > 0) {
				echoBuffer[recvMsgSize] = '\0';     
				publickey = (unsigned char *)echoBuffer;
				signed long keysize = sizeof(publickey);
				printf("totalbytes recieved = %d\n", recvMsgSize);
				printf("size of key = %d\n", keysize);
				printf("\nSystem sends publickey : \n%s\n", publickey);
				indicator = 3;
				writefilepem (publickey, 3);
				indicator = 0;
				//memset(echoBuffer,0, RCVBUFSIZE);
			}
		}
		
	}

    close(clntSocket);    /* Close client socket */
}
```

OUTPUT:
totalbytes recieved = 1460


Please tell me how to correct this problem?


----------



## SirDice (Sep 8, 2010)

Run something like tcpdump(1) and verify that the client is actually sending 1690 bytes.


----------



## quintessence (Sep 8, 2010)

Hello, 

You can try to send ( and receive ) it through GGBIT ethernet with jumbo frames with MTU ~ 1810 for example and adjust MSS to 1690 



> ```
> MSS = MTU - sizeof(TCPHDR) - sizeof(IPHDR)
> ```


----------



## telltera (Sep 9, 2010)

Thanks.

Ok so the code itself is right?

What about any malloc allocation?

By the way, I ran tcpdump and I got 387 packets captured, 508 received by filter and 78 dropped by kernel.

Can anybody tell me how to interpret this?

How many bytes make a packet?


----------



## SirDice (Sep 9, 2010)

telltera said:
			
		

> Can anybody tell me how to interpret this?


You're joking right? You're programming sockets and you have no idea how TCP/IP works?!?!

I suggest getting "TCP/IP Illustrated", Volume 1 and 2 before going any further.


----------



## expl (Sep 10, 2010)

telltera said:
			
		

> Thanks.
> 
> Ok so the code itself is right?



You called "recvMsgSize = recv()" two times but printed the size only after second call. Is that what you wanted?



			
				telltera said:
			
		

> What about any malloc allocation?



What malloc allocation? I don't see any.



			
				telltera said:
			
		

> By the way, I ran tcpdump and I got 387 packets captured, 508 received by filter and 78 dropped by kernel.
> 
> Can anybody tell me how to interpret this?
> 
> How many bytes make a packet?



Hmmm, might want to read about tcpdump. Its a wonderful tool.


----------



## dh (Sep 12, 2010)

You can't expect to receive ALL the data in one recv() call. You should do a loop loop (and possibly check with select() if socket is ready to be read) and try calling recv() until you get all the data you need from socket.


----------



## telltera (Sep 14, 2010)

Thanks all.

I do have the recv-send-recv sequence in a while loop.

The first recv fetches me an "indicator" value from the client and I acknoledge with the send.

In the second recv I get the 1690 byte array.

It is not always that I get the 1690 bytes. At times after restarting both client and server, I get the full 1690 bytes and at other times I get lesser bytes.

Should I flush out the buffers or something?


----------



## dh (Sep 16, 2010)

I don't see a loop in your code


----------

