Hi All,

i'm here due to an unexplainable winsock problem.

I've wrote a simple TcpSocket class to wrap in an object oriented way, the winsock functions.

For testing purposes i've written two applications too, a client and a server, wich just send/receive 10 bytes in a giant loop.


Now the problem:
On my machine, with Windows Xp SP3, the two programs run normally until cicle 65535 (integer?): at that point, on server side, a WSAECONNABORTED (10053) happens, causing the socket closure, while, on the server a WSAECONNRESET (10054) is returned. :-/

Running on a Windows 7 machines the same couple of applications, the failure happpens at the cicle 16 :-O

According to microsoft and other sources, WSAECONNABORTED is related to network problems and rarely to software issues: now the funny question... how can a network problem happens on a localhost connection?

I've also tried running the app over the two machines (Xp/win7) alternated to sniff with wireshark the network traffic ( Windows doesn't provide a way to capture the loopback ): the client sends a RESET packet to the server without any reason.

I've attached a screenshot of the two CMD line windows.

If you wish i may post the TcpSocket Class too.


Please, help my find out what's happening here!!!
Thanks guys.

Gianluca

REMARK
In the server i'm not missing to bind to the listen address: the Listen function binds to the listening address before calling listen.


This is the server code:

#include "stdafx.h"
#include "TcpSocket.h"

#define  listenPort 9559

int _tmain(int argc, _TCHAR* argv[])
{

	printf("TcpServerTest Started\n\n");
	

	//STARTING LISTENING SERVER
	TcpSocket socket;
	printf(("Starting listening on port " + to_string((long long)listenPort) + " ... ").c_str());
	if(!socket.Listen("", listenPort)){
		printf(("FAILURE\nError: " + socket.errorString() + "\n").c_str());
		Sleep(5000);
		return false;
	}
	printf("OK\n");

	//WAITING FOR INCOMING CONNECTION
	printf("Waiting for incoming connection...");
	TcpSocket * client = socket.Accept();
	if(!client->IsValid()){
		printf(("FAILURE\nError: " + socket.errorString() + "\n").c_str());
		Sleep(5000);
		return false;
	}


	char * outMsg = new char[10];
	int outSize = 10;
	int readData;
	//STARTING CICLE
	for(long long i = 1; i > 0; i ++){
		//Creating & sending out message
		if(!client->Write(outMsg, 10)){
			printf(("WRITING FAILURE\nError: " + client->errorString() + "\n").c_str());
			Sleep(500000);
			return false;
		}
		//Reading reply
		char * incoming = client->Read(readData);
		if(readData == 0){
			printf("\nConnection closed\n");
			break;
		}
		if(readData == SOCKET_ERROR){
			printf(("FAILURE\nError: " + client->errorString() + "\n").c_str());
			Sleep(5000000);
			return false;
		}
		delete incoming;
		printf(("Cicle " + to_string(i) + " complete\n").c_str());
	}

	delete outMsg;
	Sleep(5000);
	return 0;
	
}

And This is the client code:

#include "stdafx.h"
#include "TcpSocket.h"

#define  connectPort 9559

int _tmain(int argc, _TCHAR* argv[])
{
		printf("TcpClientTest Started\n\n");
	

	//STARTING LISTENING SERVER
	TcpSocket socket;
	printf(("Connecting on port " + to_string((long long)connectPort) + " ... ").c_str());
	if(!socket.Connect("127.0.0.1", connectPort)){
		printf(("FAILURE\nError: " + socket.errorString() + "\n").c_str());
		Sleep(5000);
		return false;
	}
	printf("OK\n");


	int outSize = 10;
	char * outMsg = new char[10];
	//STARTING CICLE
	for(long long i = 1; i > 0; i ++){
		//Reading input message
		int readData;
		char * incoming = socket.Read(readData);
		if(readData == 0){
			printf("\nConnection closed\n");
			break;
		}
		if(readData == SOCKET_ERROR){
			printf(("FAILURE\nError: " + socket.errorString() + "\n").c_str());
			Sleep(5000000);
			return false;
		}
		delete incoming;
		//Sending reply message
		if(!socket.Write(outMsg, 10)){
			printf(("WRITING FAILURE\nError: " + socket.errorString() + "\n").c_str());
			Sleep(500000);
			return false;
		}
		printf(("Cicle " + to_string(i) + " complete\n").c_str());
	}
	delete outMsg;
	Sleep(5000);
	return 0;
}

Last the TcpSocket header:

#pragma once
#pragma comment(lib, "Ws2_32.lib")

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>

#include "TcpSocket.h"


#include <string>
using namespace std;

class TcpSocket
{
public:
	TcpSocket(void);
	~TcpSocket(void);

	void		Close();
	bool		Connect(char * address, int port);
	bool 		Write (char* data, int length);
	bool  		Write (wchar_t* data, int length);
	char *	    Read(int & readBytes);
	void		SetSocket(int socket) { sock = socket; };
	int			Socket() { return sock; };
	void		SetBufferSize(unsigned int size);
	bool		Listen(char * address, int port);
	TcpSocket * Accept();
	bool		isBlocking(){ return blocking_socket; };
	bool		setBlocking(bool);
	bool		hasReadyData();
	bool		IsValid(){ return sock != INVALID_SOCKET; };
	string  errorString(){ return errorMsg; };
	int			lastErrorCode(){ return errorCode; };
	//int		SendData			(int sock, char * ptr, int length);
static string   getErrorMessage(int errorCode);

private:
	int sock;
	int errorCode;
	string errorMsg;

	int  bufferSize;
	char * buffer;
	int  readyBytes;

	bool blocking_socket;

	fd_set * readSet,
		   * writeSet,
		   * exceptionSet;
	timeval * timeout;

};

could you show the TcpSocket class' method definitions too???

Hi All,

I've solved the magic issue... nothing magic, just my fault ( as expected :D ).

Reading on a tutorial, i was using MSG_OOB flag for send().

That caused the problem: setting the flag just to 0, the programs are now running since 1 hour without any problem!


Sorry guys for my mistake!

Thanks for you support!

Bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.