Antoine Duval
Antoine Duval

Reputation: 342

Java - Client and Server communication issues

I'm trying to create a simple Java Chatting application, but have ran into some problems.

Here is my code below for both Classes:

Serveur.java :

import java.io.*;
import java.net.*;

class Serveur{

    private String msgClient, msgServeur;
    private ServerSocket serverSocket;
    private DataOutputStream out;
    private Socket socket;
    private int port;
    private BufferedReader in;

    public Serveur() throws IOException{
        System.out.println("Serveur OK...");
        this.port = 21;
        this.serverSocket = new ServerSocket(this.port);
        while(true){
            this.socket = serverSocket.accept();
            this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            this.out = new DataOutputStream(this.socket.getOutputStream());
            this.msgClient = this.in.readLine();
            System.out.println("Received: " + this.msgClient);
            this.msgServeur = this.msgClient.toUpperCase() + '\n';
            send(this.msgServeur);
        }
    }

    public void send(String msg) throws IOException{
        this.out.writeBytes(msg);
    }

    public static void main(String argv[]) throws Exception {
        Serveur serveur = new Serveur();
    }
}

Client.java :

import java.io.*;
import java.net.*;

class Client{

    private String sentence;
    private String modifiedSentence;
    private int port;
    private BufferedReader inUser;
    private BufferedReader inServeur;
    private Socket socket;
    private DataOutputStream out;

    public Client() throws UnknownHostException, IOException{
        this.sentence = "";
        this.modifiedSentence = "";
        this.port = 21;
        this.inUser = new BufferedReader(new InputStreamReader(System.in));
        this.socket = new Socket("localhost", this.port);
        this.out = new DataOutputStream(this.socket.getOutputStream());
        this.inServeur = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        sentence = this.inUser.readLine();
        this.out.writeBytes(sentence + '\n');
        modifiedSentence = this.inServeur.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
        this.socket.close();
    }

    public static void main(String argv[]) throws Exception{
        Client client = new Client();
    }
}

I have 2 problems :
- The client sends a message, and the server returns the same message in uppercase. How can I allow the client to send messages without instantly closing it's socket connection?
- The server looks OK, but when I have multiple clients, will anything change having more than one connected client? How could I broadcast messages, and messages to specific clients?

Upvotes: 0

Views: 93

Answers (1)

WillShackleford
WillShackleford

Reputation: 7008

The client send a message, the server return the same message in uppercase. How can i allow the client to send infinite message and don't destroy itself after sending the first message ?

just have the client use a loop

boolean quit = false;
while (!quit) {
    sentence = this.inUser.readLine();
    quit = sentence.trim().equals("quit");
    this.out.writeBytes(sentence + '\n');
    modifiedSentence = this.inServeur.readLine();
    System.out.println("FROM SERVER: " + modifiedSentence);
}
this.socket.close();

The server looks ok, but when i will have multiple client, will it make difference between multiple client ? How can i broadcast ? send message to focused client ?

Yes, this server blocks reading the first client so any other client would have to wait. You probably want to put the code for blocking on each client in a separate thread or submit it to an ExecutiveService. And all of the variables that are specific to a client can not be in the single server class since you need one set for each client.

while (true) {
    final Socket socket = serverSocket.accept();
    new Thread(() -> {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            boolean quit = false;
            while (!quit) {
                String msgClient = in.readLine();
                quit = msgClient.trim().equals("quit");
                System.out.println("Received: " + msgClient);
                String msgServeur = msgClient.toUpperCase() + '\n';
                out.writeBytes(msgServeur);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }).start();
}

Upvotes: 2

Related Questions