Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Lost socket error (and how to get it)
Lost socket error (and how to get it) [message #40198] Tue, 02 July 2013 10:47 Go to next message
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
Hello all

When calling TcpSocket::Connect() the error code is lost. Take a look to actual function and comments:

bool TcpSocket::RawConnect(addrinfo *arp)
{
	if(!arp) {
		SetSockError("connect", -1, "not found");
		return false;
	}
	for(int pass = 0; pass < 2; pass++) {
		addrinfo *rp = arp;
		while(rp) {
			if(rp->ai_family == AF_INET == !pass && // Try to connect IPv4 in the first pass
			   Open(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) {
				if(connect(socket, rp->ai_addr, (int)rp->ai_addrlen) == 0 ||
				   GetErrorCode() == SOCKERR(EINPROGRESS) || GetErrorCode() == SOCKERR(EWOULDBLOCK)
				) {
					mode = CONNECT;
					return true;
				}
				Close();		// Error is cleaned
			}
			rp = rp->ai_next;
		}
    }
	SetSockError("connect", -1, "failed");		// Here is not reported
	return false;
}

A possible solution to get the error could be:
- int errorCode
- errorCode = GetErrorCode() just before Close()
- Including TcpSocketErrorDesc(errorCode) in SetSockError();


Best regards
Iñaki
Re: Lost socket error (and how to get it) [message #40201 is a reply to message #40198] Tue, 02 July 2013 19:43 Go to previous messageGo to next message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
This is my client code:

#ifndef _ConnSock_ConnSock_h_
#define _ConnSock_ConnSock_h_

#include <Core/Core.h>

using namespace Upp;

String snd(String r, int a){
	TcpSocket s;
	if(!s.Connect("127.0.0.1", 11811)) {
	    return "x";
	}

	s.Put(r + "\n\0");
	if(a==1){
    	String st = s.GetLine();
	    return st;
	}
	return "y";
}
#endif


As far as any error in connecting:
	if(!s.Connect("127.0.0.1", 11811)) {
	    return "x";
	}



Re: Lost socket error (and how to get it) [message #40202 is a reply to message #40198] Tue, 02 July 2013 20:46 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13986
Registered: November 2005
Ultimate Member
koldo wrote on Tue, 02 July 2013 04:47

Hello all

When calling TcpSocket::Connect() the error code is lost. Take a look to actual function and comments:

bool TcpSocket::RawConnect(addrinfo *arp)
{
	if(!arp) {
		SetSockError("connect", -1, "not found");
		return false;
	}
	for(int pass = 0; pass < 2; pass++) {
		addrinfo *rp = arp;
		while(rp) {
			if(rp->ai_family == AF_INET == !pass && // Try to connect IPv4 in the first pass
			   Open(rp->ai_family, rp->ai_socktype, rp->ai_protocol)) {
				if(connect(socket, rp->ai_addr, (int)rp->ai_addrlen) == 0 ||
				   GetErrorCode() == SOCKERR(EINPROGRESS) || GetErrorCode() == SOCKERR(EWOULDBLOCK)
				) {
					mode = CONNECT;
					return true;
				}
				Close();		// Error is cleaned
			}
			rp = rp->ai_next;
		}
    }
	SetSockError("connect", -1, "failed");		// Here is not reported
	return false;
}

A possible solution to get the error could be:
- int errorCode
- errorCode = GetErrorCode() just before Close()
- Including TcpSocketErrorDesc(errorCode) in SetSockError();


I am not so sure about this: note that it is doing two passes, tries to connect ipv4 first, then ipv6. Which error is relevant?

Mirek
Re: Lost socket error (and how to get it) [message #40208 is a reply to message #40202] Wed, 03 July 2013 08:46 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
Hello all

I have not been very clear explaining the problem. Sorry Rolling Eyes

Actual RawConnect() function do not report the real cause of the error. This is due to:
- It just do a SetSockError("connect", -1, "failed");
- As Close() is called every connection try, the error cause is deleted, so no function out of RawConnect() can know it.

A way to improve the function could be to get the source of the problem with GetErrorCode() and TcpSocketErrorDesc() before doing the Close().

In my case I had a buffer overflow error (WSAENOBUFS in Windows). The only way I had to know it was patching the function. Without knowing a problem root cause it is difficult to solve it Very Happy


Best regards
Iñaki
Re: Lost socket error (and how to get it) [message #40464 is a reply to message #40208] Mon, 05 August 2013 12:23 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
Hello Mirek

Could you manage this Smile ?


Best regards
Iñaki
Re: Lost socket error (and how to get it) [message #40466 is a reply to message #40464] Mon, 05 August 2013 13:13 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13986
Registered: November 2005
Ultimate Member
Sorry for the delay... I just want to have the correct solution.

Issue is still the same: There are more attempts to connect (there is the loop..). Which error is relevant?

Frankly, perhaps we should return just first one. Or maybe detect that the site does not support ipv4, then report error for ipv6, otherwise ipv4. I do not know... (that is why I have not acted then).

Mirek
Re: Lost socket error (and how to get it) [message #40467 is a reply to message #40466] Mon, 05 August 2013 16:15 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
Hello Mirek

You may return just both errors:

strError << TcpSocketErrorDesc(errorCode1) << "\n" << TcpSocketErrorDesc(errorCode2);
SetSockError("connect", -1, strError);


It would work like now but at least we would get an error description Smile.


Best regards
Iñaki

[Updated on: Tue, 06 August 2013 08:01]

Report message to a moderator

Re: Lost socket error (and how to get it) [message #40549 is a reply to message #40467] Sun, 11 August 2013 18:17 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13986
Registered: November 2005
Ultimate Member
koldo wrote on Mon, 05 August 2013 10:15

Hello Mirek

You may return just both errors:

strError << TcpSocketErrorDesc(errorCode1) << "\n" << TcpSocketErrorDesc(errorCode2);
SetSockError("connect", -1, strError);


It would work like now but at least we would get an error description Smile.


OK, sounds reasonable, done.
Re: Lost socket error (and how to get it) [message #40616 is a reply to message #40549] Fri, 23 August 2013 16:35 Go to previous message
koldo is currently offline  koldo
Messages: 3372
Registered: August 2008
Senior Veteran
Thank you! Smile

Best regards
Iñaki
Previous Topic: Need a working simple example of using sockets with GUI in U++
Next Topic: Communicate two PCs using http
Goto Forum:
  


Current Time: Sun Jun 16 21:11:20 CEST 2024

Total time taken to generate the page: 0.01144 seconds