contra

Console Chat Server

May 6th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. /////////////////////////////////////////////////
  2. // Simple Chat Server, As basic as possibe
  3. /////////////////////////////////////////////////
  4. #define _WINSOCKAPI_
  5. #include <winsock2.h>
  6. #include <stdio.h>
  7.  
  8. #pragma comment(lib, "Ws2_32.lib")
  9.  
  10. #define SERVER_PORT 17000
  11.  
  12. WSADATA Winsock;
  13. SOCKET Socket;
  14. sockaddr_in ServerAddress;
  15. sockaddr_in IncomingAddress;     // Contains the address of the sending client
  16. sockaddr_in ClientAddress[8];    // Stores the client's addresses
  17. char Buffer[16];
  18. int AddressLen = sizeof(IncomingAddress);
  19.  
  20. void main()
  21. {
  22.     WSAStartup(MAKEWORD(2, 2), &Winsock);
  23.  
  24.     if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)
  25.     {
  26.         WSACleanup();
  27.         return;
  28.     }
  29.  
  30.     // Make the Socket
  31.     Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  32.  
  33.     // Input server information and bind it to the socket
  34.     ZeroMemory(&ServerAddress, sizeof(ServerAddress));
  35.     ServerAddress.sin_family = AF_INET;
  36.       ServerAddress.sin_port = SERVER_PORT;
  37.     bind(Socket, (sockaddr*)&ServerAddress, sizeof(ServerAddress));
  38.  
  39.     while(true)
  40.     {
  41.         if(recvfrom(Socket, Buffer, 256, 0, (sockaddr*)&IncomingAddress, &AddressLen))
  42.         {
  43.             // If the packet is a knock, add the client's address to ClientAddress
  44.             if(Buffer[0] == 1)
  45.             {
  46.                 for(int i = 0; i < 8; i++)
  47.                 {
  48.                     if(!ClientAddress[i].sin_family)
  49.                     {
  50.                         ClientAddress[i] = IncomingAddress;
  51.                         break;
  52.                     }
  53.                 }
  54.                 continue;
  55.             }
  56.  
  57.                   // Display the message and broadcast it to all active clients
  58.             Buffer[255] = '\0';    // Always end the packet with this
  59.             printf("Broadcasting: ");
  60.             printf(Buffer);
  61.             printf("\n");
  62.             for(int i = 0; i < 8; i++)
  63.             {
  64.                 if(ClientAddress[i].sin_family)
  65.                     sendto(Socket, Buffer, 256, 0, (sockaddr*)&ClientAddress[i],
  66.                            sizeof(sockaddr));
  67.             }
  68.  
  69.                   // If a client has quit, remove that client's address from ClientAddress
  70.             if(Buffer[0] == ' ')
  71.             {
  72.                 for(int i = 0; i < 8; i++)
  73.                 {
  74.                    if(ClientAddress[i].sin_addr.s_addr == IncomingAddress.sin_addr.s_addr)
  75.                         ZeroMemory(&ClientAddress[i], sizeof(sockaddr_in));
  76.                 }
  77.             }
  78.         }
  79.     }
  80.  
  81.     WSACleanup();
  82.  
  83.     return;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment