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 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.