🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Need help w/ recv() in WinSock!!!!!

Started by
6 comments, last by WebSpyder 23 years, 10 months ago
My coworker and I are just learning WinSock and are programming a simple client-server app in C++. The only problem is that we can''t seem to receive data!. We have status messages every step of the way and according to them, both computers connect over a streaming TCP\IP connection. The client sends its info, yet it gets and error message saying that it cannot recieve any info (error 10054). The server says it has lost the connection (error 10057). So we think the receive function on the client is causing something to happen... but we can''t explain why the server disconnects spontaneously. Here is our recieve function on the client and server: // open the port to receive any sent information char buffer2[5]; // our receiving buffer if(recv(localSock, buffer2, sizeof(buffer2), 0) == SOCKET_ERROR) { // on error, print message, cleanup and quit cout << "Socket could not receive info: " << WSAGetLastError () << endl; WSACleanup(); return(0); } here is the sockaddr_in fill-in (for the client): // fill in the sockaddr_in struct target.sin_family = PF_INET; target.sin_port = htons(5000); target.sin_addr.s_addr = inet_addr("169.254.74.122"); Since we can''t find anything wrong with the buffer, we think there''s something with our socket localSock that''s causing the error, but we can''t figure it out. If anyone can help it would be greatly appreciated. Thanx in advance. ****************************** "I do not fear computers, I fear the lack of them" - Isaac Asimov Drew Sikora Napali Networks, Inc.
******************************"I do not fear computers, I fear the lack of them" - Isaac AsimovDrew SikoraNapali Networks, Inc.
Advertisement
How about a little more info... are you calling accept()? Are you using async sockets, or select() to poll the ports?
We are calling accept:

    // upon picking up a request, accept it and connect using the//client socket	int* lenclient;if (client = accept(hSock, client_sock, lenclient)){  // on error, display message, cleanup and quit  cout << "Socket not connected!: " << WSAGetLastError() << endl;  WSACleanup();  return(0);}    


we are using blocked sockets (its a simple console program) and we are not using select to poll. Would that help? And now the server is not accepting the call from the client yet the client says it is connected, sends info, but then spits out an error on receiving that the connection was reset and it can''t recieve. The server does not indicate any data sent. Our heads are spinning.

******************************
"I do not fear computers, I fear the lack of them"

- Isaac Asimov

Drew Sikora
Napali Networks, Inc.
******************************"I do not fear computers, I fear the lack of them" - Isaac AsimovDrew SikoraNapali Networks, Inc.
You''re loosing your connection somewhere along the line.
Also, send() & recv() are not guaranteed to send/recv the amount of data that you specify it to send. Try using something like this.


BOOL ReadSocket( const SOCKET s, char *pBuffer, ULONG nAmountToRead )
{
ULONG nPos = 0;
ULONG nRead = 0;

while( nPos != nAmountToRead )
{
nRead = recv( s, &pBuffer[nPos], nAmountToRead - nPos, 0 );
if ( nRead == SOCKET_ERROR )
return WSAGetLastError();

nPos += nRead;
}

return TRUE;
}

BOOL WriteSocket( const SOCKET s, const char *pBuffer, ULONG nAmountToWrite )
{
ULONG nPos = 0;
ULONG nWrote = 0;

while( nWrote != nAmountToWrite )
{
nWrote = send( s, &pBuffer[nPos], nAmountToWrite - nWrote, 0 );
if ( nWrote == SOCKET_ERROR )
return WSAGetLastError();

nPos += nWrote;
}

return TRUE;
}
Heh, woops, I starting coding off the functions as BOOL, they should have been LONG. Was coding LONG, and thinking BOOL.
They both should return nPos also.
ARG#$(*#$

I''m smoking some good stuff today eh?
Thanks. We'll try your idea and see if it helps.

EDIT 8/25 - Thanx, your suggestion helped us solve part of our problem, but not all of it. If you're still interested, see my new, more detailed post about our new, more detailed problem .

******************************
"I do not fear computers, I fear the lack of them"

- Isaac Asimov

Drew Sikora
Napali Networks, Inc.

Edited by - WebSpyder on August 25, 2000 1:34:02 PM
******************************"I do not fear computers, I fear the lack of them" - Isaac AsimovDrew SikoraNapali Networks, Inc.
argh.

Edited by - DireWolf on August 29, 2000 11:49:21 AM

This topic is closed to new replies.

Advertisement