Solution to the problem of Connection refused

Source: Internet
Author: User

I. Background

System: CentOS7 64 bits (run on virtual machine VMware)

Virtual NIC ip:192.168.137.129

Ports in use: 9999

Sockets API in blocking mode (default)

Second, the problem description

On Terminal 1, when the server is started, it can be monitored successfully, that is, the server is waiting for the client to connect. However, on Terminal 2, when the client is started, an error--connection refused is returned (this information is exported through strerror (errno), strerror () in <string.h>, errno in < errno.h>).

The service-side portion of the code. As follows:

. . .

int sockfd;
if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0)
{
   cout << strerror (errno) << Endl;
   return-1;
}

struct sockaddr_in localaddr;
Bzero (&localaddr, sizeof (LOCALADDR));
localaddr.sin_family = af_inet;
Localaddr.sin_port = htonl (9999);
if (Inet_pton (af_inet, "192.168.137.129", &localaddr.sin_addr) <= 0)
{close
   (SOCKFD);
   cout << strerror (errno) << Endl;
   return-1;
}

if (Bind (SOCKFD, (struct sockaddr *) &localaddr, sizeof (LOCALADDR)) < 0)
{close
   (SOCKFD);
   cout << strerror (errno) << Endl;
   return-1;
}

if (Listen (SOCKFD) < 0) {close
   (SOCKFD);
   cout << strerror (errno) << Endl;
   return-1;
}

. . .


Client part code, as follows:

. . .

int sockfd;
if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0)
{
   cout << strerror (errno) << Endl;
   return-1;
}

struct sockaddr_in srvaddr;
Bzero (&srvaddr, sizeof (SRVADDR));
srvaddr.sin_family = af_inet;
Srvaddr.sin_port = htonl (9999);
if (Inet_pton (af_inet, "192.168.137.129", &srvaddr.sin_addr) <= 0)
{close
   (SOCKFD);
   cout << strerror (errno) << Endl;
   return-1;
}

If Connect (sockfd, (struct sockaddr *) &srvaddr, sizeof (SRVADDR)) < 0)
{close
   (SOCKFD);
   cout << strerror (errno) << Endl;
   return-1;
}

. . .


iii. problem-solving

Replace the HTONL (9999) with Hons (9999), which is implemented on the server and the client, respectively.

Iv. Causes of the problem

For the service side: the original source code of the service end, the port value is set to HTONL (9999). In fact, the Sockaddr_in.sin_port is a short integer, accounting for 16 bits, and the CENTOS7 system uses a byte sequence that is small-endian. Therefore, the actual result of HTONL (9999) is a network byte order of 0 rather than 9999. Because the port's actual value is 0, this causes the kernel to select a temporary port for the server when the service side calls the function listen, which is usually not the same as the port we specify.

For clients: In fact, the port number requested by the client is 0 (reserved port). When the client calls the function connect, the server side does not have a service waiting for the connection on the port requested by the client, so connect finally goes wrong, and the reason for the error is as we can see: Connection refused.

v. Summary

possible causes for connection refused: The server does not have a service on the port requested by the client waiting for a connection.

Perhaps the service end was not started at all; however, the port requested by the client is inconsistent with the correct port on which the service is listening (the port requested by the client is not necessarily consistent with what we specify, and the port on which the service is listening is not necessarily consistent with what we have specified, which is what is discussed in this article). Of course, do not rule out the existence of other circumstances will lead to this error, but the author has not met or heard it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.