Python Client - C++ Server Connection Refused Error
<p>So I'm trying to run a client-server application using Python as the client and C++ as the server. </p>

<p>The two are on separate machines - right now I'm running the python client on my macbook pro and the server is a linux desktop I have. </p>

<p>I am able to ssh into my linux box with as following: </p>

<pre><code>ssh username@address.hopto.org
</code></pre>

<p>And then entering my password </p>

<p>Despite being able to ssh with no problem, I cannot get a port/socket connection to work. </p>

<p>Here is my python code: </p>

<pre><code>import socket, time

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('username@address.hopto.org', 9999))
print client.send('Hello world!'), 'bytes sent.'
time.sleep(0.2)
print 'Received message:', client.recv(1024)
</code></pre>

<p>And here is my C++ server (which has been borrowed from this site: <a href="http://www.cs.utah.edu/~swalton/listings/sockets/programs/part2/chap6/simple-server.c" rel="nofollow">http://www.cs.utah.edu/~swalton/listings/sockets/programs/part2/chap6/simple-server.c</a> )</p>

<pre><code>#define MY_PORT     9999
#define MAXBUF      1024

int main(int Count, char *Strings[])
{   int sockfd;
    struct sockaddr_in self;
    char buffer[MAXBUF];

    /*---Create streaming socket---*/
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0 )
    {
        perror("Socket");
        exit(errno);
    }

    /*---Initialize address/port structure---*/
    bzero(&amp;self, sizeof(self));
    self.sin_family = AF_INET;
    self.sin_port = htons(MY_PORT);
    self.sin_addr.s_addr = INADDR_ANY;

    /*---Assign a port number to the socket---*/
    if ( bind(sockfd, (struct sockaddr*)&amp;self, sizeof(self)) != 0 )
    {
        perror("socket--bind");
        exit(errno);
    }

    /*---Make it a "listening socket"---*/
    if ( listen(sockfd, 20) != 0 )
    {
        perror("socket--listen");
        exit(errno);
    }

    /*---Forever... ---*/
    while (1)
    {   
        int clientfd;
        struct sockaddr_in client_addr;
        int addrlen=sizeof(client_addr);

        /*---accept a connection (creating a data pipe)---*/
        clientfd = accept(sockfd, (struct sockaddr*)&amp;client_addr, &amp;addrlen);
        printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

        /*---Echo back anything sent---*/
        send(clientfd, buffer, recv(clientfd, buffer, MAXBUF, 0), 0);

        /*---Close data connection---*/
        close(clientfd);
    }

    /*---Clean up (should never get here!)---*/
    close(sockfd);
    return 0;
}
</code></pre>

<p>I've actually tried two separate things for the python/client side: </p>

<p>If I write the following: </p>

<pre><code>client.connect(('username@address.hopto.org', 9999))
</code></pre>

<p>I get this error:
<strong>socket.gaierror: [Errno 8] nodename nor servname provided, or not known</strong></p>

<p>However if I do: </p>

<pre><code>client.connect(('address.hopto.org', 9999))
</code></pre>

<p>without the username part...I get this error: </p>

<pre><code>socket.error: [Errno 61] Connection refused
</code></pre>

<p>I'm thinking it's because I have a password on my Linux box that is "blocking" the connection but I don't know how to include it here. </p>

<p>I'm a beginner at networking code so a lot of this is very unfamiliar to me. </p>

<p>Connection refused usually means the host is reachable but the port is not opened.<br>
May be your request is block by firewall or your server is not running correctly. </p>

<p>To avoid the firewall issue. I suggest u can run your python script on same box as the server and use 127.0.0.1 to connect it first.</p>

python c++ linux sockets networking