|
-
August 11th, 2005, 10:46 AM
#1
Simple CSocket Program Hangs
I have what I think is some very simple CSocket code for a client and a server,
but I'm having difficulty getting it working. Any advice would be greatly appreciated.
Here's the situation:
I have a client program and a server program. I start them both up, assuring
that the server starts up first and is waiting and ready for the client.
When the client program is started it successfully makes a connection to the server,
sends a simple string to the server, and then tries to ReadString from the
Server, on the assumption that the server got the string from the Client and
sends a string back to the Client.
The Server does indeed Accept the connection from the Client, successfully sets
up the sending and receiving Archives, and then does the ReadString, expecting to
read the string that was sent by the Client. But it never gets the string. It waits
forever inside the ReadString.
So, the Client is left hanging, waiting for a response from the Server,
while the Server is left hanging, never getting the string sent (supposedly
successfully) from the Client.
And there it waits, never completing the operations.
The code below shows the basic, stripped-down versions of the client and server programs.
What am I doing wrong?
Code:
// CLIENT CODE
AfxSocketInit();
CSocket ClientSocket;
int nPortNumberClient = 0; // 0 means for MFC to select an available port
if (ClientSocket.Create(nPortNumberClient))
{
int nPortNumberServer = 49153;
// Connect to the Server
if (ClientSocket.Connect("68.171.26.117", nPortNumberServer))
{
CSocketFile SocketFile(&ClientSocket);
CArchive ArchiveSending(&SocketFile, CArchive::store);
CArchive ArchiveReceiving(&SocketFile, CArchive::load);
// Send "GiveMeData" to the Server
ArchiveSending.WriteString("GiveMeData");
ArchiveSending.Flush(); // to assure sending of the data.
// Get response from Server
CString strResponse;
ArchiveReceiving.ReadString(strResponse); // <<< GETS TO HERE AND STOPS
ArchiveReceiving.Close();
ArchiveSending.Close();
SocketFile.Close();
}
else
{
TRACE("CLIENT Connection failure, timeout, perhaps?\n");
}
}
else
{
// Failed to Create the CSocket.
}
and here's the server code
Code:
// SERVER CODE
AfxSocketInit();
CSocket SocketServer;
int nPortNumberServer = 49153;
if (SocketServer.Create(nPortNumberServer, SOCK_STREAM, "68.171.26.117"))
{
if (SocketServer.Listen(5))
{
while (TRUE)
{
// Accept the connection.
CSocket sockRecv;
if (SocketServer.Accept(sockRecv))
{
// Create a file object
CSocketFile file(&sockRecv);
// Create a receiving stream.
CArchive ArchiveReceiving(&file, CArchive::load);
// Create a sending stream.
CArchive ArchiveSending(&file, CArchive::store);
.. Get string that was sent from Client
CString strCommand;
ArchiveReceiving.ReadString(strCommand); // GETS TO HERE AND STOPS
// In response to string from Server, send somtheing back to Client
ArchiveSending.WriteString("Data");
ArchiveReceiving.Close();
ArchiveSending.Flush();
ArchiveSending.Close();
}
else
{
// Failed to accept the connection
}
}
}
else
{
// Failed to listen
}
}
else
{
// Failed to create the main socket
}
-
August 11th, 2005, 11:45 AM
#2
Re: Simple CSocket Program Hangs
hello roger,
i have never used CSocket, but it sounds like what has been described as the "classic mistake" in sockets programming.
send and recv are strange functions.. what i mean by this is:
send like this:
Code:
int sendTCP(const char * data,int bytestosend){
int nRet=0;
int bytessent=0;
while (bytessent<bytestosend){
nRet=send(tcpSocket,data+bytessent,bytestosend-bytessent,0);
//check for socket error.
if (nRet==SOCKET_ERROR){
cout<<"There was a socket error in sendTCPtoServer."<<endl;
return -1;
}
bytessent=bytessent+nRet;
}
return 0;
}
and receive like this:
Code:
int rcvTCP(char * data,int bytestorcv){
int nRet=0;
int bytesrecvd=0;
while (bytesrecvd<bytestorcv){
nRet=recv(tcpSocket,data+bytesrecvd,bytestorcv-bytesrecvd,0);
//check for socket error.
if (nRet==SOCKET_ERROR){
cout<<"There was a socket error in rcvTCPfromServer."<<endl;
return -1;
}
bytesrecvd=bytesrecvd+nRet;
}
return 0;
}
of course, you will have to modify the code to use your sockets adn have it work with CSocket.
have a look at these faqs:
http://www.codeguru.com/forum/showth...hreadid=296198
http://www.codeguru.com/forum/showth...hreadid=296875
http://www.codeguru.com/forum/showthread.php?t=303078
hope this helps and that this is in fact your problem... i don't know anything about CSocket as i mentioned earlier... but from the sound of what you said, i think this is your problem.
-
August 11th, 2005, 12:54 PM
#3
Re: Simple CSocket Program Hangs
Well, part of the problem in the code that I posted is that the sent strings need to be termined with \n\r in order to be recognized as a "string" by the receiving side via its ReadString() method, since ReadString looks for the \n\r to know that a full string has been sent.
But even after correcting that problem I still have another problem. The sending and receiving works quite often, but still sometimes, randomly, fails. And I suspect that the problem is that the Server Closes the sending and receiving CArchives, and that sometimes (randomly) the Client hasn't yet had time to retrieve the data sent by the client, so the Server craps out with "An unknown errir occurred while cessing an unnamed file" which seems to be coming from the ReadString on the Client side.
So the question becomes:
How can the Server know when it's OK to close its CArchives, i.e. know when the Server has retrieved all the data?
- Roger
-
August 11th, 2005, 03:00 PM
#4
Re: Simple CSocket Program Hangs
 Originally Posted by RogerGarrett
The sending and receiving works quite often, but still sometimes, randomly, fails. And I suspect that the problem is that the Server Closes the sending and receiving CArchives, and that sometimes (randomly) the Client hasn't yet had time to retrieve the data sent by the client, so the Server craps out with "An unknown errir occurred while cessing an unnamed file" which seems to be coming from the ReadString on the Client side.
try putting an infinite while loop at the end of both your functions, so that the program does not terminate.. see if you still have the error.. that way you might be able to determine if this is in fact your problem.. (if i understand you correctly.. i'm not familiar with all this CArchive business)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|