Incomplete socket recv()

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:

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?
 
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?
 
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.
 
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.
 
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.
 
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?
 
Back
Top