Hi,

My webserver code is trying to send an image file over a TCP connection. However, the send() function is returning a -1 value with the error: Connection Reset by peer. I am using a web-browser to access the server so my guess is that the server is somehow sending the RST flag to the client which is causing the termination of connection.

I am only new to socket programming so I was wondering if you could help me understand the RST packet and how it works. How can I program the server side socket to ensure that the connection is kept alive and that the RST flag is not sent.

Thanks heaps!

Recommended Answers

All 5 Replies

Are you having a problem only with image data or even with simple data as well ?
The RST flag is usually raised when a host receives a packet which does not belong to the current connection.

Are you having a problem only with image data or even with simple data as well ?
The RST flag is usually raised when a host receives a packet which does not belong to the current connection.

The text files are being sent fine. I am trying to send a file that has two images embedded in it. The webpage is sent however the send() function returns a -1 value when attempting to the image. The error says: Connect reset by peer. I have even enabled the TCP_Keep_alive option in setsockopt but still no difference :s

I think the problem is that you are trying to send images rather than being a N/W problem.

Maybe this will be of help

I think the problem is that you are trying to send images rather than being a N/W problem.

Maybe this will be of help

But everything else works fine though? Here is the code I am using to send the images:

//copy file into the buffer
			while(!feof(sendFile)){
				bzero(send_buffer,MAX_MSG);
                //result = fread (send_buffer,1,sizeof(send_buffer),sendFile);
				while(result>0){
					result = fread (send_buffer,1,sizeof(send_buffer),sendFile);
					printf("Result value is %d\n",result);
					if(ferror(sendFile)){
						printf("ERROR: %s\n", strerror(errno));
						printf("Error reading file: %s\n",request_page);
					}
					if((test=send(new_fd,send_buffer,sizeof(send_buffer),0))<0){
						printf("ERROR: %s\n", strerror(errno));
				  	    printf("Sending %s Failed\n", request_page);
					    exit(1);
					}
					printf("Sending worked\n");
					bzero(send_buffer,sizeof(send_buffer));
				}
			}
			fclose(sendFile);

What is the value of result ?
Are you sure new_fd is initialized correctly ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.