linux虚拟机udp环回通信实验
我想在虚拟机linux中做一个udp通信的实验,在虚拟机客户端中发送某一数字A,服务器程序接收到A,就求取阶乘并发回给客户端程序。
客户端和服务器都是虚拟机下的linux。虚拟机我选用NAT联网方式。
虚拟机的ip:192.168.10.140,如下ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet
192.168.10.140 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe9c:bbb3 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:9c:bb:b3 txqueuelen 1000 (Ethernet)
RX packets 838 bytes 77159 (75.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 287 bytes 24226 (23.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 19 base 0x2024
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 16436
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 57 bytes 4884 (4.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 57 bytes 4884 (4.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:18:bf:00:ed:f9 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
客户端程序为
#include<stdio.h>
#include<sys/socket.h>
#include<errno.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
main(int argc,char *argv[])
{
int n,nSend,len;
int nSocket;
long IResult;
struct sockaddr_in sin;
if(argc != 2)
{
fprintf(stderr,"Usage:%s number\n",argv[0]);
exit(0);
}
nSend = atoi(argv[1]);
<