Advertisement
Combreal

SDL2Server.cpp

Nov 21st, 2020
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <iostream>
  6. #include <string>
  7. #include <winsock2.h>
  8. #include <ws2tcpip.h>
  9. #pragma comment(lib, "ws2_32.lib")
  10. #pragma warning( disable : 4996)
  11. #define TRUE 1
  12. #define FALSE 0
  13. #define PORT 5555
  14. using namespace std;
  15.  
  16. struct t_PLAYERDATA
  17. {
  18.     int playerID;
  19.     double playerX;
  20.     double playerY;
  21. } PLAYERDATA;
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.     int opt = TRUE;
  26.     int master_socket, addrlen, new_socket, client_socket[30], max_clients = 30, activity, i, valread, sd, sdBis;
  27.     int max_sd;
  28.     struct sockaddr_in address;
  29.     WSADATA WSAData;
  30.     WSAStartup(MAKEWORD(2, 0), &WSAData);
  31.     char buffer[1024];
  32.     fd_set readfds;
  33.     const char *message = "Welcome to FoD\n";
  34.     for (i = 0; i < max_clients; i++)
  35.     {
  36.         client_socket[i] = 0;
  37.     }
  38.     if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  39.     {
  40.         perror("socket failed");
  41.         exit(EXIT_FAILURE);
  42.     }
  43.     /*int Socketerror = WSAGetLastError();
  44.     cout << Socketerror << endl;*/
  45.     if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0)
  46.     {
  47.         perror("setsockopt");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.     address.sin_family = AF_INET;
  51.     address.sin_addr.s_addr = INADDR_ANY;
  52.     address.sin_port = htons(PORT);
  53.     if (bind(master_socket, (struct sockaddr *)&address, sizeof(address)) < 0)
  54.     {
  55.         perror("bind failed");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.     printf("Listener on port %d \n", PORT);
  59.     if (listen(master_socket, 3) < 0)
  60.     {
  61.         perror("listen");
  62.         exit(EXIT_FAILURE);
  63.     }
  64.     addrlen = sizeof(address);
  65.     puts("Waiting for connections ...");
  66.  
  67.     while (TRUE)
  68.     {
  69.         FD_ZERO(&readfds);
  70.         FD_SET(master_socket, &readfds);
  71.         max_sd = master_socket;
  72.         for (i = 0; i < max_clients; i++)
  73.         {
  74.             sd = client_socket[i];
  75.             if (sd > 0)
  76.             {
  77.                 FD_SET(sd, &readfds);
  78.             }
  79.             if (sd > max_sd)
  80.             {
  81.                 max_sd = sd;
  82.             }
  83.         }
  84.         activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
  85.         if ((activity < 0) && (errno != EINTR))
  86.         {
  87.             printf("select error");
  88.         }
  89.         if (FD_ISSET(master_socket, &readfds))
  90.         {
  91.             if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0)
  92.             {
  93.                 perror("accept");
  94.                 exit(EXIT_FAILURE);
  95.             }
  96.             printf("New connection , socket fd is %d , ip is : %s , port : %d\n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
  97.             if (send(new_socket, message, strlen(message), 0) != strlen(message))
  98.             {
  99.                 perror("send");
  100.             }
  101.             puts("Welcome message sent successfully");
  102.             for (i = 0; i < max_clients; i++)
  103.             {
  104.                 if (client_socket[i] == 0)
  105.                 {
  106.                     client_socket[i] = new_socket;
  107.                     printf("Adding to list of sockets as %d\n", i);
  108.  
  109.                     break;
  110.                 }
  111.             }
  112.         }
  113.         for (i = 0; i < max_clients; i++)
  114.         {
  115.             memset(buffer, 0, sizeof buffer);
  116.             sd = client_socket[i];
  117.             if (FD_ISSET(sd, &readfds))
  118.             {
  119.                 if ((valread = recv(sd, reinterpret_cast<char*>(&PLAYERDATA), sizeof(PLAYERDATA), 0)) < 0)
  120.                 {
  121.                     getpeername(sd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
  122.                     printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
  123.                     closesocket(sd);
  124.                     client_socket[i] = 0;
  125.                 }
  126.                 else
  127.                 {
  128.                     cout << "playerID : " << PLAYERDATA.playerID << endl;
  129.                     cout << "playerX : " << PLAYERDATA.playerX << endl;
  130.                     cout << "playerY : " << PLAYERDATA.playerY << endl;
  131.                     cout << endl;
  132.                     for (i = 0; i < max_clients; i++)
  133.                     {
  134.                         send(client_socket[i], buffer, strlen(buffer), 0);
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.     }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement