0

I'm learning java. I'm trying to make a simple client/server chat system. What I have so far is a program where the server accepts multiple client connections by giving them each a seperate thread.

My problem now, is that I can't figure out how to get an input from one client, and then have it be sent amongst all of the clients, thus essentially have a very very simple chat mechanic. How would I go about accomplishing this? What would be the simpler way?

My code so far is here;

class Client {
  public static void main(String argv[]) throws Exception {
    String sentMessage;  //variable for input
    String receivedMessage; //variable for output
    String status;
    boolean running;

    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

    Socket clientSocket = new Socket("127.0.0.1", 5622); //name of computer to connect with and port number to use

    DataOutputStream outToServer =
        new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer =
        new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    System.out.println("Client Side\n");

    running = true;

    while(running)
    {
        sentMessage = inFromUser.readLine(); //user inputs text to variable 'xInput'                      

        outToServer.writeBytes(sentMessage + '\n'); //the variable is sent to the server

        status = inFromServer.readLine();


        System.out.println("FROM SERVER: " + status); //display to user
    }    
    clientSocket.close();    
    }
}

The server code.

class Server {

  public static void main(String argv[]) throws Exception {
    String clientMessage;    

    boolean listening = true;
    int portNumber = 5622;

    try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
        while (listening) {
            new ServerThread(serverSocket.accept()).start();
        }
    } catch (IOException e) {
        System.err.println("Could not listen on port " + portNumber);
        System.exit(-1);
    }
  }
}

The thread that handles the client connections.

public class ServerThread extends Thread {
private Socket socket = null;

public ServerThread(Socket socket) {
    super("ServerThread");
    this.socket = socket;
}

public void run () {

    int msgCnt = 0;

    try (
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(
                socket.getInputStream()));
    ) {
        //something needs to go here
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • Always put the relevant code **in** the question, don't just link. Links rot, and people shouldn't have to go off-site to help you. – T.J. Crowder Apr 23 '14 at 07:36
  • Ah, sorry. I didn't know if so much code might make the post too long. – user3562306 Apr 23 '14 at 07:42
  • I think there are lots of threads about this already in SO. How about [this](http://stackoverflow.com/questions/19378748/simple-chat-using-socket-connection?rq=1), for example? – eis Apr 23 '14 at 07:44
  • "I'm learning java" + "multithreading" + "sockets" = 1000 traps to step in. – Nicolas Repiquet Apr 23 '14 at 07:52

1 Answers1

0

If you are looking for a simple client-server communication samples then please have a look at below posts where I have described it step by step.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76