How to create a chat with usernames and a list of names of people connected to the server that you can choose to whom to send a message

Yairk_kaufmann 6 Reputation points
2021-03-17T18:29:48.947+00:00

I want to create a chat similar to Messenger with c #, I tried some tutorials but did not understand them, anyone have a tutorial idea how to do this?

Developer technologies | Windows Forms
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2021-03-17T19:01:00.663+00:00

    There are plenty of samples from Google, like :
    How to C# Chat server programming
    Chat Server with Client Implemented with C#
    etc...

    0 comments No comments

  2. Ram Dixit 0 Reputation points
    2023-01-20T09:23:33.5933333+00:00
    Here is some code that will show you how we will be using threads to allow multiple connections:
    
    ?
    1 Socket socket = sSocket.accept();
    2 Thread socketThread = new ThreadClass(socket);
    3 socketThread.start();
    
    We will run this code every time someone connects and this will essentially place their connection in it’s own thread. This thread will handle everything for that one client. Unfortunately we can’t just say “RUN THE SOCKET” and it will suddenly magically receive connections from the client, print them out, and send them to all the other clients. So, we essentially create our own custom thread. Here is some code that will outline how this will work:
    
    //We need to use "implements Runnable to tell Java that this is a thread
     class ClientThread implements Runnable {
     //This run method is what is executed when the thread starts
     public void run()
     {
     //Set up the PrintWriter and BufferedReader here
     while(true) {
     //Get info sent from client
     String clientInput = input.nextLine();
     //Here would would have a for loop that would send the
     //client's message to every other client connected.
     }
     }
     }
    Okay, now that you know a little about threads and what we’re going to do with them lets redo the server code.
    The New Server Code
    import java.net.*;
    import java.util.*;
    import java.io.*;
    
    public class Server
    {
        public static void main(String[] args)
        {
            new Server();
        }
    
        public Server()
        {
            //We need a try-catch because lots of errors can be thrown
            try {
                ServerSocket sSocket = new ServerSocket(5000);
                System.out.println("Server started at: " + new Date());
    
    
                //Loop that runs server functions
                while(true) {
                    //Wait for a client to connect
                    Socket socket = sSocket.accept();
    
    
    
                    //Create a new custom thread to handle the connection
                    ClientThread cT = new ClientThread(socket);
    
                    //Start the thread!
                    new Thread(cT).start();
    
                }
            } catch(IOException exception) {
                System.out.println("Error: " + exception);
            }
        }
    
        //Here we create the ClientThread inner class and have it implement Runnable
        //This means that it can be used as a thread
        class ClientThread implements Runnable
        {
            Socket threadSocket;
    
            //This constructor will be passed the socket
            public ClientThread(Socket socket)
            {
                //Here we set the socket to a local variable so we can use it later
                threadSocket = socket;
            }
    
            public void run()
            {
                //All this should look familiar
                try {
                    //Create the streams
                    PrintWriter output = new PrintWriter(threadSocket.getOutputStream(), true);
                    BufferedReader input = new BufferedReader(new InputStreamReader(threadSocket.getInputStream()));
    
                    //Tell the client that he/she has connected
                    output.println("You have connected at: " + new Date());
    
                    while (true) {
                        //This will wait until a line of text has been sent
                        String chatInput = input.readLine();
                        System.out.println(chatInput);
                    }
                } catch(IOException exception) {
                    System.out.println("Error: " + exception);
                }
            }
        }
    }
    
    Regards
    Ram Dixit
    (Full Stack Developers)
    Email: ******@digiprima.com
    Visit us at - 
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.