Reputation: 1908
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
}
}
Upvotes: 2
Views: 4701
Reputation: 487725
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?).
Upvotes: 3