Reputation: 21
I'm having a really hard time figuring out how to exit the loop on the receiver side. I have a tcp stream socket connection and I'm sending what I read from a file and putting it in the buffer. The receiver end just keeps looping writing the same info over and over. I'm sure it's because i'm not decrementing nRecv so it never hits the if(nRecv == 0) statement but i can't figure out how to decrement it. I'm posting the while loop for both sender and receiver hopefully someone can point me in the right direction.
sender
/* prepare file to send */
pf = fopen("input.txt", "rb");
if(pf == NULL)
{
printf("The file you want to send was not found");
return(1);
}
else
{
while (!feof(pf))
{
nRead = fread(bufferin, sizeof(char), 256, pf);
if (nRead <= 0)
printf("ERROR reading file");
while (nRead > 0)
{
nSent = send(filesender_socket, bufferin, nRead, 0);
if (nSent < 0)
{
printf("ERROR sending from socket = %d\n", WSAGetLastError());
break;
}
if (nSent == 0)
printf("DISCONNECTED writing to socket");
//pBuf += nSent;
//nRead -= nSent;
}
}
} //end of if statement
// Close all open sockets
#ifdef WIN
//retcode = closesocket(jsender_socket);
retcode = closesocket(filesender_socket);
if (retcode < 0)
{
printf("*** ERROR - closesocket() failed \n");
exit(-1);
} #endif
RECEIVER
//create a new socket for file transfer
filesocket = socket(AF_INET, SOCK_STREAM, 0);
if (filesocket < 0)
{
printf("*** ERROR - socket() failed \n");
exit(-1);
}
// >>> Step #2 <<<
// Fill-in my socket's address information
receiver_addr.sin_family = AF_INET; // Address family to use
receiver_addr.sin_port = htons(PORT_FILE); // Port number to use
receiver_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on any IP address
filebindcode = bind(filesocket, (struct sockaddr *)&receiver_addr,sizeof(receiver_addr));
if (filebindcode < 0)
{
printf("*** ERROR - file socket bind() failed \n");
exit(-1);
}
printf("receiver accepting connections\n");
//2 DEBUG LINES
printf("received a connection from: %s port %d\n",
inet_ntoa(sender_addr.sin_addr), ntohs(sender_addr.sin_port));
if (newsockfd = listen(filesocket, 2) < 0) {
printf("newsock in listen %d\n", newsockfd);
perror("listen failed");
exit(1);
}
//listen(filesocket, 2);
//while(1) //while loop for to accept files
//{
printf("\nwaiting for accept() to complete \n");
newsockfd = accept(filesocket, (struct sockaddr *) &sender_addr, &addr_len);
printf("newsock return %d", newsockfd);
if (newsockfd < 0)
{
printf("*** ERROR - accepting() failed \n");
exit(-1);
}
// start receiving file
fp = fopen("output.txt", "wb");
if (fp == NULL)
{
printf("File not found!\n");
return NULL;
}
else
{
printf("created file output.txt\n");
}
//receive file
while(1)
{
nRecv = recv(newsockfd, bufferin, 256, 0);
if (nRecv < 0)
{
printf("ERROR reading from socket = %d\n", WSAGetLastError());
break;
}
if (nRecv == 0)
break;
while (nRecv > 0)
{
printf("%s", bufferin); // debug
nWritten = fwrite(bufferin, sizeof(char), nRecv, fp);
if (nWritten <= 0)
printf("ERROR writing to file");
//nRecv -= nWritten;
}
}
printf("File Transfer complete\n\n"); //} //end of while
Upvotes: 2
Views: 1394
Reputation: 21
not sure why but now the two lines i had commented out because they weren't working now they work..
uncommented the following lines
pBuf += nSent;
nRead -= nSent;
and these on the reciver side
nRecv -= nWritten;
and it worked like a charm.
Thank you for the help.
Upvotes: 0
Reputation: 2890
The return value of recv()
is as follows:
>0
-- the number of bytes received0
-- no data (async sockets only) or other socket was closed cleanly<0
-- an error occurred.In your sender code, you never close the socket, so your receiver waits for more data.
Upvotes: 2