Hello ,
I created simple command line multithread application it work , but i faced a simple problem which is when 2 or 3 threads connect to server the server need to write to console equals to number of clients connected to my server example 2 clients connected , i need to write for example hi,BYE , hi goes to clients 1 and BYE goes to client2 . i didn't like this way so i need help by putting clientCode as key to define my client and send the message to it the following code will explain the functionality of clientCode and more things
here is Server code:
package Server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
public class Server {
BufferedReader reader;
String data;
private int port = 7898;
private ServerSocket serverSocket;
private Socket socket;
String inputData;
HashMap<Integer, Socket> map = new HashMap<Integer, Socket>();
PrintWriter writer;
int clientCode = 0;
public Server(String sa) {
}
public Server() throws InterruptedException {
try {
serverSocket = new ServerSocket(port);
System.out.println("waiting For connection on this port " + " "
+ port);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
Thread thread = new Thread(new ServerManager(socket));
thread.start();
thread.sleep(1000L);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException {
Server server = new Server();
}
}
and here serverManager which most of server goes here:
package Server;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Random;
class ServerManager implements Runnable {
Socket socket = null;
HashMap<Integer, Socket> map = new HashMap<Integer, Socket>();
BufferedReader inputStream;
BufferedReader input;
BufferedWriter outputStream;
BufferedWriter bufferedWriter;
String data;
String inputData;
int clientCode = 0;
public ServerManager(Socket clientSocket) {
this.socket = clientSocket;
Random random = new Random();
clientCode = Math.abs(random.nextInt());
}
public ServerManager() {
}
class ServerReader implements Runnable {
InputStream inputStream;
public ServerReader(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public void run() {
while (true) {
try {
if (inputStream.available() > 0) {
byte[] readData = new byte[inputStream.available()];
System.out.println();
inputStream.read(readData);
System.out.println("Data from Client " + clientCode
+ ": " + new String(readData));
}
Thread.sleep(1000L);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void run() {
try {
System.out.println("The Client with Code " + clientCode + ": "
+ "Connected");
Thread thread = new Thread(
new ServerReader(socket.getInputStream()));
thread.start();
outputStream = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
add(clientCode);
data = dataFromConsole().toString();
send(data);
} catch (Exception e) {
e.printStackTrace();
}
}
public void add(Integer clientCode) {
map.put(clientCode, socket);
}
public void send(String message) throws IOException,
ClassNotFoundException, InterruptedException {
outputStream.write(message);
outputStream.newLine();
outputStream.flush();
}
public String dataFromConsole() throws IOException {
input = new BufferedReader(new InputStreamReader(System.in));
inputData = input.readLine();
return inputData;
}
public void Disconnect() throws IOException {
while (true) {
input.close();
outputStream.close();
socket.close();
}
}
}
My client code built at the same way here expect the multithreading part.