Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1.     static void UdpPortCheck(char * buff, int id, sockaddr_in destination)
  2.     {
  3.         destination.sin_port = htons(id);
  4.  
  5.         int timeout = 1000;
  6.         SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
  7.         setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
  8.                    (char*)&timeout, sizeof(timeout));
  9.  
  10.         sendto(sock, &buff[0], strlen(&buff[0]), 0,
  11.             (sockaddr*)&destination, sizeof(destination));
  12.  
  13.         int dest_size = sizeof(destination);
  14.  
  15.         if (recvfrom(sock,
  16.             &buff[0],
  17.             sizeof(buff) - 1,
  18.             0,
  19.             (sockaddr*)&destination,
  20.             &dest_size) !=
  21.             SOCKET_ERROR)
  22.         {
  23.             printf("\t\tPort %i is open\n", id);
  24.         }
  25.         else if (WSAGetLastError() == WSAETIMEDOUT)
  26.         {
  27.             printf("\t\tPort %i is open\n", id);
  28.         }
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement