recv() apparently ignoring part of a TCP Stream

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

recv() apparently ignoring part of a TCP Stream

1,165 Views
clivestokes
Contributor I

MQX 3.8 recv()

I have a requirement to read a file using TCP

My TCP Client does the following:

Creates a socket

Sets socket option: OPT_RECEIVE_NOWAIT TRUE

Binds client to socket

Connects socket to Host

Sends the string "Get /fs/title.htm HTTP/1.1\r\n\

Host: 172.18.11.137\r\n\r\n"

Uses the following code to retrieve the response:

#define MAX_SOCKET_WINDOWS_SIZE  1024 *

char sBuffer[MAX_SOCKET_WINDOWS_SIZE]={0};

_time_delay(30);

memset(sBuffer, 0, sizeof(sBuffer));

bytesReceived = recv(sockfd, sBuffer, 1024, 0);

printf("bytes received: %i\n",bytesReceived);

printf("Received \"%s\".\n",sBuffer);

while ((bytesReceived > 0) && (bytesReceived != RTCS_ERROR) )

{

      memset(sBuffer, 0, sizeof(sBuffer));

      bytesReceived = recv(sockfd, sBuffer, 1024, 0);

      printf("bytes received: %i\n",bytesReceived);

      if (bytesReceived > 0)

      {

            printf("Received \"%s\".\n",sBuffer);

      }

}

Monitoring the transaction using Wireshark I get the following:

GET /fs/title.htm HTTP/1.1

Host: 172.18.11.137

HTTP/1.1 200 OK

Server: WindWeb/2.0

Connection: close

Content-Type: text/html

WWW-Authenticate: Basic realm="MPFR"

Content-Length: 2311

Date: MON JAN 21 16:00:00 2013

Last-Modified: MON JAN 21 16:00:00 2013

<<  the body of title.htm >>>

But my TCP Client only gives:

bytes received: 13

Received "HTTP/1.1 200 ".

bytes received: 0

The header and file from “OK” onwards seem to be ignored.

If  I change the HTTP command by sending the string: "HEAD /fs/title.htm HTTP/1.1\r\n\

Host: 172.18.11.137\r\n\r\n"

I get on Wireshark:

HEAD /fs/title.htm HTTP/1.1

Host: 172.18.11.137

HTTP/1.1 501 Not Implemented

Server: WindWeb/2.0

Connection: close

WWW-Authenticate: Basic realm="MPFR"

Content-Type: text/html

And from my client:

bytes received: 135

Received "HTTP/1.1 501 Not Implemented

Server: WindWeb/2.0

Connection: close

WWW-Authenticate: Basic realm="MPFR"

Content-Type: text/html

".

bytes received: -1

Received "".

Error recv() failed error code 1638

Which I would expect (presumably the server does not implement HEAD).


In both cases my server is responding as I would expect to the command sent but when using the GET command recv() does not capture all the data from the server.

Labels (1)
Tags (2)
0 Kudos
Reply
1 Reply

679 Views
Martin_
NXP Employee
NXP Employee

Seems like incorrect usage of recv() function when NOWAIT option is set. In this case, recv() function does not block waiting for , it gets the bytes that are in the receive buffer and returns this number of bytes immediately.

So I think you should call recv() in a while loop until you detect end of HTTP response in the received data. When NOWAIT is TRUE, recv() can return zero.

0 Kudos
Reply