Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <winsock.h>
- #include <iostream>
- #pragma comment(lib, "ws2_32.lib")
- #pragma comment(lib, "winmm.lib")
- struct sockaddr_in udp_addr, udp_receive;
- int addr_len = sizeof(struct sockaddr_in);
- using namespace std;
- main()
- {
- SOCKET udp_socket;
- int Er_code;
- DWORD Time = 0;
- WORD wVersion = MAKEWORD(2, 0);
- WSADATA wsaData;
- Er_code = WSAStartup(wVersion, &wsaData);
- if(Er_code != 0){
- cout << "Error! Failed initializing WinSock." << endl;
- return 0;
- }
- // udp initializing
- udp_addr.sin_family = AF_INET;
- udp_addr.sin_port = htons(13546); // Port
- udp_addr.sin_addr.s_addr = inet_addr("92.63.102.249");
- udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
- if(udp_socket == INVALID_SOCKET){
- cout << "Error! Failed create udp socket." << endl;
- return 0;
- }
- for(int i = 0; i < 10000; i++)
- {
- char text[32] = "";
- char buf[128] = "";
- sprintf(text, "Hello, test message %d", i);
- Time = timeGetTime();
- *(int*)text = i;
- Er_code = sendto(udp_socket, text, sizeof(text), 0, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in));
- if(Er_code == SOCKET_ERROR){
- cout << "Send error! I is : " << i << endl;
- break;
- }
- Er_code = recvfrom(udp_socket, buf, 128, 0, (struct sockaddr *)&udp_receive, &addr_len);
- if(Er_code == SOCKET_ERROR){
- cout << "Receive error! I is : " << i << endl;
- break;
- }
- if(*(int*)buf != i){
- cout << "Packet loss. n:" << i << endl;
- break;
- }
- Time = timeGetTime() - Time;
- if(i % 100 == 0){
- cout << "Response from " << inet_ntoa(udp_receive.sin_addr) << ":" << ntohs(udp_receive.sin_port) /*<< " is: \n"
- << buf */<< "Ping: " << Time << "ms" << endl;
- }
- }
- cout << "Completed." << endl;
- cin >> i;
- closesocket(udp_socket);
- return 0;
- }
Add Comment
Please, Sign In to add comment