Modify the socket-based date server below so that the server services each client request in a separate thread.
import java.net.*;
import java.io.*;
public class DateClient
{
public static void main(String[] args) {
try {
/* make connection to server socket */
Socket sock = new Socket(“127.0.0.1”,6013);
InputStream in = sock.getInputStream();
BufferedReader bin = new
BufferedReader(new InputStreamReader(in));
/* read the date from the socket */
String line;
while ( (line = bin.readLine()) != null)
System.out.println(line);
/* close the socket connection*/
sock.close();
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
//
Bidirectional Thread:
Client.java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) throws Exception
{
Socket sk=new
Socket("127.0.0.1",5000);
BufferedReader sin=new
BufferedReader(new InputStreamReader(sk.getInputStream()));
PrintStream sout=new
PrintStream(sk.getOutputStream());
BufferedReader stdin=new
BufferedReader(new InputStreamReader(System.in));
String s;
while (true)
{
System.out.print("Client : ");
s=stdin.readLine();
sout.println(s);
if (s.equalsIgnoreCase("BYE"))
{
System.out.println("Connection ended by client");
break;
}
s=sin.readLine();
System.out.print("Server : "+s+"\n");
}
sk.close();
sin.close();
sout.close();
stdin.close();
}
}
Server.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server {
int port;
ServerSocket server=null;
Socket client=null;
ExecutorService pool = null;
int clientcount=0;
public static void main(String[] args) throws IOException {
Server serverobj=new Server(5000);
serverobj.startServer();
}
Server(int port){
this.port=port;
pool = Executors.newFixedThreadPool(5);
}
public void startServer() throws IOException {
server=new ServerSocket(5000);
System.out.println("Server Booted");
System.out.println("Any client can stop the server by sending
-1");
while(true)
{
client=server.accept();
clientcount++;
ServerThread runnable= new
ServerThread(client,clientcount,this);
pool.execute(runnable);
}
}
private static class ServerThread implements Runnable {
Server server=null;
Socket client=null;
BufferedReader cin;
PrintStream cout;
Scanner sc=new Scanner(System.in);
int id;
String s;
ServerThread(Socket client, int count ,Server server ) throws
IOException {
this.client=client;
this.server=server;
this.id=count;
System.out.println("Connection "+id+"established with client
"+client);
cin=new BufferedReader(new
InputStreamReader(client.getInputStream()));
cout=new PrintStream(client.getOutputStream());
}
@Override
public void run()
{
int x=1;
try{
while(true){
s=cin.readLine();
System.
out.print("Client("+id+") :"+s+"\n");
System.out.print("Server : ");
//s=stdin.readLine();
s=sc.nextLine();
if (s.equalsIgnoreCase("bye"))
{
cout.println("BYE");
x=0;
System.out.println("Connection ended by server");
break;
}
cout.println(s);
}
cin.close();
client.close();
cout.close();
if(x==0) {
System.out.println( "Server cleaning up." );
System.exit(0);
}
}
catch(IOException ex){
System.out.println("Error : "+ex);
}
}
}
}
Kindly upvote if you are satisfied with the answer or feel free to comment if you have any query.If you are not satisfied with the answer please let me know what else i can provide you in the answer. plzz dont just give downvote.
Modify the socket-based date server below so that the server services each client request in a...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...
The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient { private static final String SERVER = "dict.org"; private static final int PORT = 2628; private static final int TIMEOUT = 15000; public static void main(String[] args) { Socket ...
In java write a simple 1-room chat server that is compatible
with the given client code.
9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...
Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...
I have to modify a server program and chat program to work as the following instructions but I am completely clueless as to where to start. I'd appreciate any help on how to atleast get started. This must be done in java. Diffie-Hellman Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a) and (q)....
please help. About java tcp. this is what i have so far. how to convert input value into ASCII sequence For example the message: “Hello World” would become “Ifmmp!Xpsme”For example the message: “Hello World” would become “Ifmmp!Xpsme”in server.java. Client.java: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.SocketTimeoutException; public class Client { public static void main(String[] args) throws IOException { Socket client = new Socket("127.0.0.1", 20006); client.setSoTimeout(10000); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); PrintStream out...
My client java a simple client program java .net import java.io*i public class MyClient (public static void main(String args() thrown IO Exception (part I: initialize rocket and stream BufferedReader inFromUser - new BufferedReader(new inputStreamReader(System in)); Socket clientSocket - new Socket(___. ___); DataOutputStream oldToServer - new DataOutputStream(clientSocket.getOutputStrea, ())); part 2: interact with server
How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...