PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in ..../includes/geshi.php on line 2147

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
C++ Simple Chat Program In a Console Window (Winsock & Multithreading)-VBForums
Results 1 to 8 of 8
  1. #1

    Thread Starter
    Android OpenGL ES Guru Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,222

    C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    I made a simple chat program using a console window where you run the server in one program, and multiple clients in another, where the clients can chit chat with each other, even from different machines if you got the right port and IP address handy. You could quite possibly squeeze this into an MMO game or just about anything, Enjoy the code

    Server:
    C++ Code:
    1. #include <iostream>
    2. #include <winsock2.h>
    3. #include <ws2tcpip.h>
    4. #include <string>
    5. #include <thread>
    6. #include <vector>
    7.  
    8. #pragma comment (lib, "Ws2_32.lib")
    9.  
    10. #define IP_ADDRESS "192.168.56.1"
    11. #define DEFAULT_PORT "3504"
    12. #define DEFAULT_BUFLEN 512
    13. //Function Prototypes
    14. """";
    15.  
    16.     //Session
    17. "", tempmsg))
    18.                     msg = "Client #"": "//Broadcast that message to the other clients
    19. "Client #"" Disconnected"//Broadcast the disconnection message to the other clients
    20. //end while
    21. ""//Initialize Winsock
    22. "Intializing Winsock..." << std::endl;
    23.     WSAStartup(MAKEWORD(2, 2), &wsaData);
    24.  
    25.     //Setup hints
    26. //Setup Server
    27. "Setting up server..."//Create a listening socket for connecting to server
    28. "Creating server socket..." << std::endl;
    29.     server_socket = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
    30.  
    31.     //Setup socket options
    32. //Make it possible to re-bind to a port that was used within the last 2 minutes
    33. //Used for interactive programs
    34.  
    35.     //Assign an address to the server socket.
    36. "Binding socket..."//Listen for incoming connections.
    37. "Listening..." << std::endl;
    38.     listen(server_socket, SOMAXCONN);
    39.  
    40.     //Initialize the client list
    41. //Reset the number of clients
    42.         num_clients = -1;
    43.  
    44.         //Create a temporary id for the next client
    45. //std::cout << client[i].socket << std::endl;
    46. //Send the id to that client
    47. "Client #" << client[temp_id].id << " Accepted"//Create a thread process for that client
    48. "Server is full"//end while
    49.  
    50.  
    51.     //Close listening socket
    52.     closesocket(server_socket);
    53.  
    54.     //Close client socket
    55. //Clean up Winsock
    56. "Program has ended successfully" << std::endl;
    57.  
    58.     system("pause"

    Client:
    C++ Code:
    1. #include <winsock2.h>
    2. #include <ws2tcpip.h>
    3. #include <iostream>
    4. #include <string>
    5. #include <thread>
    6.  
    7. using namespace std;
    8.  
    9. #pragma comment (lib, "Ws2_32.lib")
    10.  
    11. #define DEFAULT_BUFLEN 512            
    12. #define IP_ADDRESS "192.168.56.1"
    13. #define DEFAULT_PORT "3504"
    14. "recv() failed: ""The server has disconnected""";
    15.     client_type client = { INVALID_SOCKET, -1, """Starting Client...\n";
    16.  
    17.     // Initialize Winsock
    18. "WSAStartup() failed with error: ""Connecting...\n";
    19.  
    20.     // Resolve the server address and port
    21. "getaddrinfo() failed with error: " << iResult << endl;
    22.         WSACleanup();
    23.         system("pause"// Attempt to connect to an address until one succeeds
    24. // Create a SOCKET for connecting to server
    25. "socket() failed with error: " << WSAGetLastError() << endl;
    26.             WSACleanup();
    27.             system("pause"// Connect to server.
    28. "Unable to connect to server!" << endl;
    29.         WSACleanup();
    30.         system("pause""Successfully Connected" << endl;
    31.  
    32.     //Obtain id from server for this client;
    33. "Server is full""send() failed: "//Shutdown the connection since no more data will be sent
    34. "Shutting down socket...""shutdown() failed with error: " << WSAGetLastError() << endl;
    35.         closesocket(client.socket);
    36.         WSACleanup();
    37.         system("pause""pause"

  2. #2
    New Member
    Join Date
    Jul 2015
    Posts
    1

    Re: C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    Hi, for the following lines i get compile errors

    in the server:

    //Create a thread process for that client
    my_thread[temp_id] = std::thread(process_client, std::ref(client[temp_id]), std::ref(client), std::ref(my_thread[temp_id]));

    in the client:

    thread my_thread(process_client, client);

    VS is telling me that there are no constructors matching these arguments or even this number of arguments. Could you please explain a little bit about creating these threads? is process_client the thread proc? i have some experience with win32, multithreading and thread syncronization but never networking and i dont remember using std::thread ever

  3. #3

    Re: C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    i compile it with out any error, thanks for share

  4. #4
    New Member
    Join Date
    Dec 2016
    Posts
    1

    Re: C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    Quote Originally Posted by tounsi8orr View Post
    i compile it with out any error, thanks for share

    Where did you got the librarys, the header files?

    Code:
    server.cpp:2:22: fatal error: winsock2.h: Datei oder Verzeichnis nicht gefunden
    compilation terminated.
    client.cpp:1:22: fatal error: winsock2.h: Datei oder Verzeichnis nicht gefunden
    compilation terminated.
    winsock2.h:21:22: fatal error: pshpack4.h: Datei oder Verzeichnis nicht gefunden
    compilation terminated.

  5. #5
    New Member
    Join Date
    Jan 2017
    Posts
    1

    Question Re: C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    Quote Originally Posted by lafumafin View Post
    Where did you got the librarys, the header files?

    Code:
    server.cpp:2:22: fatal error: winsock2.h: Datei oder Verzeichnis nicht gefunden
    compilation terminated.
    client.cpp:1:22: fatal error: winsock2.h: Datei oder Verzeichnis nicht gefunden
    compilation terminated.
    winsock2.h:21:22: fatal error: pshpack4.h: Datei oder Verzeichnis nicht gefunden
    compilation terminated.
    i m using visual studio 2013 ..... plz tell me where to compile that program...can i compile it in visualstudio win32 console application or any other:confused

  6. #6

    Thread Starter
    Android OpenGL ES Guru Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,222

    Re: C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    Try installing the latest version of Windows SDK if it didnt come with your version of visual studio.

  7. #7
    New Member
    Join Date
    Jun 2017
    Posts
    1

    Re: C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    Hi Jacob,

    thank you so much this sample. That's great..

    Best regards

  8. #8

    Re: C++ Simple Chat Program In a Console Window (Winsock & Multithreading)

    Have the same issue... How did you solve it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width