0

I am quite new to C & Linux and I tried to setup a TCP socket server for Data exchange with a C-Code I compiled and executed on a Ubuntu System.

From a Tutorial I copied the following code (see below) and it worked to start the server and receive data (time & date) with a client code (on the same machine for test purpose).

Then I restarted the Ubuntu machine and since then I can't start the server anymore.

I then added exception handling to the code below and it throws "unable to bind" with errorno 22, which means "invalid argument"? This doesn't make sense to me, as the same code worked before quite good.

I assumed that the "old" socket is in a time-wait state or not closed yet, but I checked all connections with "ss -all state xxx" for all different states and everything seems alright.

Also tried to use different ports and codes - same problem.

Hope anyone can help me with this problem as I don't know what else to try.

// C-Code Server
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

int main(int argc, char *argv[])
{

    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr; 

    char sendBuff[1025];
    time_t ticks; 

    //Open a socket
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff)); 
    if (listenfd == -1) {
        printf("Error: unable to open a socket\n");
        exit(1);
    }

    //Create an Adress
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htons(INADDR_ANY);
    serv_addr.sin_port = htons(1234); 


    //Macht schon benutzte Adresse mit SO_REUSEADDR nutzbar
    int opt = 1;
    if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt))<0) {
        perror("setsockopt");exit(EXIT_FAILURE);
    }
    if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEPORT, (char *)&opt, sizeof(opt))<0)   {
        perror("setsockopt");exit(EXIT_FAILURE);
    }


    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    if ((bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) == -1) {
        printf("Error: unable to bind\n");
        printf("Error code: %d\n", errno);
        exit(1);
    }



    listen(listenfd, 10); 

    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 

        if (connfd == -1) {
            printf("Error: unable to accept connections\n");
            printf("Error code: %d\n", errno);
            exit(1);
        }



        ticks = time(NULL);
        snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
        write(connfd, sendBuff, strlen(sendBuff)); 

        close(connfd);
        sleep(1);
     } 
}
0

bind(2) tells:

EINVAL The socket is already bound to an address.

Indeed, you're calling twice bind() on the same socket:

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    if ((bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) == -1) {
        printf("Error: unable to bind\n");
        printf("Error code: %d\n", errno);
        exit(1);
    }

Once the first bind() was removed I could receive the date when connecting.

|improve this answer|||||
  • Thank you very much for your quick response. This is one of the exceptions I added myself - seems to be wrong then. But the original version only contains the first bind command and still doesn't work/shows the same error. The original code can be found on "thegeekstuff.com/2011/12/c-socket-programming" (hope I am allowed to post hyperlinks here). I will try this code again without the first command and let you know if it works - thanks a lot. – timosmd Oct 23 '19 at 17:26
  • Well I already ran it, and just removing the one line made your code work (on linux). To be honest I didn't see it alone, but it became obvious with strace. – A.B Oct 23 '19 at 17:35
  • Thank you very much - now it works again. – timosmd Oct 25 '19 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.