Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
66.3k views
in Technique[技术] by (71.8m points)

c++ - Linux UDP Server unreachable from Window 7

I have the following configuration for my experiment.

  1. Wifi(Belkin) router connected to Internet.
  2. Laptop with Windows 7 OS
  3. Laptop with Ubuntu OS.

Experiment: When I connect both of my laptop to Wifi router it assigns DHCP IPs 192.168.2.2 to Linux & 192.168.2.3 to Win 7. Both of them can browse internet. I start a UDP server on my Linux machine with the following code.

int main(int argc, char *argv[]) {

    int sd, rc, n, cliLen, flags;
    struct sockaddr_in cliAddr, servAddr;
    char msg[MAX_MSG];

    //Create a socket
    sd=socket(AF_INET, SOCK_DGRAM, 0);
    if(sd<0){ printf("%s: cannot open socket 
",argv[0]); exit(1); }

    //Bind now to a port
    servAddr.sin_family = AF_INET;
    servAddr.sin_addr.s_addr = inet_addr("192.168.2.2");
    servAddr.sin_port = htons(9999);
    rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
    if(rc<0) {printf("%s: cannot bind port number %d 
", argv[0], 9999);exit(1);}

    //We are done ... Notify User
    printf("%s: waiting for data on port UDP %u
",argv[0],LOCAL_SERVER_PORT);

    //Server's Infinite Loop
    while(1)
    {
            memset(msg,0x0,MAX_MSG);//Sanity
            /* receive message */
            cliLen = sizeof(cliAddr);
            n = recvfrom(sd, msg, MAX_MSG, flags,(struct sockaddr *) &cliAddr, (socklen_t * )&cliLen);

            if(n<0){printf("%s: cannot receive data 
",argv[0]);continue;}

            //Received message
            printf("%s: from %s:UDP%u : %s 
", argv[0],inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),msg);

            sleep(1);

            //Sending back the data thus received
            sendto(sd,msg,n,flags,(struct sockaddr *)&cliAddr,cliLen);

    }//while
    return 0;
}

This code work well & I can receive the packet to the server when some local client on the Linux machine tries to contact my server.

PROBLEM : When I make the same client in Android AVD present in my windows 7 system I am unable to reach my server.

I thought may be that's firewall issue, so I removed the firewall & added by pass custom rules to the IP "192.168.2.2" as given in the following link. http://www.brighthub.com/computing/windows-platform/articles/40014.aspx# But it did not work. I thought that first I should try with raw java first then with AVD. Hence, I created a UDP client with Java code still I was not able to connect to server.

Then I thought let's try with raw C++ so that I would come to know exactly what is the problem. Following is the Visual Studio code which I implemented for the same.

#define  PORT_NUM       9999            // Port number used
#define  IP_ADDR        "192.168.2.2"   // IP address of server1 
#define  BUFFER_SIZE    4096

void main(void){

WORD wVersionRequested = MAKEWORD(2,2);       // Stuff for WSA functions
WSADATA wsaData;                              // Stuff for WSA functions

int                  client_s;        // Client socket descriptor
struct sockaddr_in   server_addr;     // Server Internet address
int                  addr_len;        // Internet address length
char                 out_buf[BUFFER_SIZE];   // Output buffer for data
char                 in_buf[BUFFER_SIZE];    // Input buffer for data
int                  retcode;         // Return code


// This stuff initializes winsock
WSAStartup(wVersionRequested, &wsaData);


client_s = socket(AF_INET, SOCK_DGRAM, 0);
if (client_s < 0){ printf("*** ERROR - socket() failed 
"); exit(-1);}

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

strcpy(out_buf, "Test message from CLIENT to SERVER");

retcode = sendto(client_s, out_buf, (strlen(out_buf) + 1), 0,(struct sockaddr *)&server_addr, sizeof(server_addr));

if (retcode < 0){printf("*** ERROR - sendto() failed 
");exit(-1);}

addr_len = sizeof(server_addr);
retcode = recvfrom(client_s, in_buf, sizeof(in_buf), 0,(struct sockaddr *)&server_addr, &addr_len);

if (retcode < 0){printf("*** ERROR - recvfrom() failed 
");exit(-1);}

printf("Received from server: %s 
", in_buf);

retcode = closesocket(client_s);
if (retcode < 0){ printf("*** ERROR - closesocket() failed 
");exit(-1);}

WSACleanup();
}

But it gives me error of destination unreachable.

To find out exactly what is going on at the packet level, I installed "Wireshark", on my ubuntu machine.

My observation is... whenever my windows client executes I get a ICMP message 3 times on the Wireshark having the type 3 message. The detailed analysis of the packet showed that the port is unreachable.

Kindly help me to find out what I am missing here :(.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have you tried disabling the firewall on the linux machine, or adding an exception for the port you are using?

sudo ufw disable

or use the following to show your iptables firewall rules:

sudo iptables -L 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...