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 » Community » Newbie corner » TcpSocket, Know when distant connection have been closed
Re: TcpSocket receiving ghost data [message #57233 is a reply to message #57223] Sun, 13 June 2021 23:06 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
I found what I needed, in order to get noticed of pair closing socket (i.o client sending a FIN)

According to MSDN and the select(...) (from Winsock2) function :
Quote:
For connection-oriented sockets, readability can also indicate that a request to close the socket has been received from the peer. If the virtual circuit was closed gracefully, and all data was received, then a recv will return immediately with zero bytes read. If the virtual circuit was reset, then a recv will complete immediately with an error code such as WSAECONNRESET. The presence of OOB data will be checked if the socket option SO_OOBINLINE has been enabled (see setsockopt).


To perform a correct client server ready to handle a FIN from a client, I did this :

Server.h
d_activeConnection.Timeout(500);
d_activeConnection.Linger(200);
SocketWaitEvent swe;
swe.Add(d_activeConnection, 0x7); // 0x7 is WAIT_ALL
while(!d_stopThread && !d_activeConnection.IsError() && d_activeConnection.IsOpen()){
	swe.Wait(500);
	if(swe[0] & WAIT_WRITE){
		if((swe[0] & WAIT_READ)){
			Upp::String data;
			
			int read =  d_activeConnection.Get(); //if read return -1 then pair have sent us FIN
			if(read == -1) break;
			data << char(read);
			do{ data << char(d_activeConnection.Get()); }while(d_activeConnection.Peek() != -1);
								
			if(data.GetCount() > 0){
				Upp::String sendingCmd = "";
				LLOG("[Server][Listener] Receiving  from web server: " + data.Left(20));
				sendingCmd = d_callbackServer(d_activeConnection, data);
				if(sendingCmd.GetCount() > 0){
					LLOG("[Server][Listener] Sending to web server: " + sendingCmd.Left(20));
					d_activeConnection.Put(sendingCmd);
				}
			}
		}
	}else{
		break;
	}
}
if(d_activeConnection.IsError()) LLOG("[Server][Listener] WebServer error: " + d_activeConnection.GetErrorDesc());
d_activeConnection.Close();
LLOG("[Server][Listener] WebServer disconnected");
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: RichEdit with word wrap?
Next Topic: Calendar
Goto Forum:
  


Current Time: Sun Aug 24 23:19:46 CEST 2025

Total time taken to generate the page: 0.05852 seconds