Created
September 2, 2021 15:56
-
-
Save arunsammit/18d33e307b84219763c3089d79bc1cf5 to your computer and use it in GitHub Desktop.
Server Client Communication with multiple client support in Java using sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
public class Client { | |
//declearing socket and input output streams | |
private Socket socket = null; | |
ObjectOutputStream dout; | |
ObjectInputStream din; | |
// constructor to put ip address and port | |
public Client(String address, int port) throws UnknownHostException, IOException, ClassNotFoundException { | |
// establish a connection | |
socket = new Socket(address, port); | |
System.out.println("Connected"); | |
} | |
// initializing the input output streams | |
public void initializeStreams() throws IOException{ | |
dout = new ObjectOutputStream(socket.getOutputStream()); | |
din = new ObjectInputStream(socket.getInputStream()); | |
} | |
public void closeStreams() throws IOException { | |
dout.close(); | |
din.close(); | |
} | |
// method to communicate with server by sending the message and getting back the response (List of words) | |
private Object communicate(String userInput) throws IOException, ClassNotFoundException { | |
Object serverResponse; | |
dout.writeUTF(userInput); | |
dout.flush(); | |
serverResponse = din.readObject(); | |
return serverResponse; | |
} | |
public static void main(String args[]) throws UnknownHostException, IOException, ClassNotFoundException { | |
if(args.length != 1){ | |
System.err.println("Usage: java Client <port number>"); | |
System.exit(1); | |
} | |
int portNumber = Integer.parseInt(args[0]); | |
Client client = new Client("127.0.0.1", portNumber); | |
BufferedReader input = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); | |
String userInput = ""; | |
// Read until "End" is encountered | |
client.initializeStreams(); | |
while (!userInput.equals("End")) { | |
userInput = input.readLine(); | |
Object serverResponse = client.communicate(userInput); | |
System.out.println("Server says: " + serverResponse); | |
} | |
//closing the input/output streams and the socket | |
client.closeStreams(); | |
client.socket.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.io.*; | |
public class MultiServer { | |
public static void main(String[] args) throws IOException { | |
if (args.length != 1) { | |
System.err.println("Usage: java MultiServer <port number>"); | |
System.exit(1); | |
} | |
int portNumber = Integer.parseInt(args[0]); | |
boolean listening = true; | |
int clientNumber = 0; | |
System.out.println("Server started"); | |
try (ServerSocket serverSocket = new ServerSocket(portNumber)) { | |
while (listening) { | |
System.out.println("Waiting for next client ..."); | |
Socket socket = serverSocket.accept(); | |
clientNumber++; | |
System.out.println("Connected to client number " + clientNumber); | |
new Thread(new ServerThread(socket,clientNumber)).start(); | |
} | |
} catch (IOException exception) { | |
System.err.println("Couldn't listen on port " + portNumber); | |
System.exit(1); | |
} | |
} | |
} | |
class ServerThread implements Runnable { | |
private Socket socket; | |
private int clientNumber; | |
public ServerThread(Socket socket, int clientNumber) { | |
this.socket = socket; | |
this.clientNumber = clientNumber; | |
} | |
//method to parse clients message and sort the words present in it by their length | |
private List<String> sortStrings(String input) { | |
String[] stringList = input.split(" "); | |
Arrays.sort(stringList, (a, b) -> { | |
return a.length() - b.length(); | |
}); | |
return Arrays.asList(stringList); | |
} | |
@Override | |
public void run() { | |
// takes input from the client socket | |
try (ObjectInputStream din = new ObjectInputStream(socket.getInputStream()); | |
ObjectOutputStream dout = new ObjectOutputStream(socket.getOutputStream());) { | |
String clientMessage = ""; | |
// reads message from client until "End" is sent | |
while ((!clientMessage.equals("End"))) { | |
clientMessage = din.readUTF(); | |
System.out.println("Client " + clientNumber + " says: " + clientMessage); | |
// replying to client with list of sorted words contained in the clients message | |
dout.writeObject(sortStrings(clientMessage)); | |
dout.flush(); | |
} | |
socket.close(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile the java classes using the command
javac Multiserver.java
javac Client.java
Run the Compiled java class files using the following commands in different terminals
Note that the must be same while running client and server
(Run the Server Class first to avoid the Client program from crashing)
send a string which consists of space seperated words
The server will respond with a List Containing the words sorted according to their lengths
To end the communication with server send "End" from the Client
Note that the server supports multiple clients just like any normal server