Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #pragma GCC diagnostic ignored "-Wconversion-null"
  2.  
  3. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  4.  
  5. #include <WinSock2.h>
  6. #include <iostream>
  7. #include <string>
  8.  
  9. #define MAX_CONN 100
  10.  
  11. SOCKET Connections[MAX_CONN];
  12. int TotalConnections = 0;
  13.  
  14. enum Packet
  15. {
  16. P_ChatMessage,
  17. P_Test
  18. };
  19.  
  20. bool ProcessPacket(int ID, Packet packettype)
  21. {
  22. std::string remitent = "From " + std::to_string(ID+1) + ": ";
  23. int length = remitent.length();
  24.  
  25. switch (packettype)
  26. {
  27. case P_ChatMessage:
  28. {
  29. int bufferlength; //Length of the string
  30. //Logic for receiving a string
  31. recv(Connections[ID], (char*)&bufferlength, sizeof(int), NULL); //get buffer length
  32. char * buffer = new char[bufferlength]; //Allocate buffer
  33. recv(Connections[ID], buffer, bufferlength, NULL); //get buffer message from client
  34.  
  35. std::cout << std::endl << "Recieved Message Packet from ID: " << ID+1 << std::endl;
  36.  
  37. for (int i = 0; i < TotalConnections; i++) //For each client connection
  38. {
  39. if (i == ID) //Don't send the chat message to the same user who sent it
  40. continue; //Skip user
  41.  
  42. Packet chatmessagepacket = P_ChatMessage; //create chat message packet to be sent
  43. send(Connections[i], (char*)&chatmessagepacket, sizeof(Packet), NULL); //send chat message packet
  44. send(Connections[i], (char*)&length, sizeof(int), NULL); //send the length of the remitent message
  45. send(Connections[i], remitent.c_str(), length, NULL); //send the remitent message
  46.  
  47. send(Connections[i], (char*)&bufferlength, sizeof(int), NULL);//send the buffer length to client at index i
  48. send(Connections[i], buffer, bufferlength, NULL);//send the chat message to client at index i
  49.  
  50. std::cout << "Sended Message Packet to ID: " << i+1 << std::endl;
  51. }
  52. delete[] buffer; //Deallocate buffer to stop from leaking memory
  53.  
  54. break;
  55. }
  56.  
  57. case P_Test:
  58. std::cout << "Recieved Test Packet from ID: " << ID << std::endl;
  59. break;
  60.  
  61. default:
  62. std::cout << "Unknown packet received." << std::endl;
  63. break;
  64. }
  65.  
  66. return true;
  67. }
  68.  
  69. void ClientHandlerThread(int ID) //ID = the index in the SOCKET Connections array
  70. {
  71. while (true)
  72. {
  73. //First get the packet type
  74. Packet packettype;
  75. recv(Connections[ID], (char*)&packettype, sizeof(Packet), NULL); //Receive packet type from client
  76.  
  77. //Once we have the packet type, process the packet
  78. if (!ProcessPacket(ID, packettype)) //If the packet is not properly processed
  79. break; //break out of our client handler loop
  80.  
  81. Sleep(1000);
  82. }
  83. closesocket(Connections[ID]); //close the socket that was being used for the client's connection
  84. std::cout << "Closed connection for ID: " << ID << std::endl;
  85. }
  86.  
  87. int main()
  88. {
  89. //Winsock Startup
  90. WSAData wsaData;
  91. WORD DllVersion = MAKEWORD(2, 1);
  92. if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
  93. {
  94. MessageBoxA(NULL, "WinSock startup failed", "Error", MB_OK | MB_ICONERROR);
  95. return 0;
  96. }
  97.  
  98. SOCKADDR_IN addr; //Address that we will bind our listening socket to
  99. int addrlen = sizeof(addr); //length of the address (required for accept call)
  100. addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Broadcast locally
  101. addr.sin_port = htons(1111); //Port
  102. addr.sin_family = AF_INET; //IPv4 Socket
  103.  
  104. SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL); //Create socket to listen for new connections
  105. bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); //Bind the address to the socket
  106. listen(sListen, SOMAXCONN); //Places sListen socket in a state in which it is listening for an incoming connection. Note:SOMAXCONN = Socket Oustanding Max Connections
  107.  
  108. std::cout << "Listening..." << std::endl;
  109.  
  110. SOCKET newConnection; //Socket to hold the client's connection
  111. for (int i = 0; i < MAX_CONN; i++)
  112. {
  113. newConnection = accept(sListen, (SOCKADDR*)&addr, &addrlen); //Accept a new connection
  114. if (newConnection == 0) //If accepting the client connection failed
  115. {
  116. std::cout << "Failed to accept the client's connection." << std::endl;
  117. }
  118. else //If client connection properly accepted
  119. {
  120. std::cout << "Client " << TotalConnections+1 << " Connected!" << std::endl;
  121. Connections[i] = newConnection; //Set socket in array to be the newest connection before creating the thread to handle this client's socket.
  122.  
  123. int sendID = TotalConnections + 1;
  124. send(Connections[i], (char*)&sendID, sizeof(int), NULL);
  125.  
  126. CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientHandlerThread, (LPVOID)(i), NULL, NULL); //Create Thread to handle this client. The index in the socket array for this thread is the value (i).
  127.  
  128. Packet testpacket = P_Test;
  129. send(Connections[i], (char*)&testpacket, sizeof(Packet), NULL); //Send test packet
  130.  
  131. TotalConnections++; //Incremenent total # of clients that have connected
  132. }
  133. Sleep(20);
  134. }
  135.  
  136. system("pause");
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement