I am openning a udp blocking socket on an ubuntu machine, it goes successful (no errors returned).
When sending data to that 开发者_StackOverflow中文版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
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.
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?
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)?
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.
精彩评论