contra

Console Chat Client

May 6th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. ////////////////////////////////////////////////////
  2. // Simple Chat Client, as basic as it gets
  3. // thx dxtuts
  4. ////////////////////////////////////////////////////
  5. #define _WINSOCKAPI_    // Don't include Winsock.h
  6. #include <winsock2.h>    // WinSock header file
  7. #include <stdio.h>    // Input/Output header file for gets() function
  8.  
  9. #pragma comment(lib, "Ws2_32.lib")    // WinSock Library
  10.  
  11. #define SERVER_ADDRESS "192.168.1.100" // ip where server is, yours will differ
  12. #define SERVER_PORT 17000
  13.  
  14. DWORD WINAPI RecvThread(LPVOID Whatever);    // The RecvThread() prototype
  15.  
  16. WSADATA Winsock;    // Stores information about Winsock
  17. SOCKET Socket;    // The ID of the socket
  18. sockaddr_in ServerAddress;    // The address to send data to
  19. char SendBuffer[256];    // The buffer of data to send
  20. char RecvBuffer[256];    // The buffer of data to receive
  21. int SizeInt = sizeof(ServerAddress);    // The size of the server's address
  22.  
  23. void main()
  24. {
  25.     WSAStartup(MAKEWORD(2, 2), &Winsock);
  26.  
  27.     if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)    // Check version
  28.     {
  29.         WSACleanup();
  30.         return;
  31.     }
  32.  
  33.     // Make the Socket
  34.     Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  35.  
  36.     // Input Server Information
  37.     ZeroMemory(&ServerAddress, sizeof(ServerAddress));    // clear the struct
  38.     ServerAddress.sin_family = AF_INET;    // set the address family
  39.     ServerAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);    // set the IP address
  40.     ServerAddress.sin_port = SERVER_PORT;    // set the port
  41.  
  42.     // Send a knock to the server
  43.     SendBuffer[0] = 1;
  44.     sendto(Socket, SendBuffer, 1, 0, (sockaddr*)&ServerAddress, sizeof(sockaddr));
  45.  
  46.     // Start the receiver thread
  47.     CreateThread(NULL, 0, RecvThread, NULL, 0, NULL);
  48.  
  49.     // Send the Messages
  50.     while(true)
  51.     {
  52.         gets(SendBuffer);
  53.         sendto(Socket, SendBuffer, 256, 0, (sockaddr*)&ServerAddress, sizeof(sockaddr));
  54.  
  55.         if(SendBuffer[0] == ' ')
  56.             break;
  57.     };
  58.  
  59.     WSACleanup();
  60.  
  61.     return;
  62. }
  63.  
  64. DWORD WINAPI RecvThread(LPVOID Whatever)
  65. {
  66.     while(true)
  67.     {
  68.         // Wait for messages to arrive
  69.         recvfrom(Socket, RecvBuffer, 256, 0, (sockaddr*)&ServerAddress, &SizeInt);
  70.  
  71.         // Print them to the screen
  72.         printf("Received: ");    // print a pretty "Received: " label
  73.         printf(RecvBuffer);    // print the data itself
  74.         printf("\n");    // jump to a new line
  75.  
  76.         // If the user quits, this thread has to exit too, or the program won't end
  77.         if(SendBuffer[0] == ' ')
  78.             break;
  79.     }
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment