Recv and RecvFrom Socket APIs

HiWe’re writing a TCP based Socket application. The following is the pseudo code: open socket bind listen accept while(1) { recv printf (“data received”) } we use setsocketopt to configure the socket to be blocking. Results ------- We used a simple PC sock application to open the socket connection. We’re able to get right down to the recv function… the system “appears” to block @ recv. We then send 10 bytes of data using the PC Sock application… we get the “data received” message printed out. However, our assumption is that the code should go back to recv and wait for the next chunk of data, but it doesn’t it simply continues in the while loop. Question -------- a. Can the recv function be used to “wait” or suspend the thread until data is received? b. After receiving data, do we need to do anything to avoid recv from simply “passing through”? Thanks in advance.

Recv and RecvFrom are normally not blocking. To enable blocking use the setsockopt. For example: setsockopt(SockID, SOL_SOCKET, SO_BIO, (char *)&Value, sizeof(int)); This will set the SockID socket to be blocking. Value is an integer with the value 1.

Thanks Jesper , It solves my problem. However, i still encounter another problem regarding the server & client communication. I have a NS7520 which is my socket server, and another standard pc act as the client. The client will initiate the connection by the following psuedo code s = socket bind connect while the server will do the following socket bind listen accept So far so good. the connection is up. However, my application required the server to send 1 bytes over, by calling send However, the client was keep being blocked at the loop recv(s…,…) Understand that in normal convention, the client will initiate the connection then send out data. I have tested this and the result is pretty statisfatory. However, my question is, can the client activate the connection then put into recv mode? At the mean time the server will send the data immediate right after the accept cheers fong huat

client should always send data to the server first

hi Den not necessary right? now i can send the data from server directly right after the initialization of the socket connection. regards, fong huat