我有两个正在运行的虚拟机。两者都是Ubuntu 18.04。主机操作系统为Windows10。虚拟机具有IP 10.0.2.10 (A)
和10.0.2.11 (B)
。现在,我编写了以下C
代码以将UDP消息从B
发送到A
。
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(){
struct sockaddr_in dest_info;
char *data = "UDP message\n";
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
memset((char *) &dest_info, 0, sizeof(dest_info));
dest_info.sin_family = AF_INET;
dest_info.sin_addr.s_addr = inet_addr("10.0.2.10");
dest_info.sin_port = htons(9090);
sendto(sock, data, strlen(data), 0, (struct sockaddr *) &dest_info, sizeof(dest_info));
close(sock);
return 0;
}
我在A
的终端上运行以下命令:
nc -luv 9090
现在,我从B
运行程序,该程序在A
上给出以下输出:
Listening on [0.0.0.0] (family 0, port 9090)
Connection from 10.0.2.11 33849 received!
UDP message
正如预期的那样。现在我稍微更改一下参数:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(){
struct sockaddr_in dest_info;
char *data = "TCP message\n"; // <------ Changed the message
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); // <--- Changed parameter here
memset((char *) &dest_info, 0, sizeof(dest_info));
dest_info.sin_family = AF_INET;
dest_info.sin_addr.s_addr = inet_addr("10.0.2.10");
dest_info.sin_port = htons(9090);
sendto(sock, data, strlen(data), 0, (struct sockaddr *) &dest_info, sizeof(dest_info));
close(sock);
return 0;
}
在A
的终端上:
nc -lv 9090
现在从B
运行程序不会在A
上提供任何输出:
Listening on [0.0.0.0] (family 0, port 9090)
它只是坐在那里而不显示任何输出。 TCP传输出了什么问题?