views:

54

answers:

4

Hello,

I am openning a udp blocking socket on an ubuntu machine, it goes successful (no errors returned).

When sending data to that machine and port from another machine the receive doesnt breaches and in a wireshark sniffer I see and Icmp error "port unreachable".

I though it might be an iptables problem and opened the port for accept.

Any other suggestions how to debug this?

Thanks.

Timmy

+1  A: 

You should show a minimal test case.

Do you see your process in

sudo netstat -4lp

? What local address is its socket bound to (as reported by netstat)?

Roman Cheplyaka
Tried this and in the list returned the problematic socket doesn't seem to appear. However I do see others socket I openned in the program.
Timmy
Perhaps bind() failed. Do you check its return value? errno then should give you a hint on what's wrong.(Wild guess) Did you try to bind to the low port without root privileges?Please, show us the code.
Roman Cheplyaka
Your answer helped spotting the bug. The socket was opened and then closed because of an uninitiated socket descriptor. Many Thanks!
Timmy
+2  A: 

I usually use netcat to figure out if the problem comes from the network/firewall or from my own code

try running a test server with netcat : eg.

nc -l -u -p 9999 

will open and listen an udp socket, port 9999.

Now you can try to send a packet from the same or from another computer using

nc -u <ipaddress> 9999

Then type something and see if it reaches the first computer.

There are a lot of other cool stuffs in netcat, have a look on the manual.

Ben
Great idea, Thanks! did it and I see the incoming data, so it's probably something with the socket. Question is what? since no error is returned when openning and binding it...
Timmy
hum try to post peaces of your code
Ben
+1  A: 

Are you using bind() to correctly bind the socket to the local port?

Did you remember to pass the local port number through htons() to convert it to network byte order?

What value did bind() return?

caf
A: 

Try this simple server and see if it works for you:

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFSZ 4096
#define PORTNUM 1099
char buffer[BUFSZ];

int main( int argc, char* argv[] )
{
    int fd;
    struct sockaddr_in servaddr, cliaddr;
    socklen_t clilen = sizeof( cliaddr );
    ssize_t nread;

    if (( fd = socket( AF_INET, SOCK_DGRAM, 0 )) == -1 )
        err( 1, "socket" );

    bzero( &cliaddr, sizeof( cliaddr ));
    bzero( &servaddr, sizeof( servaddr ));

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
    servaddr.sin_port = htons( PORTNUM );

    if ( bind( fd, ( struct sockaddr* )&servaddr, sizeof( servaddr )) == -1 )
        err( 1, "bind" );

    printf( "bound to %s:%d\n", inet_ntoa( servaddr.sin_addr ),
        ntohs( servaddr.sin_port ));

    while (( nread = recvfrom( fd, buffer, BUFSZ, 0,
        ( struct sockaddr* )&cliaddr, &clilen )) != -1 )
    {
        printf( "received %lu bytes from %s:%d\n", nread,
            inet_ntoa( cliaddr.sin_addr ),
            ntohs( cliaddr.sin_port ));
    }

    return 1;
}

See if all the required steps are there in your code.

Nikolai N Fetissov