Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Am trying relevent with socket programming but my thing is long duration connection which is deciated connection. but every after 1 min connection resetting connection due to recv failure with is error no : 22 error message : invalid argument. i tried maximum analysis but i can't able to find out my mistake. please help me for furthur process.

Below code just an sample, but am using that same socket programming important function in my project.

#include<iostream>
using namesapce std;
int g_SocFd  = -1;
bool Connect(char *p_IpAddress, int p_Port)
{
   if (0 > (g_SocFd = socket(AF_INET, SOCK_STREAM, 0)))
   {
      g_Logger.log(LOG_LEVEL_NORMAL, "%s <Line:%d> Failed to Create client Socket. [ErrNo:%d,ErrMsg:%s]",
            __FUNCTION__, __LINE__, errno, strerror(errno));
      g_SocFd = -1;
      return false;
   }
   g_Logger.log(LOG_LEVEL_DEBUG, "%s <Line:%d> IP_ADDRESS:%s PORT_NUMBER: %d", __FUNCTION__, __LINE__, p_IpAddress, p_Port);

   struct sockaddr_in serv_addr;
   memset(&serv_addr, 0, sizeof(serv_addr));
   serv_addr.sin_family = AF_INET;
   serv_addr.sin_addr.s_addr = inet_addr(p_IpAddress);
   serv_addr.sin_port = htons(p_Port);

   if (0 > connect(g_SocFd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)))
   {
      g_Logger.log(LOG_LEVEL_CRITICAL, "%s <Line:%d> Failed to Connect [SockFd:%d,ErrNo:%d,ErrMsg:%s]",
            __FUNCTION__, __LINE__, g_SocFd, errno, strerror(errno));
      close(g_SocFd);
      g_SocFd = -1;
      return false;
   }
   g_ConnectionFlag = true;
   return true;
}

int RecvAll(int &s, TEXT *buf, UINT32 len)
{
   unsigned long total = 0;
   int bytesleft = len;
   int n;
   int l_Count = 0;
   while(total < len)
   {
      if(0>s)
         return -1;

      n = recv(s, buf+total, bytesleft,0);
      int l_errno = errno;
      if (n <= 0)
      {
         if(EINTR == errno)
         {

            if((++l_Count) == 5)
            {
               
               close(s);
               return 0;
            }
            if(true == g_CloseConnection)
            {
               cout<<"failing recv for Graceful exit.\n";
				return 0;
            }
            continue;
         }
    
         close(s);
         return n;
      }
      total += n;
      bytesleft -= n;
	  }
   len = total; // return number actually sent here
   return len;
}


bool ReadMessage()
{
while(1)	{
	fd_set l_FdSet;
   int l_MaxFd = -1;
   struct timeval tv;

   FD_ZERO(&l_FdSet);
   
   if((true == g_ConnectionFlag))
   {
      FD_SET(g_SocFd, &l_FdSet);
      if(g_SocFd > l_MaxFd)
         l_MaxFd = g_SocFd;
   }

   if(-1 == l_MaxFd)
      return false;

   tv.tv_sec  = 5;
   tv.tv_usec = 0;
   if (-1 == select(l_MaxFd+1, &l_FdSet, 0, 0, &tv))
   {
      g_Logger.log(LOG_LEVEL_CRITICAL, "%s <Ln:%d> Error in select [ErrMsg:%s]", __FUNCTION__, __LINE__, strerror(errno));
      return false;
   }

	if(true == g-ConnectionFlag)
      {
         if(FD_ISSET(g_SocFd, &l_FdSet))
         {
            
            int num_charread;
            char l_LenStr[10];
            memset(l_LenStr, '\0', sizeof(l_LenStr));

            num_charread = RecvAll(g_SocFd, l_LenStr, 9);
			
            if (num_charread < 0)
            {
               close(g_SocFd);
               g_ConnectionFlag = false;
               return false;
            }
            else if (0 == num_charread)
            {
               close(g_SocFd);
               g_ConnectionFlag = false;
               return false;
            }
			else
			{
				cout<<"String : "<<l_LenStr<<endl;
			}

		}
		}		
}}

string g_ReqXml = "";
void SendMessage()
{
	while(1)
	{
	
	if(g_ReqXml.length() > 0)
	{
	
	if((g_ConnectionFlag == true) && (g_SocFd > 2))
                     {
                        if (-1 == send(g_SocFd, g_ReqXml.c_str(), g_ReqXml.length(), 0))
                        {
                           
                           g_Logger.log(LOG_LEVEL_WARNING, "%s <Ln:%d> Again pushed in Queue, due to sending failed.", __FUNCTION__, __LINE__);
                           usleep(2000000);
                           return false;
                        }
						}
}}}

int main()
{
	Connect("123.123.123.123", 8970);
	thread_t t1(SendMessage);
	thread_t t2(ReadMessage);
	sleep(2);
	g_ReqXml = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	
	sleep(2);
	g_ReqXml = "FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	
	sleep(2);
	g_ReqXml = "EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	
	sleep(2);
	g_ReqXml = "DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	
	sleep(2);
	g_ReqXml = "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	
	sleep(2);
	g_ReqXml = "BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	sleep(2);
	getchar();
	}


What I have tried:

tried but only am getting invalid argument as a error code : 22 from network.
Posted
Comments
mathiv327 8-Feb-24 8:23am    
RecvAll <ln:3696> socket: 38 recv failed ret: 0 with [errno: 22, ErrMsg:Invalid argument]
This is the error am getting from envirnoment
Richard MacCutchan 8-Feb-24 8:38am    
Use the debugger to see which argument value is not valid.

1 solution

At first: Log all your close() calls. I guess there is one you do not want to be executed.

But common speaken: You need to debug it.
Because you wrote that it is some timing problem, you need to add timestamps in your debugs log.
The disconnect may also get triggered by the server you communicate with. Check whether is hasnt some disconnect timer or on inactivity activated.
 
Share this answer
 
Comments
Rick York 8-Feb-24 12:24pm    
The most common problem I have had with socket interfaces is disconnections from inactivity. I always have either a periodic status update message or I put a "PING" command in my interfaces now just to keep the connection alive and it simplifies things quite a bit. I still have re-connection logic because it happens but much more rarely now.
KarstenK 9-Feb-24 6:46am    
I like reconnecting more than pinging because means also energy for nothing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900