Advertisement
Guest User

Untitled

a guest
Mar 15th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #pragma GCC diagnostic ignored "-Wconversion-null"
  2.  
  3. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  4.  
  5. #include <WinSock2.h> //For win sockets
  6. #include <iostream> //For std::cout, std::endl, std::cin.getline
  7.  
  8. SOCKET Connection; //This client's connection to the server
  9. int ID = 0;
  10.  
  11. enum Packet
  12. {
  13. P_ChatMessage,
  14. P_Test
  15. };
  16.  
  17. bool ProcessPacket(Packet packettype)
  18. {
  19. switch (packettype)
  20. {
  21. case P_ChatMessage:
  22. {
  23. int remitentLength;
  24. recv(Connection, (char*)&remitentLength, sizeof(int), NULL);
  25. char * remitentMSG = new char[remitentLength + 1];
  26. recv(Connection, remitentMSG, remitentLength, NULL);
  27. remitentMSG[remitentLength] = '\0';
  28. std::cout << remitentMSG;
  29.  
  30. int bufferlength; //length of the chat message
  31. recv(Connection, (char*)&bufferlength, sizeof(int), NULL); //receive buffer
  32. char * buffer = new char[bufferlength + 1]; //Allocate buffer
  33. recv(Connection, buffer, bufferlength, NULL);
  34. buffer[bufferlength] = '\0'; //Set last character of buffer to be a null terminator so we aren't printing memory that we shouldn't be looking at
  35. std::cout << buffer << std::endl; //print out buffer
  36. delete[] buffer; //Deallocate buffer
  37. break;
  38. }
  39.  
  40. case P_Test:
  41. std::cout << "You have received the test packet!" << std::endl;
  42. break;
  43.  
  44. default:
  45. std::cout << "Unknown packet: " << packettype << std::endl;
  46. break;
  47. }
  48. return true;
  49. }
  50.  
  51. void ClientThread()
  52. {
  53. Packet packettype;
  54. while (true)
  55. {
  56. //First get the packet type
  57. recv(Connection, (char*)&packettype, sizeof(Packet), NULL); //Receive packet type from server
  58.  
  59. if (!ProcessPacket(packettype)) //If the packet is not properly processed
  60. break; //break out of our client handler loop
  61.  
  62. Sleep(1000);
  63. }
  64. }
  65.  
  66. int main()
  67. {
  68. //Winsock Startup
  69. WSAData wsaData;
  70. WORD DllVersion = MAKEWORD(2, 1);
  71. if (WSAStartup(DllVersion, &wsaData) != 0)
  72. {
  73. MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
  74. return 0;
  75. }
  76.  
  77. SOCKADDR_IN addr; //Address to be binded to our Connection socket
  78. int sizeofaddr = sizeof(addr); //Need sizeofaddr for the connect function
  79. addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //Address = localhost (this pc)
  80. addr.sin_port = htons(1111); //Port = 1111
  81. addr.sin_family = AF_INET; //IPv4 Socket
  82.  
  83. Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set Connection socket
  84. if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //If we are unable to connect...
  85. {
  86. MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR);
  87. return 0; //Failed to Connect
  88. }
  89.  
  90. recv(Connection, (char*)&ID, sizeof(int), NULL);
  91.  
  92. std::cout << "Connected under the ID: " << ID << std::endl;
  93.  
  94. CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientThread, NULL, NULL, NULL); //Create the client thread that will receive any data that the server sends.
  95.  
  96. std::string userinput; //holds the user's chat message
  97. while (true)
  98. {
  99. std::getline(std::cin,userinput); //Get line if user presses enter and fill the buffer
  100. int bufferlength = userinput.size(); //Find buffer length
  101. Packet chatmessagepacket = P_ChatMessage; //Create packet type: Chat Message to be sent to the server
  102. send(Connection, (char*)&chatmessagepacket, sizeof(Packet), NULL); //Send packet type: Chat Message
  103. send(Connection, (char*)&bufferlength, sizeof(int), NULL); //Send length of buffer
  104. send(Connection, userinput.c_str(), bufferlength, NULL); //Send buffer
  105. Sleep(20);
  106. }
  107.  
  108. return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement