Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C, Sockets: Connection Refused error

Tags:

c

sockets

I've a data acquisition module from which I would like to collect data from the Ethernet port. I'm getting there in steps, currently I would like to just connect to server from a client. I've used Beej's guide to get the basic C code. But I just keep getting this connect error connect: Connection refused.

This is what I do:

  1. The network IP mentioned here is STATIC IP which I have configured.

  2. The port number is set to 50000 on Server side and from the client side I connect to this IP on the port 50000.

  3. I build and run the server side application and then try to connect to it by running a client side application.

One doubt about server side, server side application returns before I start the client side application, so should I keep it running (while(1);) so that I can connect to it from the client side?

What's going wrong am I forgetting something here? Help!

I'm pasting the very slightly modified (IP and port numbers are different) Beej's C code for Server side and Client side here:

Server.c

/*
** server.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>



int main(int argc, char *argv[])
{
    // code for a server waiting for connections
    // namely a stream socket on port 3490, on this host's IP
    // either IPv4 or IPv6.
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE; // use my IP address

    if ((rv = getaddrinfo(NULL, "50000", &hints, &servinfo)) != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }

    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) 
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
        p->ai_protocol)) == -1) 
        {
            perror("socket");
            continue;
        }
        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
        {
            close(sockfd);
            perror("bind");
            continue;
        }
        break; // if we get here, we must have connected successfully
    }

    if (p == NULL) 
    {
        // looped off the end of the list with no successful bind
        fprintf(stderr, "failed to bind socket\n");
        exit(2);
    }

    freeaddrinfo(servinfo); // all done with this structure

}

Client.c

/*
** client.c
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    // code for a client connecting to a server
    // namely a stream socket to www.example.com on port 80 (http)
    // either IPv4 or IPv6
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo("192.168.2.4", "50000", &hints, &servinfo)) != 0) 
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }
    // loop through all the results and connect to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next)
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
        p->ai_protocol)) == -1) 
        {
            perror("socket");

        continue;
        }
        if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) 
        {
            close(sockfd);
            perror("connect");
            continue;
        }
        break; // if we get here, we must have connected successfully
    }

    if (p == NULL)
    {
        // looped off the end of the list with no connection
        fprintf(stderr, "failed to connect\n");
        exit(2);
    }

    freeaddrinfo(servinfo); // all done with this structure

}
like image 483
HaggarTheHorrible Avatar asked Jul 16 '12 03:07

HaggarTheHorrible


People also ask

What is socket error connection refused?

10061 is a Connection Refused error sent to you by the server. You could not make a connection because the target machine actively refused it. The most common cause is a misconfigured server, full server, or incorrect Port specified by the client.

Why does socket connection fail?

In short, 'failed to connect socket connection timed out error' can occur due to wrong SMTP host or port settings, ISP firewall blocks and more.


1 Answers

Your server code is missing listen() and accept() code to "wait" for a connection by calling listen() and then performing an accept() to accept new connections. Doesn't the example you are using show how to do that? Typically you will also fork a new thread for each new connection.

See http://www.linuxhowtos.org/C_C++/socket.htm for more information.

Here's a link to a more complete implementation: http://www.thegeekstuff.com/2011/12/c-socket-programming/

like image 107
HeatfanJohn Avatar answered Sep 29 '22 16:09

HeatfanJohn