JmRag
JmRag

Reputation: 1459

Loopback example using INADDR_LOOPBACK does not work

I am trying to set a loopback socket in C but nothing works. I'm trying to make a function that opens a socket with the loopback address ,send data to socket and from another function read the data but nothing works. I believe that I don't know how to use the functions related to connections. Here is what I accomplished so far:

#include <sys/wait.h>       
#include <sys/types.h>       
#include <sys/socket.h>     
#include <netinet/in.h>       
#include <netdb.h>           
#include <unistd.h>             
#include <stdlib.h>         
#include <ctype.h>          
#include <signal.h>         
#include <iostream>
#include <cerrno>
#include <pthread.h>

int internal_s;

void function1(){
    if ((internal_s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        perror_exit("socket");

    /* Find server address */
    struct sockaddr_in loopback;
    struct sockaddr *serverptr = (struct sockaddr*)&loopback; 

    /*Convert port number to integer*/        
    loopback.sin_family = AF_INET;       /* Internet domain */
    loopback.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
    loopback.sin_port=htons(10000);

    /* Initiate connection */
    if (bind(internal_s,serverptr, sizeof(loopback))<0)
        perro("bind");

    int test=1;
    err=write(internal_s,&test,sizeof(int));
    if(err<0)
        perror(write);
}

void Open_Internal_sock(int socket_s){
    struct sockaddr_in loopback;
    struct sockaddr *serverptr = (struct sockaddr*)&loopback; 

    /*Convert port number to integer*/        
    loopback.sin_family = AF_INET;       /* Internet domain */
    loopback.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
    loopback.sin_port=htons(10000);

    /* Initiate connection */
    if (bind(socket_s,serverptr, sizeof(loopback))<0)
        perror("bind");//Invalid argument
    int test;
    if(read(socket_s,&test,sizeof(int))<0)
        perror("read");//herer it prints:Transport endpoint is not connected
}

int main(){
    function1(i);
    Open_Internal_sock(internal_s);
}

Upvotes: 6

Views: 15767

Answers (2)

cam.b
cam.b

Reputation: 157

This doesn't address the content of this post, but if you're here from the title of this question only, I made the silly mistake of trying to do:

sin_addr.s_addr = INADDR_LOOPBACK;

This is wrong and should be:

sin_addr.s_addr = htonl(INADDR_LOOPBACK);

or

sin_addr.s_addr = inet_addr("127.0.0.1");

Upvotes: 0

thuovila
thuovila

Reputation: 2030

In short, the client(sender, "writer") needs to call connect() and the server(listener, receiver, "reader") needs to cal listen() and accept().

The server and client also need separate threads of execution, because some of the socket operations block and would cause a single thread of execution to stop forever. Easiest is probably to make a server.c and client.c as separate programs.

Additionally, try compiling your code with warnings enabled, e.g., gcc -Wall . There are now quite many errors, which the compiler can point out for you. For clearer messages, try clang instead of gcc as a compiler.

I suggest looking at http://kohala.com/start/unpv12e/unpv12e.tar.gz . Unpack with tar xzvf unpv12e.tar.gz and look at unpv12e/tcpcliserv/tcpcli01.c and unpv12e/tcpcliserv/tcpserv01.c . In case you are tempted to copy&paste, notice that the Capital letters in, e.g., Listen() need to be changed to lower case for the code to work without unpv headers. This change also removes all checks for errors, so put in your own error handling.

Upvotes: 7

Related Questions