Guest User

Untitled

a guest
Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <winsock.h>
  3. #include <iostream>
  4.  
  5. #pragma comment(lib, "ws2_32.lib")
  6. #pragma comment(lib, "winmm.lib")
  7.  
  8. struct sockaddr_in udp_addr, udp_receive;
  9. int addr_len = sizeof(struct sockaddr_in);
  10.  
  11. using namespace std;
  12.  
  13. main()
  14. {
  15. SOCKET udp_socket;
  16. int Er_code;
  17. DWORD Time = 0;
  18.  
  19. WORD wVersion = MAKEWORD(2, 0);
  20. WSADATA wsaData;
  21.  
  22. Er_code = WSAStartup(wVersion, &wsaData);
  23. if(Er_code != 0){
  24. cout << "Error! Failed initializing WinSock." << endl;
  25. return 0;
  26. }
  27.  
  28. // udp initializing
  29. udp_addr.sin_family = AF_INET;
  30. udp_addr.sin_port = htons(13546); // Port
  31. udp_addr.sin_addr.s_addr = inet_addr("92.63.102.249");
  32.  
  33. udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
  34. if(udp_socket == INVALID_SOCKET){
  35. cout << "Error! Failed create udp socket." << endl;
  36. return 0;
  37. }
  38.  
  39. for(int i = 0; i < 10000; i++)
  40. {
  41. char text[32] = "";
  42. char buf[128] = "";
  43.  
  44. sprintf(text, "Hello, test message %d", i);
  45.  
  46. Time = timeGetTime();
  47.  
  48. *(int*)text = i;
  49.  
  50. Er_code = sendto(udp_socket, text, sizeof(text), 0, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in));
  51. if(Er_code == SOCKET_ERROR){
  52. cout << "Send error! I is : " << i << endl;
  53. break;
  54. }
  55.  
  56. Er_code = recvfrom(udp_socket, buf, 128, 0, (struct sockaddr *)&udp_receive, &addr_len);
  57. if(Er_code == SOCKET_ERROR){
  58. cout << "Receive error! I is : " << i << endl;
  59. break;
  60. }
  61.  
  62. if(*(int*)buf != i){
  63. cout << "Packet loss. n:" << i << endl;
  64. break;
  65. }
  66.  
  67. Time = timeGetTime() - Time;
  68.  
  69. if(i % 100 == 0){
  70. cout << "Response from " << inet_ntoa(udp_receive.sin_addr) << ":" << ntohs(udp_receive.sin_port) /*<< " is: \n"
  71. << buf */<< "Ping: " << Time << "ms" << endl;
  72. }
  73.  
  74.  
  75. }
  76.  
  77. cout << "Completed." << endl;
  78. cin >> i;
  79.  
  80. closesocket(udp_socket);
  81.  
  82. return 0;
  83. }
Add Comment
Please, Sign In to add comment