Linux UDP服务器从窗口7不可达 [英] Linux UDP Server unreachable from Window 7
问题描述
我的实验具有以下配置。
I have the following configuration for my experiment.
- Wifi(Belkin)路由器连接到互联网。
- Windows 7操作系统的笔记本电脑
- 使用Ubuntu操作系统的笔记本电脑。
当我连接我的笔记本电脑到Wifi路由器,它分配DHCP IP 192.168.2.2到Linux& 192.168.2.3 to Win 7.他们都可以浏览互联网。
我在我的Linux机器上用以下代码启动一个UDP服务器。
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 \n",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 \n", argv[0], 9999);exit(1);}
//We are done ... Notify User
printf("%s: waiting for data on port UDP %u\n",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 \n",argv[0]);continue;}
//Received message
printf("%s: from %s:UDP%u : %s \n", 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;
}
我可以在Linux机器上的某些本地客户端尝试联系我的服务器时接收到服务器的数据包。
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.
问题:当我在Android AVD中使用相同的客户端我的Windows 7系统我无法到达我的服务器。
PROBLEM : When I make the same client in Android AVD present in my windows 7 system I am unable to reach my server.
我认为可能是防火墙问题,所以我删除了防火墙&通过传递自定义规则添加到下面链接中给出的IP192.168.2.2。 http://www.brighthub.com/computing/windows-platform/ articles / 40014.aspx#
但它没有工作。我认为首先我应该尝试与原始java首先,然后用AVD。
因此,我使用Java代码创建了一个UDP客户端,但仍然无法连接到服务器。
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.
然后我想让我们尝试使用原始C ++,会知道究竟是什么问题。下面是我为其实现的Visual Studio代码。
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 \n"); 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 \n");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 \n");exit(-1);}
printf("Received from server: %s \n", in_buf);
retcode = closesocket(client_s);
if (retcode < 0){ printf("*** ERROR - closesocket() failed \n");exit(-1);}
WSACleanup();
}
但它给我错误的目的地不可达。
But it gives me error of destination unreachable.
为了弄清楚数据包级别发生了什么,我在我的ubuntu计算机上安装了Wireshark。
To find out exactly what is going on at the packet level, I installed "Wireshark", on my ubuntu machine.
我的观察是...每当我的Windows客户端执行我得到一个ICMP消息3次在具有类型3消息的Wireshark。
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 :(.
推荐答案
您是否尝试在linux机器上禁用防火墙,或者为您使用的端口添加异常?
Have you tried disabling the firewall on the linux machine, or adding an exception for the port you are using?
sudo ufw disable
显示您的iptables防火墙规则:
or use the following to show your iptables firewall rules:
sudo iptables -L
这篇关于Linux UDP服务器从窗口7不可达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!