当前位置: 技术问答>linux和unix
请教:client怎么收不到UDP包?
来源: 互联网 发布时间:2016-03-02
本文导语: //服务器端: #ifndef _WIN32_ typedef int SOCKET; #endif const int INVALID_SOCKET = -1; const int SOCKET_ERROR = -1; const int port = 6789; const char * ip = "192.168.0.33"; //我的机器在局域网中的IP int main() { SOCKET sock_comm; struct soc...
//服务器端:
#ifndef _WIN32_
typedef int SOCKET;
#endif
const int INVALID_SOCKET = -1;
const int SOCKET_ERROR = -1;
const int port = 6789;
const char * ip = "192.168.0.33"; //我的机器在局域网中的IP
int main()
{
SOCKET sock_comm;
struct sockaddr_in addr_remote;
const int addrsize = sizeof(struct sockaddr_in);
char buf[1473]; //80
int i;
sock_comm = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr_remote,0,addrsize);
addr_remote.sin_family = AF_INET;
addr_remote.sin_port = htons(port);
addr_remote.sin_addr.s_addr = inet_addr(ip);
for (i=0; i 192.168.0.33.smc-https: UDP, length 22
20:06:55.333812 IP 192.168.0.19.32785 > 192.168.0.33.smc-https: UDP, length 22
//为什么是从IP:192.168.0.19.32785发送到192.168.0.33
//以下是客户端程序,接收不到UDP包:
#ifndef _WIN32_
typedef int SOCKET;
#endif
const int INVALID_SOCKET = -1;
const int SOCKET_ERROR = -1;
const int port = 6789;
int main()
{
SOCKET sock_comm;
struct sockaddr_in addr_local;
int addrsize = sizeof(struct sockaddr_in);
char message[256];
printf("waiting for data from sender...n");
sock_comm = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr_local, 0, addrsize);
addr_local.sin_family = AF_INET;
addr_local.sin_port = htons(port);
addr_local.sin_addr.s_addr = htonl(INADDR_ANY); //inet_addr("192.168.0.33");也不行
bind(sock_comm, (struct sockaddr *)&addr_local, addrsize);
//在本机指定端口上接收数据
while (1)
{
recvfrom(sock_comm, message, sizeof(message), 0,
(struct sockaddr *)&addr_local, &addrsize);
//addr_local得到发送方的IP地址和端口
printf("Response from sender: %sn", message);
if (strncmp(message, "stop", 4) == 0)
{
printf("sender has told me to end the connectionn");
break;
}
}
close(sock_comm);
return 0;
}
//为什么接收不到UDP包??
//将服务器程序中的IP改为"127.0.0.1",客户端就可以接收到UDP包了,为什么?
//哪位帮忙提点一下,谢谢!!
以上代码出自GNU、Linux编程指南
#ifndef _WIN32_
typedef int SOCKET;
#endif
const int INVALID_SOCKET = -1;
const int SOCKET_ERROR = -1;
const int port = 6789;
const char * ip = "192.168.0.33"; //我的机器在局域网中的IP
int main()
{
SOCKET sock_comm;
struct sockaddr_in addr_remote;
const int addrsize = sizeof(struct sockaddr_in);
char buf[1473]; //80
int i;
sock_comm = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr_remote,0,addrsize);
addr_remote.sin_family = AF_INET;
addr_remote.sin_port = htons(port);
addr_remote.sin_addr.s_addr = inet_addr(ip);
for (i=0; i 192.168.0.33.smc-https: UDP, length 22
20:06:55.333812 IP 192.168.0.19.32785 > 192.168.0.33.smc-https: UDP, length 22
//为什么是从IP:192.168.0.19.32785发送到192.168.0.33
//以下是客户端程序,接收不到UDP包:
#ifndef _WIN32_
typedef int SOCKET;
#endif
const int INVALID_SOCKET = -1;
const int SOCKET_ERROR = -1;
const int port = 6789;
int main()
{
SOCKET sock_comm;
struct sockaddr_in addr_local;
int addrsize = sizeof(struct sockaddr_in);
char message[256];
printf("waiting for data from sender...n");
sock_comm = socket(AF_INET, SOCK_DGRAM, 0);
memset(&addr_local, 0, addrsize);
addr_local.sin_family = AF_INET;
addr_local.sin_port = htons(port);
addr_local.sin_addr.s_addr = htonl(INADDR_ANY); //inet_addr("192.168.0.33");也不行
bind(sock_comm, (struct sockaddr *)&addr_local, addrsize);
//在本机指定端口上接收数据
while (1)
{
recvfrom(sock_comm, message, sizeof(message), 0,
(struct sockaddr *)&addr_local, &addrsize);
//addr_local得到发送方的IP地址和端口
printf("Response from sender: %sn", message);
if (strncmp(message, "stop", 4) == 0)
{
printf("sender has told me to end the connectionn");
break;
}
}
close(sock_comm);
return 0;
}
//为什么接收不到UDP包??
//将服务器程序中的IP改为"127.0.0.1",客户端就可以接收到UDP包了,为什么?
//哪位帮忙提点一下,谢谢!!
以上代码出自GNU、Linux编程指南
|
还有就是windows下的本机IP和虚拟机运行的linux下的本机IP为什么会不同呢??
虚拟机,虚拟机,就是虚拟出来的一个独立机器,除了基本的硬件,都是独立的.你就把它们当成两个独立的机器就可以了
你不会把他们认为是一个系统吧?
|
你的LINUX设置中IP表是怎么设的
没准不能正确识别127.0.0.1
没准不能正确识别127.0.0.1