最近一直在弄项目的虚拟机网络的配置,现在主要的问题就是要测试在我们进行虚拟机迁移的过程中的网络情况。于是写了一个网络测试程序,因为基于TCP连接的网络,网络是不会丢包的,所以为了更好的显示网络情况,本测试程序是基于UDP的。
客户端程序
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #define PORT 9877 /* Server port*/ #define MAXDATASIZE 100 /*........... */ #define SERVER_IP "127.0.0.1" int main(int argc, char *argv[]) { int sockfd, sin_size; sin_size = sizeof(struct sockaddr_in); int *buf = (int *)malloc(sizeof(int)); struct sockaddr_in server_addr; /* server */ if ((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) { perror("socket"); exit(1); } bzero(&tserver_addr,sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(PORT); /* short, NBO */ server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); loop: for(i = 1; i < 10000; i++) { *buf = i; sendto(sockfd, buf, sizeof(int), 0, (struct sockaddr *)&server_addr, sin_size); printf("client sending: %d\n", *buf); sleep(1); } goto loop; return 0; }
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/wait.h> #define MYPORT 3490 /*Listen port*/ void main() { int sockfd; /* Data port*/ struct sockaddr_in my_addr; /* myself addr */ struct sockaddr_in client_addr; /* others addr */ int sin_size; sin_size = sizeof(struct sockaddr_in); int *buf = (int *)malloc(sizeof(int)); int i; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); } bzero(&my_addr, sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); /* ...... */ my_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* .....IP */ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) == -1) { perror("bind"); exit(1); } while(1){ *buf = i; z = recvfrom(sockfd, buf, sizeof(int), 0, (struct sockaddr *)&client_addr, &sin_size); if(z < 0) continue; printf("server reciving: %d\n", i); } }
虚拟机迁移过程中网络状况测试的测试程序,布布扣,bubuko.com
原文:http://blog.csdn.net/wan_hust/article/details/24813087