C, Sockets: Connection Rejected Error

I have a data acquisition module from which I would like to collect data from an Ethernet port. I get the steps there , for now I would just like to connect to the server from the client. I used the Beej manual to get the base C code. But I just keep getting this connect: Connection refused. error connect: Connection refused.

This is what I do:

  • This is the IP address of the STATIC IP .

  • The port number is set to 50,000 on the server side, and on the client side I connect to this IP address on port 50,000.

  • I create and run the application on the server side, and then try to connect to it by running the application on the client side.

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

What happens, did I forget something? Help!

I insert very slightly changed (IP numbers and port numbers are different). Beej 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 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 } 
+7
source share
4 answers

There is no listen() and accept() code in the server code to "wait" for the connection by calling the listen () function and then executing accept() to accept new connections. Doesn't the example you use show how to do this? Typically, you also fork a new thread for each new connection.

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

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

+6
source

Yes, you need to save the server program. In your server program, you created a socket using socket() and tied to the bind() address, now you need to start listening for incoming connections. This is done by calling listen() . When the socket listens for incoming connections, you must use the accept() call to actually accept the connection and get the socket to communicate with this particular client.

As a quick example, after freeaddrinfo you can add the following code:

 listen(sockfd, 8); /* allow up to 8 pending connections, ie created but not accepted */ while (1) { int clientfd; struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(struct sockaddr_in); clientfd = accept(sockfd, &client_addr, &client_addr_len); /* communicate with client by send()/recv() or write()/read() on clientfd */ close(clientfd); } 

This code has a flaw that is processed by only one client at a time. There are several ways to handle multiple simultaneous clients: multiple processes using fork() , multiple threads, or polling. Each of these approaches, in my opinion, is beyond the scope of this issue.

+3
source

Look at your Server.c file: it doesn't call listen () at all!
If the target server does not listen on a specific port, it will return an RST packet when it receives a SYN packet from the client, so connect () will return with "Connection reject".

The usual server-side function flow is socket -> bind -> listen -> accept:

 getaddrinfo(); socket(); bind(); listen(); /* accept() goes here */ 

Refer to http://beej.us/guide/bgnet/output/html/multipage/syscalls.html#listen

+1
source

I got the error message "connection failed" due to the lack of a remote host entry in the / etc / hosts file. Entrance must be attended by both sides. The client machines / etc / hosts should have a server machine entry and vice versa in the following template.

<ip address> <hostname with domain> <alias hostname>

This solved the error that was in the getaddrinfo () function.

0
source

All Articles