CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2008
    Posts
    168

    socket connection reset on every 5 minutes

    Dear Members,

    I wrote a client-server program and I ended up in a difficult situation.

    OS: CentOS7.9 and Rocky9

    Language Used: C++
    Compiler: GCC 11

    Client and server waiting infinitely and none of the socket close calls executed.
    Keep alive time/interval/probe: 7200 / 75/ 9

    Even though, the socket connection terminated every 5 minutes.
    I set the KEEP_ALIVE flag and sent and received a timeout but that did not resolve the issue.
    Are there any kernel calls/interrupts that close the socket?
    Please let me know how to resolve this issue.

    Best,
    Dave

  2. #2
    Join Date
    Nov 2018
    Posts
    137

    Re: socket connection reset on every 5 minutes

    So do you have any code to share?

    Preferably something short, as outlined by https://www.sscce.org/

    Failing that, a github link we can sync to.

  3. #3
    Join Date
    Apr 2008
    Posts
    168

    Re: socket connection reset on every 5 minutes

    This is my code fragment.
    Code:
    	fd_set readfds;
    	struct timeval timeout;
    	SOCKET conSocket;
    	struct sockaddr_in sa;
    	SOCKET s;
    	int n,nStatus;
    	UINT16	aPort;
    	unsigned long option;
    	union
    	{
    		tDCMSG msg;
    		UINT8 b[32];
    		UINT16 h[16];
    		UINT32 w[8];
    	} u;
    	// Create the listen socket.
    	memset((void *)&sa, 0, sizeof(sa));
    	S_ADDR(sa) = NTOHL(INADDR_ANY);
    	aPort = (UINT16)m_port;
    	sa.sin_port = NTOHS(aPort);
    	sa.sin_family = AF_INET;
    	conSocket = socket(AF_INET, SOCK_STREAM, 0);
    
    	int	reuseOn	= 1;
    
    	setsockopt(conSocket, SOL_SOCKET,SO_REUSEADDR,(const char*)&reuseOn, sizeof(reuseOn));
    
    	nStatus = bind(unsigned int, (struct sockaddr *)&sa, sizeof(sa));
    
    	listen(conSocket, 1);
    
    	while (bRunning)
    	{
    		FD_ZERO(&readfds);
    
    		FD_SET(conSocket, &readfds);
    
    		timeout.tv_sec = 3;
    		timeout.tv_usec = 0;
    
    		if (1 == select(conSocket+1, &readfds, NULL, NULL, &timeout))
    		{
    			memset((void *)&sa, 0, sizeof(sa));
    			n = sizeof(struct sockaddr_in);
    
    			if (INVALID_SOCKET != (s = accept(conSocket, (struct sockaddr *)&sa, (socklen_t*)&n)))
    			{
    				option = 1;
    				ioctl(s, FIONBIO, &option);
    				timeout.tv_sec = 3;
    				timeout.tv_usec = 0;
    				FD_ZERO(&readfds);
    				FD_SET(s, &readfds);
    				if (1 == select(s+1, &readfds, NULL, NULL, &timeout))
    				{
    					n = recv(s, (char *)u.b, sizeof(u), 0);
    				}
    			}
    		}
    	}
    	// Close the listen socket.
    	Close(conSocket);
    This is client side code . it is expected to wait for data from server. But client side socket connection terminated .
    What setting lead to the failure.
    Last edited by VictorN; August 28th, 2024 at 02:01 AM. Reason: added Code tags

  4. #4
    Join Date
    Nov 2018
    Posts
    137

    Re: socket connection reset on every 5 minutes

    > This is my code fragment.
    I wanted something I can copy/paste/compile/run.

    It's not even formatted properly,

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,413

    Re: socket connection reset on every 5 minutes

    Quote Originally Posted by Dave1024 View Post
    This is my code fragment.
    ...
    This is client side code . it is expected to wait for data from server. But client side socket connection terminated .
    What setting lead to the failure.
    Well it is your 166-th post here in the Forum, but you still cannot use CODE tags properly?
    Just Go Advanced and use there # button in the toolbar
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2018
    Posts
    137

    Re: socket connection reset on every 5 minutes

    > This is client side code . it is expected to wait for data from server.
    I normally associated the whole listen/accept pattern with being a server, not a client.

    You might want to read this before deciding SO_REUSEADDR is the answer to your problem.
    https://learn.microsoft.com/en-us/wi...g-so_reuseaddr

    Check the return result of all your functions - seriously, you're driving around blind.

    If 'int' and 'socklen_t' are different sizes, your accept call is well and truly borked.

    You don't close 's' for the accepted connection.

    Wait, don't tell me, you do all these things in the "real" code, this is just some hashed up facsimile.

  7. #7
    Join Date
    Apr 2008
    Posts
    168

    Re: socket connection reset on every 5 minutes

    Hello,
    I can find the source of the socket close.
    The socket close is sponsored by the kernel RST flag.
    I am trying to connect to port 65000 and 65001. How can I reset the RST flag to exclude my ports?
    Dave.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured