|
-
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
}
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
|