I need to convert this single threaded Server in Multi Threaded one, so i'm able to handle multiple request from a server:
public class YASGP {
public static void main(String args[]) throws IOException {
ServerSocket server;
try{
server = new ServerSocket(5559);
System.out.println("Listening for connection on port 5559 ....");
while (true) {
Socket clientSocket = server.accept();
InputStreamReader isr = new InputStreamReader(clientSocket.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
new Thread(new WorkerRunnable(clientSocket)).start();
while (!line.isEmpty()) {
System.out.println("----- " + line);
if (!line.contains("OPTIONS")) {
// System.out.println("Non c'รจ nulla!!!");
} else {
timeS = line.substring(line.indexOf("timeS=") + 6, line.indexOf("&url"));
url = line.substring(line.indexOf("url=") + 4, line.lastIndexOf("¶m"));
param = line.substring(line.indexOf("¶m=") + 7, line.indexOf("HTTP"));
}
line = reader.readLine();
}
}
}
}catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
private static class RequestHandlingClass {
public RequestHandlingClass(Socket clientSocket) {
}
}
}
How i can convert it? Thanks to all
remove the 'client processing' code from the server as follows
public static void main(String args[]) throws IOException {
ServerSocket server;
try{
server = new ServerSocket(5559);
System.out.println("Listening for connection on port 5559 ....");
while (true) {
Socket clientSocket = server.accept();
new Thread(new WorkerRunnable(clientSocket)).start();
}
}catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
then the code that you lifted out goes in to the run method of the workerRunnable class .
I recommend u use the excutorService api.Since it will manager all your threading issue for u behind the scene
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
//Your code here
});
Related
I'm trying to build a server with Java.
My question is: how to do if I have multiple users at the same time? The answer is: multi threading. But I don't know how to do.
For example, if there is two client connected at the same time, and a server (who does 2*number) : if the client1 say "50" to the server and the client 2 "10", the server is supposed to return "100" to the first client and "20" to the second. But i'm not sure my code works.
Server side:
public class Server {
public static void main(String[] args){
ServerSocket socket;
try {
socket = new ServerSocket(4444);
Thread t = new Thread(new Accept(socket));
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
class Accept implements Runnable {
private ServerSocket socketserver;
private Socket socket;
private int nbrclient = 1;
public Accept(ServerSocket s){
socketserver = s;
}
public void run() {
try {
socket = socketserver.accept();
in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
String message = in.readLine();
System.out.println(message);
out = new PrintWriter(socket.getOutputStream());
out.println("Pong");
out.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client side:
public class Client {
public static void main(String[] zero) {
Socket socket;
BufferedReader in;
PrintWriter out;
try {
socket = new Socket(InetAddress.getLocalHost(),4444);
out = new PrintWriter(socket.getOutputStream());
out.println("Ping");
out.flush();
in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
String message = in.readLine();
System.out.println(message);
socket.close();
}catch (UnknownHostException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
If you have any ideas (how to do multi-threading and how to verify if my code works, like run two Clients.java and check if the multi-threading works)
The Server sides needs a while loop:
public class Server {
public static void main(String[] args){
ServerSocket socket;
try {
while(true){
socket = new ServerSocket(4444);
Socket socketInstance = socket.accept();
Thread t = new Thread(new Accept(socketInstance));
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class Accept implements Runnable {
private Socket socket;
private int nbrclient = 1;
public Accept(Socket s){
socket = s;
}
public void run() {
try {
in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
String message = in.readLine();
System.out.println(message);//this message should be your number
Double number = Double.parseString(message);
out = new PrintWriter(socket.getOutputStream());
//out.println("Pong");
out.println(2*number +"");
out.flush();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The client side looks ok. Just replace out.println("Ping"); with out.println("50"); or whatever you want.
You first start the server and then you can start multiple client applications. If you have any errors you can then post them here and have a look on an exact scenario.
I have one TCP-Client and one TCP-Server written in Java. The Server is waiting for instructions from the Client but should also be able to send instructions to the client.
I can make the Client send something to the Server and wait for a reply. But I can not make it like waiting for a message without sending something before.
TCP-Client
public class TCPClient {
static DataOutputStream toServer;
static BufferedReader fromServer;
static Socket socket;
public static void main(String[] args) throws Exception {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("*************************");
System.out.println("* Client *");
System.out.println("*************************");
System.out.println("INSTRUCTION | EFFECT");
System.out.println("aktiv | ready to do something");
System.out.println("exit | disconnect");
System.out.println();
System.out.print("Please enter the IP-Address of the Server: ");
String ip = input.readLine();
System.out.println();
try {
socket = new Socket(ip, 9999);
} catch (Exception e) {
System.out.println("Can not connect to Server!");
}
toServer = new DataOutputStream(socket.getOutputStream()); // Datastream FROM Server
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (sendRequest()) {
receiveResponse();
}
socket.close();
toServer.close();
fromServer.close();
}
private static boolean sendRequest() throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line;
boolean holdTheLine = true; // Connection exists
System.out.print("> ");
line = input.readLine();
switch (line) {
case "aktiv":
toServer.writeBytes("active" + '\n');
break;
case "exit":
holdTheLine = false;
break;
default:
break;
}
return holdTheLine;
}
private static void receiveResponse() throws IOException {
System.out.println("Server: " + fromServer.readLine() + '\n');
}
}
TCP-Server
public class TCPServer {
static boolean connected = true;
public static void main(String[] args) throws Exception {
System.out.println("********************************");
System.out.println("* Server *");
System.out.println("********************************");
System.out.println("INSTRUCTION | EFFECT");
System.out.println("ok | send an ok to client");
ServerSocket listenSocket = new ServerSocket(9999);
while (true) {
final Socket client = listenSocket.accept();
Thread newClientThread = new Thread(new Runnable() {
#Override
public void run() {
multithreadedServer(client);
}
});
newClientThread.start();
}
}
public static void multithreadedServer(Socket client) {
String line;
final BufferedReader fromClient;
final DataOutputStream toClient;
Thread cmdForClient;
try {
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
toClient = new DataOutputStream(client.getOutputStream());
while (connected) {
cmdForClient = new Thread(new Runnable() {
#Override
public void run() {
try {
String line = fromClient.readLine();
System.out.println("Client: " + line);
if (line.equals("exit")) {
connected = false;
}
} catch (Exception e) {
System.out.println(e);
}
}
});
cmdForClient.start();
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
String reply = input.readLine();
if (reply.equals("ok")) {
toClient.writeBytes("OK." + '\n');
}
} catch (Exception e) {
System.out.println(e);
}
}
fromClient.close();
toClient.close();
client.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
The typical operation for a client-server scenario is for the client sending requests to the server. However, in peer to peer applications, both endpoints might act as both clients and servers. The only difference would be which endpoint that opened the connection. In your case, the problem is that only the "server" is using a receiver thread. Start a receiver thread at the client side and your problem should be solved. You should pretty much be able to just reuse your threaded code from the server in the client. Just pass the socket to the receive thread after opening the connection to the server.
EDIT:
In your client:
Thread newServerThread = new Thread(new Runnable() {
#Override
public void run() {
multithreadedServer(socket);
}
});
newServerThread.start();
where socket, is the socket to the server. You may need to update multithreadedServer for any specifics or differences for the clients operations, but the principle should be the same.
So I've created a server and a client for a previous part of the assignment, but now I have to extend/modify the server so that it can handle multiple clients concurrently.
I know I have to do something along the lines of
Server server1 = new Server();
Thread thread = new Thread(server1);
thread.start();
and have the Class Server implement Runnable.
But the lecturers notes on multi-threading are not very clear and I have been starring at this part of the question for a long time and have got no where.
Below is the code I have written for one client at a time to connect to the server.
Any help would be much appreciated.
Server.java
public class Server {
ArrayList<String> tokens = new ArrayList<String>();
private Socket s;
private Scanner in;
private PrintWriter out;
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(1234);
Server serverInstance = new Server();
System.out.println("Server running. Waiting for a client to connect...");
while (true) {
serverInstance.s = server.accept();
System.out.println("Client connected");
serverInstance.run();
System.out.println("Client disconnected. Waiting for a new client to connect...");
}
}
public void start() {
System.out.println("Starting " + threadName);
if (t == null) {
t = new Thread(this, threadName);
t.start();
}
}
public void run() {
try {
try {
in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream());
doService(); // the actual service
}
finally {
s.close();
}
}
catch (IOException e) {
System.err.println(e);
}
}
public void doService() throws IOException {
while (true) {
if (!in.hasNext())
return;
String request = in.next();
System.out.println("Request received: " + request);
// (...) test for type of request here (not implemented)
Request(request);
}
}
public void Request(String request) {
String amountStr = in.next();
if (request.startsWith("SUBMIT")) {
if (tokens.size() < 10) {
tokens.add(amountStr);
System.out.println("Token added");
out.println("OK");
}
else {
System.err.println("Error");
out.println("Error");
}
}
else if (request.startsWith("REMOVE")) {
if (tokens.contains(amountStr)) {
tokens.remove(amountStr);
System.out.println("Tokens removed");
out.println("OK");
}
else {
System.err.println("Error");
out.println("Error");
}
}
else if (request.equals("QUIT")) {
System.err.println("Program ended");
out.println("Program ended");
}
tokens.sort(null);
System.out.println(tokens);
out.flush();
}
}
Client.java
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 1234);
InputStream instream = s.getInputStream();
OutputStream outstream = s.getOutputStream();
Scanner in = new Scanner(instream);
PrintWriter out = new PrintWriter(outstream);
String request = "SUBMIT hello \n";
out.print(request);
out.flush();
String response = in.nextLine();
System.out.println("Token: " + response);
s.close();
}
}
What you are trying to duplicate is what RMI has been doing for decades. Java is open source so you can look at how RMI works and learn from there. You can also look on the internet for myriad examples of multi threaded servers with/without RMI.
I have 2 classes, a server and a client. The server uses multiple threads to accept many clients. So x clients can join the same server. However in an attempt to identify the threads from the client method, I seem to have found that its not making multiple threads as the ID are the same for all clients. The code I have is as follows:
SERVER:
public class Server
{
ServerSocket serverSocket;
int portNumber;
public static volatile String userInput;
public volatile int noOfClients = 0;
public static void main(String[] args)
{
Server s = new Server();
s.startup();
}
/**
* Start the server on the user picked port
*/
public void startup()
{
try
{
System.out.println("Enter a port");
Scanner dif = new Scanner(System.in);
portNumber = Integer.parseInt(dif.nextLine());
dif.close();
serverSocket = new ServerSocket(portNumber);
newThread();
}
catch (IOException e) {
System.out.println("Error");
System.exit(0);
}
}
public void newThread()
{
Thread thread =new Thread()
{
public void run()
{
while(true) {
try {
accept();
} catch (Exception e) {
System.out.println("Error");
}
}
}
};
thread.start();
}
public void accept()
{
try
{
Socket clientSocket = serverSocket.accept();
new Thread(new ClientSocket(clientSocket)).start();
System.out.println("A new client has just connected.");
noOfClients++;
} catch(IOException e)
{
System.out.println("Error");
System.exit(0);
}
}
class ClientSocket implements Runnable {
Socket clientSocket;
public ClientSocket(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
{
try
{
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while (true)
{
userInput = in.readLine();
}
} catch (IOException e)
{
System.out.println("Error");
}
}
}
}
}
CLIENT:
public class Client
{
Socket clientSocket;
public static int threadName;
public static void main(String[] args) throws IOException {
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try {
Socket serverSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(serverSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
Thread thread = Thread.currentThread();
System.out.println("RunnableJob is being run by " + thread.getName() + " (" + thread.getId() + ")");
String userInput;
while ((userInput = stdIn.readLine()) != null)
{
out.println(userInput);
System.out.println("Server: " + userInput);
}
} catch(UnknownHostException e) {
System.out.println("error in host");
} catch(IOException e) {
System.out.println("error in IO");
}
}
}
When running two separate clients, the
System.out.println("RunnableJob is being run by " + thread.getName() + " (" + thread.getId() + ")");
line of code prints out the same. How can i fix it so that each new client connection is started within is own UNIQUE thread. so 2 clients will have 2 threads in total? Thanks :)
First, you are checking the thread ids for the clients, which are separate from each other, so that won't work.
However using a thread id is not a very good way to identify clients. Instead why don'y you keep a count of the number of clients, then when a new one joins, increment the number and give the client object that number as an id.
Multiple clients will connect at different port no with server. You can use that port no to distinguish between clients.
You can store ClientSocket some where to retrieve other information of each client in future if needed as shown in below code.
Here is the code:
private static HashMap<Integer, ClientSocket> clientInfo = new HashMap<Integer, ClientSocket>();
class ClientSocket implements Runnable {
Socket clientSocket;
public ClientSocket(Socket clientSocket) {
this.clientSocket = clientSocket;
System.out.println(clientSocket.getPort());
clientInfo.put(clientSocket.getPort(), this);
}
...
Read more about Java Server with Multiclient communication.
Problem:
I have written one java socket server which send response when I send first message from the client to it. But I want to send one more message based on the first response. After the first response i am not getting any other response?
Here is the Server code:
public class SendSmsServerSocket {
private final static CxpsLogger logger = CxpsLogger.getLogger(SendSmsServerSocket.class);
SendSmsServerSocket(){
try {
new SeverSocketForSms(new ServerSocket(4330));
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class SeverSocketForSms extends Thread {
private Socket socket;
private ServerSocket serversocket;
private volatile static boolean running = true;
public SeverSocketForSms(ServerSocket ssocket) throws IOException {
this.serversocket = ssocket;
start();
}
public void run() {
try{
while(running) {
this.socket = serversocket.accept();
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String msg = br.readLine();
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("inside SeverSocketForSms: msg received is : "+msg);
}
}
catch(Exception e){
e.printStackTrace();
}
catch(Throwable t) {
System.out.println("Caught " + t + "xmlServerThread - closing thread");
}
}
public static void shutdown() {
System.out.println("Stopping socket connection and thread");
try{
socket.close();
}catch (Exception e) {
e.printStackTrace();
}
running = false;
}
public static void main (String [] args){
try {
System.out.println("Starting sms server ----- Please press Enter Key to stop the server after receiving one message");
SendSmsServerSocket s=new SendSmsServerSocket();
new Scanner(System.in).nextLine();
SeverSocketForSms.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Once you have an incoming connection, you should delgate the responsibility for handling that incoming connection to another Thread, otherwise you will block your "accept" thread until the current connection is closed...
while (running) {
this.socket = serversocket.accept();
Thread thread = new Thread(new Handler(socket));
thread.start();
}
And the Handler
public class Handler implements Runnable {
private Socket socket;
public Handler(Socket socket){
this.socket = socket;
}
public void run() {
// You may need to add a repeat and exit clause here...
InputStreamReader ir = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(ir);
String msg = br.readLine();
PrintStream ps = new PrintStream(socket.getOutputStream());
ps.println("inside SeverSocketForSms: msg received is : " + msg);
}
}