ronex dicapriyo
ronex dicapriyo

Reputation: 161

recv function for winsock socket

I have a server application which is connected with telnet client(i.e. telnet localhost _port_num - here port number is same associated with the server application),

My application works correctly, but the thing is I used recv as follows:

#define BUFLEN 512
char buf[BUFLEN];
iResult = recv(sd, (char *)buf, BUFLEN, 0);

here recv call returns as soon as any character pressed over the connected telnet terminal, and most of the time iResult is 1 or some times 2, Even though I wouldn't press enter telnet client sends frame containing a single character to the server application.

How can I make sure that recv should return after BUFLEN read ? In case of linux recv works as expected, get blocks until enter.

Any help or pointers are greatly appreciated.

Upvotes: 0

Views: 2494

Answers (2)

Tayyab
Tayyab

Reputation: 10631

You need to call recv function again and again until your desired amount of data is received. Please note that when you use TCP Sockets, you cannot make sure if you receive all data in single receive call. If you send data using single TCP Send() call, then it is fairly possible that you receive it in multiple receives as TCP sockets are Stream Sockets.

The recv() function returns the number of bytes received, so you can keep calling the function until you get all they bytes.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121649

Q: How can I make sure that ... BUFLEN read ?

A: You read in a loop until you get all the characters you expect. Or until you get a timeout, or an error.

Upvotes: 2

Related Questions