Hello Everyone,
I wanted to create a HTTP client which can send request to and receive response from server using TWR-K60 and
TWR-SER2 tower boards with MQX RTOS 4.2.
I found this thread: TCP / IP Socket Client Issue (RTCS bug?)
Based on the reference code attached with the above thread, i tried to setup the HTTP client and it worked for me.
-----------
But I would like to know, can I perform send() and receive() for multiple time by opening Socket connection only once?
(open connection only once, perform request/response multiple time, close the connection at the end)
I tried to debug the code and observed the TCP connection state (tcb->state) as follow:
bind() --> connect()
[ tcb->state = 1(i.e. SYN_SENT) after successful ACK, tcb->state = 3(i.e. ESTABLISHMENT)]
Send request -- send() [tcb->state remains 3]
Receive response -- recv() [After receiving response tcb ->state changed to 6 (i.e. CLOSE_WAIT)]
Now, if I try to send another request, its unsuccessful, because server is in CLOSE_WAIT state.
Is there any API to keep server in ESTABLISHMENT state until Client closes the connection?
Your help will be really appreciated.
Thanks!!
Hi Priyab
Yes, TCP socket should work as you described, (open connection only once, perform request/response multiple time, close the connection at the end). You can debug with MQX TAD.
Can you attach your modified code (server and client) for further analyzing?
Regards
Daniel
Hello Daniel,
I would like to mention that I am working with cloud based server(project specific), so i haven't done any server implementation.
Attached is the http client code with request and response Log.
I have few more questions regarding the recv() function behavior:
1. recv() returns value '0' for initial few seconds and later it returns the actual response content length. I added socket option 'OPT_RECEIVE_TIMEOUT' with value = 10000.
Do i need to program any other socket options as well?
To come over this issue, I added recv() function in while loop till the returned value is '0'.
2. After reading response for 1st time, if i try to read the response again, I am getting return value(tmpres) = '-1' and error code as 0x1638 (i.e. connection closing), but I am expecting to get value = '0' (i.e. no more data to be read). Why is it so?
(Below is the snippet of recv() function)
----
memset(recvBuf, 0, sizeof(recvBuf) );
while ((tmpres = recv(sockets.sock4, recvBuf, sizeof(recvBuf), 0)) == 0);
do
{
numBytes += tmpres;
recvBuf[tmpres] = 0; /* for test assuming receiving a html page, this can be printed */
printf("%s\n",recvBuf);
printf("Packet received of %d Bytes Total Bytes Received %d\n",tmpres,numBytes);
tmpres = recv(sockets.sock4, recvBuf, sizeof(recvBuf), 0);
}while(tmpres > 0);
----
Please let me know, how can I overcome these issues..
Thanks,
Priya