Click to See Complete Forum and Search --> : 10057 error


MasterDucky
October 24th, 2009, 02:49 AM
WSAGetLastError() returns 10057 ,meaning socket error, on the recv() function though i am connected.

And the recv() function isnt blocking it loops around.

Server and client in the same code.

Thanks for any suggestions.



#include <windows.h>
#include <WinSock2.h>
#include <iostream>

using namespace std;

const int STRLEN = 256; char message[STRLEN] = {0};
unsigned int port = 666;
char recMessage[STRLEN] = {0};
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

class Socket
{
protected:
WSADATA wsaData;
SOCKET mySocket;
SOCKET myBackup;
SOCKET acceptSocket;
sockaddr_in sockad;
public:
Socket();
~Socket();
bool SendData( char* );
bool RecvData( char*, int );
void CloseConnection();
void GetAndSendMessage();
};

class ClientSocket : public Socket //Inheritance
{
public:
void ConnectToServer( const char *ipAddress, int port );
}; ClientSocket sockClient;

void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
sockad.sin_family = AF_INET;
sockad.sin_addr.s_addr = inet_addr( ipAddress );
sockad.sin_port = htons( port );

if ( connect( mySocket, (SOCKADDR*) &sockad, sizeof( sockad ) ) == SOCKET_ERROR )
{
cerr << "ClientSocket: Failed to connect\n";
system("pause");
WSACleanup();
exit(13);
}
cout << "\nCONNECTED!\n\n";
}

void Socket::GetAndSendMessage() //Member functions
{
cin.ignore();
message[0] = '\0';

cout << "You > ";
cin.get( message, STRLEN );
SendData( message );
}

bool Socket::SendData( char *buffer )
{
send( mySocket, buffer, strlen( buffer ), 0 );
return true;
}

bool Socket::RecvData( char *buffer, int size )
{
int i = recv( mySocket, buffer, size, 0 );

cout<<WSAGetLastError()<<"\r\n";
cout <<i<< "\n";

buffer[i] = '\0';
return true;
}

Socket::Socket()
{
if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
{
cerr << "Socket Initialization: Error with WSAStartup\n";
system("pause");
WSACleanup();
exit(10);
}

//Create a socket
mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( mySocket == INVALID_SOCKET )
{
cerr << "Socket Initialization: Error creating socket" << endl;
system("pause");
WSACleanup();
exit(11);
}

myBackup = mySocket;
}

Socket::~Socket()
{
WSACleanup();
}
/////////////////////////////////////////////////////////////////////////
class ServerSocket : public Socket //Inheritance
{
public:
void Bind( int port );
void Listen();
void StartHosting( int port );
}; ServerSocket sockServer;

void ServerSocket::StartHosting( int port )
{
Bind( port );
Listen();
}

void ServerSocket::Bind( int port )
{
sockad.sin_family = AF_INET;
sockad.sin_addr.s_addr = inet_addr( "0.0.0.0" );
sockad.sin_port = htons( port );

if ( bind ( mySocket, (SOCKADDR*) &sockad, sizeof( sockad) ) == SOCKET_ERROR )
{
cerr << "ServerSocket: Failed to connect\n";
system("pause"); WSACleanup(); exit(14);
}
}

void ServerSocket::Listen()
{
if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
{
cerr << "ServerSocket: Error listening on socket\n";
system("pause");
WSACleanup();
exit(15);
}

acceptSocket = accept( myBackup, NULL, NULL );
while ( acceptSocket == SOCKET_ERROR )
{
acceptSocket = accept( myBackup, NULL, NULL );
}
mySocket = acceptSocket;
cout << "\nCONNECTED!\n\n";
}

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

int main()
{
string choice;
string ipAddress;
while(1){
cout << "1) Client" << endl;
cout << "2) Server" << endl;
cin >> choice;

/***************************************CLIENT***************************************/
if ( choice == "1" )
{
cout << "ATTEMPTING TO CONNECT..." << endl;
sockClient.ConnectToServer( "127.0.0.1", port );
while (1)
{
sockClient.GetAndSendMessage();
}
sockClient.CloseConnection();
}
/****************************************SERVER***************************************/
else if ( choice == "2" )
{
//(HANDLE)_beginthreadex(NULL, 0, LISTENandACCEPT , NULL, 0, &Th2);
cout << "\t--------------\n" << endl;
cout << "\t****SERVER****\n" << endl;
cout << "\t--------------\n" << endl;
cout << "WAITING FOR CLIENT..." << endl;
sockServer.StartHosting( port );

while(1)
{
sockClient.RecvData( recMessage, STRLEN );
cout << "Friend > " << recMessage << endl; Sleep(1999);
}
}
else cout << "\nType 1 or 2 Please!\n\n";
}
}

hoxsiew
October 24th, 2009, 12:42 PM
What compiler are you using? I can't get your code to compile.

Nevertheless, shouldn't you be polling sockServer?

sockServer.StartHosting( port );

while(1)
{
sockServer.RecvData( recMessage, STRLEN );
cout << "Friend > " << recMessage << endl; Sleep(1999);
}

MasterDucky
October 24th, 2009, 12:59 PM
Hi, thanks for your response!

Im using codeblocks with mingw and it compiles without a problem.
What error do you get?

What do you mean by polling?
If you mean to declare, it is declared here: "class ServerSocket : public Socket" .

hoxsiew
October 24th, 2009, 03:13 PM
No, I mean your calling the wrong object. Look at your code for the server section of main:


else if ( choice == "2" )
{
//(HANDLE)_beginthreadex(NULL, 0, LISTENandACCEPT , NULL, 0, &Th2);
cout << "\t--------------\n" << endl;
cout << "\t****SERVER****\n" << endl;
cout << "\t--------------\n" << endl;
cout << "WAITING FOR CLIENT..." << endl;
sockServer.StartHosting( port );

while(1)
{
sockClient.RecvData( recMessage, STRLEN );
cout << "Friend > " << recMessage << endl; Sleep(1999);
}
}
else cout << "\nType 1 or 2 Please!\n\n";
}


It is the server object your dealing with in option '2' so I would assume you wanted to call sockServer.RecvData() instead of sockClient.RecvData() since as far as I can tell, sockClient is not initialized for the server option.

hoxsiew
October 24th, 2009, 03:17 PM
Oh, and the errors compiling in VS2005 (I analyzed it a bit further)were not including the header for std::string and your socket class has on implementation for void CloseConnection(); a violation in ANSI C++.

Richard.J
October 24th, 2009, 07:34 PM
... and your socket class has on implementation for void CloseConnection(); a violation in ANSI C++.

Is that really a violation? My gcc 3.3.4. for a Redhat Linux has no problem with that.

MasterDucky
October 25th, 2009, 12:39 AM
Oh god, thanks a lot Hoxsiew!

You dont know how long ive been searching.

You made one man really happy today! :)

Btw its interesting that mingw doesnt require the header for std::string to be included.