Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7695053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:27:32+00:00 2026-05-31T21:27:32+00:00

I send data between sockets in a C-application. I first bind the ‘receiver’ to

  • 0

I send data between sockets in a C-application. I first bind the ‘receiver’ to listen to a certain port at 127.0.0.1 (localhost) (blocking – in a separate thread). I then start sending data to this port.
I get the following print-out from the program:

*** Created server socket good. Server socket is 4 
(..)
 port nr 4793 & ip 127.0.0.1***Bind succeed.. 
Waiting for recvfrom() to complete... 
waiting to receive msg*** Start sending messages.. 
sendto() successs 
sendto() successs 
(..)
sendto() successs 

Running Wireshark with the loopback-interface, I notice that the packets are sent to the correct destination address.
However, I get the error ‘Destination unreachable’ (‘Port unreachable’)’. I get the same error despite turning off the firewall.

Is there an error in the code; and how do I resolve this?
Below follows excerpts from the code:

Some definitions in header-file:

#define  PORT_NUM   4793    // Arbitrary port number for the server
#define  IP_ADDR      "127.0.0.1" // IP address of server1 (*** HARDWIRED ***)
#define  SYMBOL_SIZE 1024 //todo..on a different position also

Program initiating sender and receiver:

    ret = init_sender();
    if (ret != OF_STATUS_OK) {
        OF_PRINT_ERROR(("ERROR, init_sender() failed\n"))
        goto error;
    }

     // >>> Step #3 <<<
      // Wait to receive a message from client..Since receiver is waiting-do this in separate thread.
    pthread_t thread1;
    int  iret1;
    char *message1 = "Thread 1";

    iret1 = pthread_create( &thread1, NULL, init_receiver, (void*) message1);

    ret = encode(); //this sends messages
    if (ret != OF_STATUS_OK) {
        OF_PRINT_ERROR(("ERROR, encode() failed\n"))
        goto error;
    }


    if (ret != OF_STATUS_OK) {
        OF_PRINT_ERROR(("ERROR, init_receiver() failed\n"))
        goto error;
    } //todo..ok after init_receiver?
    ret = init_tx_simulator();  /* must be done after init_sender */
    if (ret != OF_STATUS_OK) {
        OF_PRINT_ERROR(("ERROR, init_tx_simulator() failed\n"))
        goto error;
    }

    print_params();
    //print_rx_stats();
#endif /* OF_DEBUG */
#endif

    pthread_join (thread1, NULL); //We want the receiver thread to be finished before finishing it..

Sender:

of_status_t encode (void)

{

      // Create a socket
      //   - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
      client_s = socket(AF_INET, SOCK_DGRAM, 0);
      if (client_s < 0)
      {
        printf("*** ERROR - socket() failed \n");
        exit(-1);
      }
        printf("*** Sender -- socket created \n");

            printf("*** Start sending messages.. \n");   



  // >>> Step #2 <<<
      // Fill-in server1 socket's address information
      server_addr.sin_family = AF_INET;                 // Address family to use
      server_addr.sin_port = htons(PORT_NUM);           // Port num to use
      server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use 


   //n is number of symbols to sent -- repair + original..
int j= 0;
   for (j=0; j < 10 ; j++ ) 
       {
       retcode = sendto(client_s, (const void*) encoding_symbols_tab[j],  symbol_size, 0,
      (struct sockaddr *)&server_addr, sizeof(server_addr) );

      if (retcode < 0)
      {
       // printf("*** ERROR - sendto() failed.. retcode is %d  \n", retcode);
        printf ("errno is %s",strerror (errno));        
        exit(-1);
      }
      else
       printf("sendto() successs \n");

       }

 retcode = close(client_s);
  if (retcode < 0)
  {
    printf("*** ERROR - close() failed \n");
    exit(-1);

Receiver:

    init_receive () {


          // >>> Step #1 <<<
          // Create a socket
          //   - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
          server_s = socket(AF_INET, SOCK_DGRAM, 0);
          if (server_s < 0)
          {
            printf("*** ERROR - socket() failed \n");
            exit(-1);
          }
          else
            printf("*** Created server socket good. Server socket is %d \n", server_s);


          // >>> Step #2 <<<
          // Fill-in my socket's address information
          server_addr.sin_family = AF_INET;                 // Address family to use
          server_addr.sin_port = htons(PORT_NUM);           // Port number to use
          server_addr.sin_addr.s_addr = htonl(IP_ADDR);  // Listen on this IP..
            printf("***We have a server socket; and now we will try to bind it with the IP_ADDR-local host -- that we sent.. \n port nr %d & ip %s", PORT_NUM, IP_ADDR);

          retcode = bind(server_s, (struct sockaddr *)&server_addr,
            sizeof(server_addr));

          if (retcode < 0)
          {
            printf("*** ERROR - bind() failed \n");
            exit(-1);
          }
        else
            printf("***Bind succeed.. \n");


        waitToReceiveMessageFromClient (); //call it and wait to receive messages.

        return OF_STATUS_OK;

    no_mem:
        return OF_STATUS_ERROR;
    }

waitToReeiveMessageFromClient () {

int
waitToReceiveMessageFromClient ( void )
{
      printf("Waiting for recvfrom() to complete... \n");
      addr_len = sizeof(client_addr);
     encoding_symbols_tab = (void*) calloc(MAX_N, sizeof(void*));
    int i = 0;
    //Run until we have finished all messages from the Sender.

      while (numberMessagesReceivedCounter <  tot_nb_source_symbols + tot_nb_repair_symbols )
        {
        printf ("waiting to receive msg");

        encoding_symbols_tab[i] = (void*)calloc(1, SYMBOL_SIZE); //each
//change to non-blocking
//if(fcntl(server_s, F_SETFL, fcntl(server_s, F_GETFL) | O_NONBLOCK) < 0) {
  //  printf ("error for the moment");
//}

          retcode = recvfrom(server_s, in_buf, sizeof(in_buf), 0,
            (struct sockaddr *)&client_addr, &addr_len);

          if (retcode < 0)
          {
            fprintf (stderr,"errno is %d", errno);
            exit(-1);
          }
          else
            { //copy all symbols to its right location from received message.
        printf("*** Received message.. \n");

            int k= 0;
            for (k= 0; k < sizeof (in_buf) ; k++ )
            {
            encoding_symbols_tab[numberMessagesReceivedCounter][k] = (char*) in_buf[k];
            }
            numberMessagesReceivedCounter++;

            //Finished ready to do decoding.
            if (numberMessagesReceivedCounter ==  tot_nb_source_symbols + tot_nb_repair_symbols )
             {
            receive_and_decode ();
            break;
            }

            }
        }
     //add to buffer... numberMessagesReceivedCounter

}



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:27:33+00:00Added an answer on May 31, 2026 at 9:27 pm

    This line (in the sender):

    server_addr.sin_addr.s_addr = inet_addr(IP_ADDR);
    

    is right.

    This line (in the receiver):

    server_addr.sin_addr.s_addr = htonl(IP_ADDR);
    

    is wrong. It’s a little surprising it even compiled cleanly (or did it?).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am developing application which send and receive data between two computers but there
Is it a good idea to use sockets to send data between two servers,
I am using Csharp tcp sockets to send data between a client and server.
I'm try to send and get data using sockets between Adobe flash client and
I need to send data to a hardware device over serial port. I'm using
I'm writing a networking application that uses ASIO/UDP to send and receive between a
I have been sending binary data between applications lots of times over TCP sockets
I have a distributed java application and I want to send the database data
I am creating an server to send data to many persistent sockets. I have
I would like to send data between two ethernet interfaces that are connected with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.