TCP Client-Server "bad address" error (in C)
<p>Although it seems to be correctly implemented, <strong>it keeps on returning me <em>ERROR</em> when I establish a connection using the loopback address</strong>(127.0.0.1).</p>

<p>In addition to a simple TCP Client/Server connection, I have added an additional case:</p>

<p>If the client tries to send data but <strong>finds the connection closed</strong>, it is closed too. I perform it by <strong>checking if received data is equal to 0 (recv)</strong>.</p>

<p><strong>Given error</strong>:</p>

<pre><code>CLIENT:
Welcome to the Client mode
Please, enter the Server's IP Address and Port (eg. 192.128.192.0 1320) 
127.0.0.1 2700
Connected to the server. Now you can send messages
Please, enter a message. Enter "FINISH" if you want to finish the connection
ECHO 
client: connection closed -&gt;: Success
 (1 bytes)Closing the connection 

SERVER:
Hello and welcome to the Server mode
Please, enter the Server's Port (eg. 1320) 
2700
Server socket successfully configured
Server listening [Clients allowed: 5]
server: accept error: Bad address
</code></pre>

<p><strong>Client</strong> implementation:</p>

<pre><code>#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;

#include &lt;netinet/in.h&gt;
#include &lt;netinet/ip.h&gt;

/**

 struct sockaddr{
     uint8_t sa_len; // struct length
     sa_family_t sa_family; //protocol family: AF_XXX
     char sa_data[8]; //socket addr
 }

 */

//void notConnected();
int main(){

    struct sockaddr_in  serv_addr;  //port + ip_addr
    int                 my_socket, tcp_port;
    char                serv_host_addr[30];

    char                buffer[1024], inbuff[1024];
    int                 io_buffer;

    printf("Welcome to the Client mode\n");

    //CONFIGURING THE CONNECTION
    my_socket = socket(AF_INET, SOCK_STREAM, 0);//(2)
    if(my_socket &lt; 0){
        perror("client: socket() error -&gt;");
        exit(EXIT_FAILURE);
    }

    bzero(&amp;serv_addr, sizeof(serv_addr));//(4)
    printf("Please, enter the Server's IP Address and Port (eg. 192.128.192.0 1320) \n");
    scanf("%s %d", serv_host_addr, &amp;tcp_port);//(1)


    serv_addr.sin_family = AF_INET ;
    serv_addr.sin_port = htons(tcp_port);
    if(inet_pton(AF_INET,serv_host_addr,&amp;serv_addr.sin_addr) &lt; 1){
        perror("client: inet_pton() error -&gt;");
        exit(EXIT_FAILURE);
    }

    if((connect(my_socket, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr) )) &lt; 0)//(5)
    {
        perror("client: connect() error -&gt;");
        exit(EXIT_FAILURE);
    }


    //ONCE CONNECTED, START THE SENDING/RECEIVING
    printf("Connected to the server. Now you can send messages\n");
    bzero(&amp;buffer, sizeof(buffer));

    while(strcmp(buffer, "OK\n") != 0){

        printf("Please, enter a message. Enter \"FINISH\" if you want to finish the connection\n");//(3)
        bzero(&amp;buffer, sizeof(buffer));
        fgets(buffer, sizeof(buffer), stdin);

        io_buffer = send(my_socket, buffer, strlen(buffer),0);//(6)
        if(io_buffer &lt; 0){
            perror("client: send() error -&gt;");
            exit(EXIT_FAILURE);
        }
        printf("ECHO %s (%d bytes)", buffer, io_buffer);


        //RECEIVE AND CHECK IF CONNECTION HAS BEEN CLOSED
        io_buffer = recv(my_socket, buffer, sizeof(buffer),0);
        if(io_buffer &lt; 0){
            perror("client: recv() error -&gt;");
            exit(EXIT_FAILURE);
        }
        else if(io_buffer == 0){    //THIS IS SERVER IS CLOSED
            perror("client: connection closed -&gt;");
            break;
        }
        printf("ECHO %s (%d bytes)", buffer, io_buffer);

    }

    printf("Closing the connection \n");
    for(int i=0; i &lt; 5; i++){
        printf(". ");
        usleep(500000);
    }
    close(my_socket);
}
</code></pre>

<p><strong>Server</strong> implementation:</p>

<pre><code>#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;

#define LISTENQ 5

int main()
{
    struct sockaddr_in  cli_addr, serv_addr;
    char                buffer[1024];
    int                 serv_socket, cli_socket, clilen, io_buffer;
    int                 tcp_port;

printf("Hello and welcome to the Server mode\n");

//  ASKING FOR PORT NUMBER
    if((serv_socket = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0){
        perror("server: can't open stream socket");
        exit(EXIT_FAILURE);
    }

printf("Please, enter the Server's Port (eg. 1320) \n");
    scanf("%d", &amp;tcp_port);

//  CONFIGURING THE CONNECTION
    serv_addr.sin_family        = AF_INET;
    serv_addr.sin_port          = htons(tcp_port);
    serv_addr.sin_addr.s_addr   = htonl(INADDR_ANY);

//  ASSIGNING A NAME TO THE SOCKET
    if(bind(serv_socket,(struct sockaddr *) &amp;serv_addr,sizeof(serv_addr)) &lt; 0){
        perror("server: can't assign a name to the socket");
        exit(EXIT_FAILURE);
    }
printf("Server socket successfully configured\n");

printf("Server listening [Clients allowed: %d]\n", LISTENQ);
    if(listen(serv_socket, LISTENQ) &lt; 0)
    {
        perror("server: fail to listen network");
        exit(EXIT_FAILURE);
    }

//  READ &amp; WRITE STREAM
    while(1){
        //returns a file descriptor for the client
        cli_socket = accept(serv_socket,(struct sockaddr *) &amp;cli_addr,(socklen_t *) sizeof(cli_addr));

        if(cli_socket &lt; 0){
            perror("server: accept error");
            exit(EXIT_FAILURE);
        }
        printf("Server successfully connected to Client\n");

        while(1)
        {

            if ((io_buffer=recv(cli_socket,buffer,sizeof(buffer),0))&lt;0){
                perror("ERROR: recv");
                exit(EXIT_FAILURE);
            }
            printf("\"%s\" received from client", buffer);

            if(strcmp(buffer, "FINISH") == 0)
            {
                break;
            }

            if ((io_buffer=send(cli_socket,buffer,strlen(buffer),0))!=strlen(buffer)){
                perror("ERROR: send");
                exit(EXIT_FAILURE);
            }

            bzero(buffer, sizeof(buffer));
        }

        strcpy(buffer, "OK");
        if ((io_buffer=send(cli_socket, buffer, strlen(buffer), 0)) != strlen(buffer)){
            perror("ERROR: send");
            exit(EXIT_FAILURE);
        }
        printf("\"OK\" message sent to the Client.\n");
        printf("Closing the connection \n");
        for(int i=0; i &lt; 5; i++)
        {
            printf(". ");
            usleep(500000);
        }
        close(cli_socket);
    }
}
</code></pre>