Hi,
I'm having a very peculiar problem with my socket implementation in a c++ dll. Basically, what the code does is make a connection, send and receive messages to/from a server, and close the connection.
4 out of 5 people in my group can successfully run a program that uses this functionality in the dll but 1 of them frequently encounters an error when receiving back messages from the server.
Here's a snipet of my code:
// Initialize Winsocket.
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
{
return -1;
}
// Create a TCP/IP socket that is bound to the server.
if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
return -1;
}
// Fill out the server socket's address information.
destination_sin.sin_family = AF_INET;
// Retrieve the host information corresponding to the host name.
if ((phostent = gethostbyname (HOSTNAME)) == NULL)
{
closesocket (ServerSock);
return -1;
}
// Assign the socket IP address.
memcpy ((char FAR *)&(destination_sin.sin_addr),
phostent->h_addr,
phostent->h_length);
// Convert to network ordering.
destination_sin.sin_port = htons (PORTNUM);
// Establish a connection to the server socket.
if (connect (ServerSock,
(PSOCKADDR) &destination_sin,
sizeof (destination_sin)) == SOCKET_ERROR)
{
closesocket (ServerSock);
return -1;
}
// Send a string to the server.
if (send (ServerSock, "\x00", 1, 0)
== SOCKET_ERROR)
{
return -1;
}
// Get acknowledgement
iReturn = recv (ServerSock, buf, (MAXDATASIZE - 1), 0);
if ((iReturn != 1) || (buf[0] != '\x06'))
{
return -1;
}
His problem:
The return value is sometimes 0 which means that the server terminated the connection successfully. Other times it's
-1 and the description I get back from WSAGetLastError () is WSAESHUTDOWN (10058). I don't understand since I did not explicitly call a shutdown anytime prior to the code above.
If you have any cluse, please help.
Thanks!!!
I'm having a very peculiar problem with my socket implementation in a c++ dll. Basically, what the code does is make a connection, send and receive messages to/from a server, and close the connection.
4 out of 5 people in my group can successfully run a program that uses this functionality in the dll but 1 of them frequently encounters an error when receiving back messages from the server.
Here's a snipet of my code:
// Initialize Winsocket.
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
{
return -1;
}
// Create a TCP/IP socket that is bound to the server.
if ((ServerSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
return -1;
}
// Fill out the server socket's address information.
destination_sin.sin_family = AF_INET;
// Retrieve the host information corresponding to the host name.
if ((phostent = gethostbyname (HOSTNAME)) == NULL)
{
closesocket (ServerSock);
return -1;
}
// Assign the socket IP address.
memcpy ((char FAR *)&(destination_sin.sin_addr),
phostent->h_addr,
phostent->h_length);
// Convert to network ordering.
destination_sin.sin_port = htons (PORTNUM);
// Establish a connection to the server socket.
if (connect (ServerSock,
(PSOCKADDR) &destination_sin,
sizeof (destination_sin)) == SOCKET_ERROR)
{
closesocket (ServerSock);
return -1;
}
// Send a string to the server.
if (send (ServerSock, "\x00", 1, 0)
== SOCKET_ERROR)
{
return -1;
}
// Get acknowledgement
iReturn = recv (ServerSock, buf, (MAXDATASIZE - 1), 0);
if ((iReturn != 1) || (buf[0] != '\x06'))
{
return -1;
}
His problem:
The return value is sometimes 0 which means that the server terminated the connection successfully. Other times it's
-1 and the description I get back from WSAGetLastError () is WSAESHUTDOWN (10058). I don't understand since I did not explicitly call a shutdown anytime prior to the code above.
If you have any cluse, please help.
Thanks!!!