WSAECONNABORTED Error Help

Started by
4 comments, last by Shock 18 years, 10 months ago
Hello all ! Im having winsock problems on my programs. Let me explain. Im creating a server and client programs that use TCP. When my client connects, the server accepts, everything goes well. However when I send data from the server to the client or vice versa I get hit with a WSAECONNABORTED error. Im not very knowledgable on winsock and all the Platform SDK docs tell me is: Software caused connection abort. An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error. I have checked protocols and both are fine. It would not accept/connect if this wasn't the case, right? As for timing out, I dont know...I use select() for monitoring when to recv/send data. I have no idea on where to start in order to fix this. Iv'e looked at the code and all seems to be well, I have even re-written it to no avail. The server and client run from the same machine and are both bound and listen/connect to 127.0.0.1, port 1000. Im sure this isn't the problem, is it? Someone please give me some idea of where to start fixing this, or possible causes for this error! Thanks
Advertisement
You maye get that error if the program on the other end quits without closing the socket. This is probably what is happening.

Obviously we can't be any more specific than this at the moment, as you've provided no source and only the vaguest description of what you're doing.

How long does the client/server stay running for after it connects / accepts? Does either of them quit immediately after connect or accept? If so, that may cause a connection reset.

Mark


Thank you for the reply markr

The server stays running until a call to _kbhit() returns true. During this time its basically handling processing for the clients,... well it should when I fix this error. In another thread the server listens for incoming connections. I use mutexes for the thread.

The client atm basically sits and waits for data to be sent from the server, which it cant do atm. Until then it processes the window message queue using GetMessage().

I was wondering...when multithreading...would creating a socket, binding and listening on THREAD A and then accepting connections on THREAD B ( listen thread ) cause this problem as well if the socket is not being passed through the thread proc's function parameter ?

Thanks
Maybe it would help you : here

Post some code would be fine to track your bug..
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
YOu may be inadvertently closing a socket in the other thread.

WSAECONNABORTED is commonly delivered under 2 conditions:

1. The socket was closed while a send/recv was in progress.
2. TCP was not able to send data to the other side within a certain time (on a loopback connection ,this is definitely not an issue.)


Hello you guys. Here is my socket class. gWinsock is winsock management global variable, basically just init/shutdown of winsock 2.

To set up a socket on my server I do :

/* Initialize socket for incoming connections */if ( ! socket_bind( SERVER_HOST, SERVER_PORT ) || ! socket_listen() || ! socket_block(true) )		{			printf( "Unable to setup socket for incoming client connections\n" );			return false;		}


Then I call socket_accept() in my listen thread and wait for a connection.

For the client its, socket_connect() and socket_recv() basically.

Socket Class:
/* Constructor */socket_handler::socket_handler() :	socket_id(0)	{		socket_host = "Unknown";		socket_port = "Unknown";	}/* Constructor */socket_handler::socket_handler( int sock_id, addrinfo sock_addrinfo ) :	socket_id( sock_id ),	socket_addrinfo( sock_addrinfo )	{		/* Since we are being passed an already valid socket and address info , we 			need to get host:port strings */		char host[ NI_MAXHOST];		char port[ NI_MAXSERV ];		if ( getnameinfo( (sockaddr*) &sock_addrinfo, sizeof( sock_addrinfo ), host, NI_MAXHOST,			port, NI_MAXSERV, NI_NUMERICSERV ) != 0 )			{				printf( "socket::socket_handler, failed to get host name/port info\n" );				socket_host = "Unknown";				socket_port = "Unknown";			}		else			{				socket_host = host;				socket_port = port;			}	}/* Destructor */socket_handler::~socket_handler()	{		/* Close socket on destruction */		socket_close();	}/* Close the socket */void socket_handler::socket_close( void )	{		/* Close the socket if open */		if ( socket_id )			{				if ( dev_msg ) printf("socket::socket_close socket for %s:%s\n", socket_host.c_str(), socket_port.c_str() );				closesocket( socket_id );				socket_id = 0;			}		socket_host = "Unknown";		socket_port = "Unknown";	}/* Connect to a specified host */bool socket_handler::socket_connect( const char *host, const char *port )	{#ifdef _WIN32		/* Initialize winsock 2 on win32 platform */		if ( ! gWinsock.winsock_created() && ! gWinsock.winsock_init())			{				return false;			}#endif		/* Disconnect and close socket if open */		socket_close();		/* Create a new socket */		if ( (socket_id = (int) socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) 			{				printf( "Unable to create a new socket for connecting to %s:%s\n", host, port ); 				return false;			}		/* Get host address info */		ADDRINFO hints, *res;		int gai_rc = 0;		ZeroMemory( &hints, sizeof( hints ) );		hints.ai_family = AF_INET;		hints.ai_addr = INADDR_ANY;		hints.ai_socktype = SOCK_STREAM;		hints.ai_flags = AI_NUMERICHOST;		if ( (gai_rc = getaddrinfo( host, port, &hints, &res )) != 0 )			{				printf( "Unable to get host address info for connecting to %s:%s\nReason:%s\n", host, port,					gai_strerror( gai_rc));				return false;			}		/* Connect to the specified host:port */		if ( connect( socket_id, res->ai_addr, (int) res->ai_addrlen ) == SOCKET_ERROR  )			{				freeaddrinfo( res );				printf( "Unable to connect to host %s:%s", host, port );				return false;			}		/* Set data locally */		socket_addrinfo = *res;		socket_host = host;		socket_port = port;		/* Free address info resources */		freeaddrinfo( res );		if ( dev_msg ) printf( "socket::socket_connect, connected to %s:%s\n", host, port );		/* Done */		return true;	}/* Bind to a specified host */bool socket_handler::socket_bind( const char *host, const char *port )	{#ifdef _WIN32		/* Initialize winsock 2 on win32 platform */		if ( ! gWinsock.winsock_created() && ! gWinsock.winsock_init())			{				return false;			}#endif		/* Disconnect and close socket if open */		socket_close();		/* Create a new socket */		if ( (socket_id = (int) ::socket( AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET ) 			{				printf( "Unable to create a new socket for binding to %s:%s\n", host, port ); 				return false;			}		/* Get host address info */		ADDRINFO hints, *res;		int gai_rc = 0;		ZeroMemory( &hints, sizeof( hints ) );		hints.ai_family = AF_INET;		hints.ai_socktype = SOCK_STREAM;		hints.ai_addr = INADDR_ANY;		hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE ;		if ( (gai_rc = getaddrinfo( host, port, &hints, &res )) != 0 )			{				printf( "Unable to get host address info for binding to %s:%s\nReason:%s\n", host, port,					gai_strerror( gai_rc));				return false;			}		/* Bind the socket */		if ( bind( socket_id, res->ai_addr, (int) res->ai_addrlen ) == SOCKET_ERROR )			{				printf( "Unable to bind created socket to %s:%s", host, port );				freeaddrinfo( res );				return false;			}		/* Set data locally */		socket_addrinfo = *res;		socket_host = host;		socket_port = port;		/* Free address info resources */		freeaddrinfo( res );		if ( dev_msg ) printf( "socket::socket_bind, bound socket to %s:%s\n", host, port );		/* Done */		return true;	}/* Set the socket for listening */bool socket_handler::socket_listen( void )	{		/* Make sure we have a socket */		if ( ! socket_id ) 			{				printf( "Tried to listen on invalid socket!\n" );				return false;			}		/* Listen on the socket */		if ( listen( socket_id, SOMAXCONN ) == SOCKET_ERROR  )			{				printf( "Failed to listen on socket\n" );				return false;			}		if ( dev_msg ) printf( "socket::socket_listen, listening on %s:%s\n", socket_host.c_str(),			socket_port.c_str());		/* Done */		return true;	}/* Wait to accept a connection on the socket, returns a new allocated socket object */socket_handler *socket_handler::socket_accept( void )	{		addrinfo accept_address;		int addrlen = sizeof( accept_address );		/* Wait for an incoming connection , and then accept it */		int accept_socket = (int) accept( socket_id, (sockaddr*) &accept_address, &addrlen );		/* Make sure socket is valid */		if ( accept_socket == INVALID_SOCKET )			{				return 0;			}		if ( dev_msg ) printf( "socket::socket_accept, accepted an incoming connection\n" );		socket_handler *socket = new socket_handler( accept_socket, accept_address );		/* Create a new socket_handler object using the accepted socket */		return socket;	}/* Recieve data on the socket */bool socket_handler::socket_recv( char *buffer, int size, int &recv_bytes )	{		/* Make sure we have a valid socket to recieve on */		if ( ! socket_id )			{				printf( "Tried to recieve data on an invalid socket\n" );				return false;			}		/* Recieve the data */		if ( (recv_bytes = recv( socket_id, buffer, size, 0)) == SOCKET_ERROR )			{				printf( "Failed to recieve data on socket\nError: %d:%s\n", WSAGetLastError() , gWinsock.winsock_getlasterror());				return false;			}		/* Done */		return true;	}/* Send data on the socket */bool socket_handler::socket_send( char *buffer, int size,  int &send_bytes )	{		/* Make sure we have a valid socket to send on */		if ( ! socket_id )			{				printf( "Tried to send data on an invalid socket\n" );				return false;			}		/* Send the data */		if ( (send_bytes = send( socket_id, buffer, size, 0)) == SOCKET_ERROR )			{				printf( "Failed to send data on socket" );				return false;			}		/* Done */		return true;	}/* Enables/disables blocking on the socket */bool socket_handler::socket_block( bool block )	{		/* Make sure we have a valid socket */		if ( ! socket_id )			{				printf( "Tried to change blocking on an invalid socket\n" );				return false;			}		unsigned long blocking_status = (block) ? 0 : 1;		/* Set the socket blocking state */		if ( ioctlsocket( socket_id, FIONBIO, &blocking_status ) != 0 )			{				printf( "Unable to change blocking status on socket\n" );				return false;			}		/* Done */		return true;	}/* Check the socket for readability, writeability and errors */bool socket_handler::socket_select( bool read, bool write, bool error )	{		/* Make sure we have a valid socket */		if ( ! socket_id )			{				printf( "Tried to call select() on an invalid socket\n" );				return false;			}		FD_SET set;		FD_ZERO( &set );		FD_SET( socket_id, &set );		/* Timeout for select() */		TIMEVAL tv = { 0, 0 };		int result = 0;		/* Call select() querying read/write or error status */		if ( (result = select( set.fd_count,  (read) ? &set : 0, (write) ? &set : 0, (error) ? &set : 0, &tv )) == SOCKET_ERROR )			{				FD_ZERO(&set);				printf( "Failed to select() on socket\n" );				return false;			}		FD_ZERO(&set);		/* Return result */		return result == 1;	}

This topic is closed to new replies.

Advertisement