Am trying to connect to local machine on port 9102
There is no server application listening on port 9102.
if (connect(fd,(struct sockaddr *)&addr, (socklen_t)sizeof(struct sockaddr)) == -1)
return -1;
cout<<"Connected ,ERRNO:"<<errno<<", txt:"<<strerror(errno);
netstat -na |grep 9102
tcp 0 0 192.168.151.42:9102 192.168.151.42:9102 ESTABLISHED
And the o/p
Connected ,ERRNO:42, txt:No message of desired type
Any suggestion?
its seems that the cout is called every time, if the connection is established/not estatblished,
please correct me, if i wrongly understood the problem
pludi
November 26, 2009, 1:28pm
4
What's the output of netstat if your program is not running? What's in addr? Is there any call before the connect that could be failing? Because according to the connect man page, it shouldn't fail with ENOMSG (which is what you're getting).
there is no errno 42
what i am pointing at is that why somebody need to print the errno, after making sure that there is no error, everything is clear from the output.
if you just make errno=0 before calling connect, you might able to know there is no error happening technically
Ok ,this is the reduced version of my actual code to simulate this error
class Connection
{
int fd,ctr;
struct hostent *he;
struct sockaddr_in host;
int portno;
public:
bool Connect();
Connection(int port);
};
Connection::Connection(int _port):ctr(0)
{
portno = _port;
if ((he=gethostbyname("192.168.151.42")) == NULL)
throw runtime_error("gethostbyname failed"+string(strerror(errno)));
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
throw runtime_error("socket creation failed"+string(strerror(errno)));
host.sin_family = AF_INET;
host.sin_port = htons(portno);
host.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(host.sin_zero),'\0', 8);
}
bool Connection::Connect()
{
ctr++;
if (connect(fd,(struct sockaddr *)&host, (socklen_t)sizeof(struct sockaddr)) == -1)
return false;
cout<<"Connected after "<<ctr<<"try ,New Sd:"<<fd<<"\n";
cout<<"Just Checking-ERRNO:"<<errno<<", txt:"<<strerror(errno)<<"\n";
return true;
}
int main()
{
Connection cobj(9816);
while(cobj.Connect()==false)
usleep(1000);
sleep(20);
}
And the output
>./a.out
Connected after 6724try ,New Sd:3
Just Checking-ERRNO:111, txt:Connection refused
Netstat from another terminal
>netstat -na |grep 9816
tcp 0 0 192.168.151.42:9816 192.168.151.42:9816 ESTABLISHED