SHARE
TWEET

Untitled

a guest Mar 24th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int main(){
  2.     DWORD Status;
  3.     WSADATA WsData;
  4.     DWORD MaxRecv = 1024;
  5.     int Connection;
  6.  
  7.     //socket stuff
  8.     SOCKET MasterSock = INVALID_SOCKET;
  9.     SOCKET NewSocket = INVALID_SOCKET;
  10.     SOCKET ClientSocket[30];
  11. //  fd_set Readfds;
  12.     struct sockaddr_in Server;
  13.     struct sockaddr_in Client;
  14.  
  15.     char *buffer;
  16.     buffer = (char*)malloc((MaxRecv + 1) * sizeof(char));
  17.  
  18.     //init client sockets
  19.     for (int i = 0; i < 30; i++)
  20.     {
  21.         ClientSocket[i] = 0;
  22.     }
  23.  
  24.     //init WSA
  25.     Status = WSAStartup(MAKEWORD(2, 2), &WsData);
  26.     if (Status != 0) {
  27.         wprintf(L"WSAStartup failed: %d\n", Status);
  28.         return 1;
  29.     }
  30.  
  31.     wprintf(L"WSASocket is Initialized\n");
  32.  
  33.     //Create Socket
  34.     if ((MasterSock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
  35.         wprintf(L"Could not create socket %d", WSAGetLastError());
  36.     }
  37.    
  38.     wprintf(L"Socket Created\n");
  39.    
  40.     //prepare socket info
  41.     Server.sin_family = AF_INET;
  42.     Server.sin_port = htons(24567);
  43.     Server.sin_addr.s_addr = INADDR_ANY;
  44.  
  45.     //bind to socket
  46.     if (bind(MasterSock, (struct sockaddr *)&Server, sizeof(Server)) == SOCKET_ERROR)
  47.     {
  48.         wprintf(L"Bind failed with error code : %d", WSAGetLastError());
  49.         exit(EXIT_FAILURE);
  50.     }
  51.  
  52.     listen(MasterSock,3);
  53.  
  54.     //Accept and incoming connection
  55.     puts("Waiting for incoming connections...\n");
  56.  
  57.     Connection = sizeof(struct sockaddr_in);
  58.     NewSocket = accept(MasterSock, (struct sockaddr *)&Client, &Connection);
  59.     if (NewSocket == INVALID_SOCKET)
  60.     {
  61.         wprintf(L"Accept failed with error code : %d", WSAGetLastError());
  62.     }
  63.  
  64.     puts("Connection accepted\n");
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top