有时候可能由于路由没有配置正确,或者NCTUns需要的依赖环境没有安装完备,则external host与 simulated host可能无法直接通信。例如如下的拓扑中,若要求host1与外部的物理主机PC_B要进行通信,而在测试时又无法实现,一种替代的做法是通过host2进行转发。
![]()
host1、host2以及PC_B上运行的程序如上图。测试代码如下:
|
udpclient.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> int main(int argc, char *argv[]) { if(argc < 2) { printf("请输入要传送的内容.\r\n"); exit(0); } int sock; //sendto中使用的对方地址 struct sockaddr_in toAddr; //在recvfrom中使用的对方主机地址 struct sockaddr_in fromAddr; unsigned int fromLen; char recvBuffer[128]; sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if(sock < 0) { printf("创建套接字失败了.\r\n"); exit(1); } memset(&toAddr,0,sizeof(toAddr)); toAddr.sin_family=AF_INET; toAddr.sin_addr.s_addr=inet_addr("1.0.2.1");
//toAddr.sin_addr.s_addr=inet_addr("192.168.0.11");
toAddr.sin_port = htons(8000);
if(sendto(sock,argv[1],strlen(argv[1]),0,(struct sockaddr*)&toAddr,sizeof(toAddr)) != strlen(argv[1])) { printf("sendto() 函数使用失败了.\r\n"); close(sock); exit(1); }
/* fromLen = sizeof(fromAddr); if(recvfrom(sock,recvBuffer,128,0,(struct sockaddr*)&fromAddr,&fromLen)<0) { printf("()recvfrom()函数使用失败了.\r\n"); close(sock); exit(1); } printf("recvfrom() server:%s\r\n",recvBuffer);
*/
close(sock);
}
|
|
udp2RecSnd.c
/* changed from udpserver.c */ /* inner for NCTUns, outer for externalMachine */
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> int main(int argc, char *argv[]) { int sock; // //sendto中使用的对方地址: for external machine struct sockaddr_in toAddr; //在recvfrom中使用的对方主机地址: from NCTUns simulated host. struct sockaddr_in fromAddr; int recvLen; unsigned int addrLen; char recvBuffer[128]; char sendBuffer[128]; sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if(sock < 0) { // printf("创建套接字失败了.\r\n"); exit(0); } memset(&fromAddr,0,sizeof(fromAddr)); memset(&toAddr,0,sizeof(toAddr));
fromAddr.sin_family=AF_INET; fromAddr.sin_addr.s_addr=htonl(INADDR_ANY); fromAddr.sin_port = htons(8000);
toAddr.sin_family=AF_INET; toAddr.sin_addr.s_addr=inet_addr("192.168.0.11"); toAddr.sin_port=htons(8000);
if(bind(sock,(struct sockaddr*)&fromAddr,sizeof(fromAddr))<0) { //printf("bind() 函数使用失败了.\r\n"); close(sock); exit(1); }
addrLen = sizeof(fromAddr); if((recvLen = recvfrom(sock,recvBuffer,128,0,(struct sockaddr*)&fromAddr,&addrLen))<0) { //printf("()recvfrom()函数使用失败了.\r\n"); close(sock); exit(1); } if(recvLen>0) { //printf("recvfrom client: %s\r\n",recvBuffer); }
if(sendto(sock,recvBuffer,recvLen,0,(struct sockaddr*)&toAddr,sizeof(toAddr))!=recvLen){ //printf("sendto fail\r\n"); close(sock); exit(0); } return 0;
}
|
|
udpRecSnd.c
// changed from udpserver.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> int main(int argc, char *argv[]) { int sock; //sendto中使用的对方地址 struct sockaddr_in toAddr; //在recvfrom中使用的对方主机地址 struct sockaddr_in fromAddr; int recvLen; unsigned int addrLen; char recvBuffer[128]; sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); if(sock < 0) { printf("创建套接字失败了.\r\n"); exit(0); } memset(&fromAddr,0,sizeof(fromAddr)); fromAddr.sin_family=AF_INET; fromAddr.sin_addr.s_addr=htonl(INADDR_ANY); fromAddr.sin_port = htons(8000); if(bind(sock,(struct sockaddr*)&fromAddr,sizeof(fromAddr))<0) { printf("bind() 函数使用失败了.\r\n"); close(sock); exit(1); }
addrLen = sizeof(toAddr); if((recvLen = recvfrom(sock,recvBuffer,128,0,(struct sockaddr*)&toAddr,&addrLen))<0) { printf("()recvfrom()函数使用失败了.\r\n"); close(sock); exit(1); } if(recvLen>0) { printf("recvfrom NCTUns: %s\r\n",recvBuffer); }
//printf("sendto fail\r\n"); close(sock); exit(0); }
|