Multithreaded server Consider the following source code for a simple time of day
ID: 3571360 • Letter: M
Question
Multithreaded server Consider the following source code for a simple time of day server. public class Daytime server (public static void main (String [] args) while (true) Socket client Socket - server socket - accepts(); Print writer - new print water while (true0 string time stamp = new simple date forward format (new date ()); writer, print in (time stamp0; if(writer, check error9))//we couldn't write, meaning the break; thread, sleep (5000); writer, close (); client socket, close(); When a client connect date and time on the client every 5 seconds. The server, as given can only handle one client at a time. Write a multithreaded version of this server that can handle multiple clients are the same time you may ignore exception handling in your solution.Explanation / Answer
I had written & compiled the code for you as per your specifications. You need to add one more file which will extend Thread class. Your server class will create a thread of this class for every client. Your logic will be kept inside this class's run() method.
So every time a client connects to the server, a new thread is created for it which will do the assigned work.
Below is the code for your reference :
DayTimeServer.java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class DayTimeServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listeningSocket = true;
try {
serverSocket = new ServerSocket(13000);
} catch (IOException e) {
System.err.println("Could not listen on port: 2343");
}
while(listeningSocket)
{
Socket clientSocket = serverSocket.accept();
ServerHelper helper = new ServerHelper(clientSocket);
helper.start();
}
serverSocket.close();
}
}
=======================================================
ServerHelper.java
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ServerHelper extends Thread{
private Socket socket = null;
public ServerHelper(Socket socket)
{
super("MiniServer");
this.socket = socket;
}
@Override
public void run()
{
PrintWriter writer=null;
try {
writer= new PrintWriter(socket.getOutputStream(),true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true)
{
String pattern = "yyyy-MM-dd";
String timeStamp = new SimpleDateFormat(pattern).format(new Date());
writer.println(timeStamp);
if(writer.checkError())
{
System.out.println("Error: couldn't write");
break;
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
writer.close();
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
feel free to ask if you have any doubt :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.