recv returning -1

Hi,

I'm trying to get some data across a TCP network. I printf "buf" in my receive function before "recv" and it shows the data string perfectly.
Then, for some reason, recv hits an error and jumps to "bytes<=0". still, the following printf prints "buf" perfectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int recvline(const int sock, char *buf, const size_t buflen)
{
	int status = 0; // Return status.
	size_t bufleft = buflen;
	printf("recvline START buf:%s\n",buf);
	while (bufleft > 1) {
		printf("recvline WHILE buf:%s\n",buf);
		// Read one byte from scoket.
		ssize_t bytes = recv(sock, buf, 1, 0);
		if (bytes <= 0) {
			// recv() was not successful, so stop. ** LENGTH (in this case 1) WAS RETURNED -1, A.K.A. SOCKET ERROR
			status = -1;
			printf("recvline BYTES<=0 buf:%s\n",buf);
			break;
		} else if (*buf == '\n') {
			printf("recvline EOF buf:%s\n",buf);
			// Found end of line, so stop.
			*buf = 0; // Replace end of line with a null terminator.
			status = 0;
			break;
		} else {
			// Keep going.
			bufleft -= 1;
			buf += 1;
		}
	}
	*buf = 0; // add null terminator in case it's not already there.
	return status;
}


following is output:
1
2
3
4
5
6
recvline START buf:AUTH user xxxq.BM

recvline WHILE buf:AUTH user xxxq.BM

recvline BYTES<=0 buf:AUTH user xxxq.BM


I think this is happening because I used strtok to break whats passed to "buf" (through TCP) to parse and check for errors, thus cancelling the NULL?

Last edited on
Check what errno has been set to.

1
2
3
4
5
6
if (bytes <= 0) 
{
	// recv() was not successful
        // print out the error message corresponding to errno
        perror( "recv() error" ) ; 
        // ... 
thanks for replying! :)

there is bad news, it's recv() error: Connection reset by peer
I read up on it and it's pretty fatal.
thing is, I have no clue how this is happening...

it shouldn't be too bad because I'm not messing with the socket programming part as of yet. just sending and receiving strings!

could it be because I'm not memset-ing my buffer? I tried, but then it messed up the send/receive time by one.

could it be because "buf" does not have NULL?
It should, since my send all on the server (other side) is:
1
2
	sendall(sock, cmd, strlen(cmd));  //Communication Protocol,  client-server.
	sendall(sock, "\n", 1);	


client: sendall passes ==0, but it is stuck on recvline ==-1.
1
2
3
...
if (sendall(sock, buf, strlen(buf)) == 0 && recvline(sock, buf, sizeof buf) == 0)
...


sendall function just in case:
1
2
3
4
5
6
7
8
9
10
11
12
13
int sendall(const int sock, const char *buf, const size_t len)
{
	size_t tosend = len;
	while (tosend > 0) {
		ssize_t bytes = send(sock, buf, tosend, 0);
		if (bytes <= 0) 
			break; // send() was not successful, so stop.
		tosend -= (size_t) bytes;
		buf += bytes;
	};

	return tosend == 0 ? 0 : -1;
}

Last edited on
> could it be because I'm not memset-ing my buffer?
> could it be because "buf" does not have NULL?

No. It has got nothing to do with what your receive buffer contains.

The program at the other end of the socket (from which you had hoped to receive data) has forcibly closed the socket connection. You need to find out why it has done so.
Topic archived. No new replies allowed.