Multiple client to server communication program in Java
Asked Answered
V

3

12

I wrote a server-client communication program and it worked well.

Client module

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

class Client {
    public static void main(String argv[]) throws Exception {
        String sentence;
        String modifiedSentence;
      while(true){
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        Socket clientSocket = new Socket("myname.domain.com", 2343);

        DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        System.out.println("Ready");
        sentence = in.readLine();
        out.writeBytes(sentence + '\n');
        modifiedSentence = in.readLine();
        System.out.println(modifiedSentence);
       }
      clientSocket.close();
    }
}

Server module

import java.net.*;

public class Server {
    public static void main(String args[]) throws Exception {
        String clientSentence;
        String cap_Sentence;
        ServerSocket my_Socket = new ServerSocket(2343);

        while(true) {
            Socket connectionSocket = my_Socket.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream out = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = in.readLine();
            cap_Sentence = "Raceived:" +  clientSentence + '\n';
            out.writeBytes(cap_Sentence);
        }
    }
}

The above is the code for a single client - server communication, now I want multiple client to interact with that server. I googled for it and found that it can be done with the use of a thread for each single client to talk to the server, but since I am a beginner I don't know exactly how to implement. So somebody please tell me how to do or give me some idea about it.

Veator answered 24/3, 2011 at 12:46 Comment(1)
It might be a good idea for you to consider what it is in the above code that doesnt work with multiple clients. Have you tried it with multiple clients? What should happen when multiple clients are connected?Tripalmitin
M
24

MainServer class

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;

        boolean listeningSocket = true;
        try {
            serverSocket = new ServerSocket(2343);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 2343");
        }

        while(listeningSocket){
            Socket clientSocket = serverSocket.accept();
            MiniServer mini = new MiniServer(clientSocket);
            mini.start();
        }
        serverSocket.close();       
    }

}

Helper Class

public class MiniServer extends Thread{

    private Socket socket = null;

    public MiniServer(Socket socket) {

        super("MiniServer");
        this.socket = socket;

    }

    public void run(){
            //Read input and process here
    }
            //implement your methods here

}
Masquer answered 24/3, 2011 at 12:58 Comment(0)
O
3

You want to look into Java concurrency. That's the concept of one Java program doing multiple things at once. At a high level you will be taking your while(true) { //... } block and running it as part of the run() method of a class implementing Runnable. You'll create instances of Thread that invoke that run() method, probably one per client you expect.

For a really good, deep understanding of all that Java offers when it comes to concurrency, check out Java Concurrency in Practice.

Oliviaolivie answered 24/3, 2011 at 12:50 Comment(0)
G
1

Well, when I do something like that, I implement a listener that manages the server side, so when a client (the client won't probably need changes) connects, the server launch one thread to work with that client.

while (!stop)
            {
                socket = serverSocket.accept();
                HiloSocket hiloSocket = new HiloSocket(socket, this);
                hiloSocket.start();
            }

Of course, HiloSocket extends Thread and it has the logic behind to manage the client...

Georgiana answered 24/3, 2011 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.