Write a multi-threaded Server program to handle requests from multiple Client programs. The objective is to learn how to

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Write a multi-threaded Server program to handle requests from multiple Client programs. The objective is to learn how to

Post by answerhappygod »

Write a multi-threaded Server program to handle requests frommultiple Client programs. The objective is to learn how to useServerSocket and Java Thread. The Server will be “listening” onport 5525 for the connection requests from the Clients. You can runthe Server and the Clients on the same machine. In this case, theServer will be a “localhost” with the IP address 127.0.0.1. You canuse the Client program in Program 1 to test your Server. Once aconnection request is accepted, the Server creates a new thread tohandle the connection and keeps listening on its own localport foradditional connection requests. That is, your Server must be ableto handle multiple connections simultaneously. The Server is alsorequired to maintain a log file that records every connection.
Write A Multi Threaded Server Program To Handle Requests From Multiple Client Programs The Objective Is To Learn How To 1
Write A Multi Threaded Server Program To Handle Requests From Multiple Client Programs The Objective Is To Learn How To 1 (143.79 KiB) Viewed 25 times
Write A Multi Threaded Server Program To Handle Requests From Multiple Client Programs The Objective Is To Learn How To 2
Write A Multi Threaded Server Program To Handle Requests From Multiple Client Programs The Objective Is To Learn How To 2 (62.24 KiB) Viewed 25 times
Program Requirements 1. Your Server must not crash under any situation and must work with the Client program from Program 1. 2. You must try-catch everything and output appropriate error messages identifying the specific exception. 3. You MUST have two classes in your Server program: Server and ServerThread. o Server class should have exactly two public methods: main() and run () (you may have other private methods). The main () method of the Server should simply create a new Server object and call the run () method on it. Exceptions generated from this class must be caught, and the error messages must be displayed on the Server console. In the run() method, do the following: a. Create a new ServerSocket listening on port 5525 (refer to the socket tutorial in Canvas). b. Create a new PrintWriter object with a new FileOutputStream that writes to a log file called prog2.log, which should be stored in your project folder. Make sure the second argument to the PrintWriter constructor is true. Every Server Thread will write connection information to the same log file. Each time your server comes up, you can either overwrite or append to prog2.log. c. Have an infinite while loop that: i. Listens to connection requests using the .accept() method. Once a connection is accepted, you shouldcreate a new instance of Socket to handle the connection. ii. Creates a new instance of Server Thread to handle the connection. When you're creating a new Server Thread object, use the socket object and the log file as arguments. Therefore, each Server Thread can communicate with the Client socket and write to the same log file. iii. Calls the start () method of the Server Thread object to start the thread. 4. The Server Thread class should extend the Thread class and should NOT be designated as public. a. Declare one Socket object, two PrintWriters objects (one for the socket I/O and one for the log file), and one BufferedReader object, outside of all methods, so they are visible in the Server Thread class. b. Define a constructor with the interface: public ServerThread (Socket client Sock, PrintWriter logfile) c. Since Server Thread extends Thread, it MUST have a run() method, which is the method that eventually gets called after you call start () on a Thread. The run method should do the following: i. Write a line to the log file indicating that a connection was received. You should write the date/time the connection was received, the remote IP address, and the remote port of the connection. You may find the following methods useful for your log file: Date().toString(), getInetAddress (), and getPort () of the Socket class. ii. Have a while loop that deals with receiving/sending the messages. iii. When the Client disconnects (quit), you should print an appropriate message to the log file saying the connection on some port number is closed. DO NOT close the PrintWriter for the log file. Just close the Client socket and return from the run() method. Doing so effectively kills the Server Thread. iv. All output from ServerThread must go to the log file. No output may be sent to System.out.
5. Your Server.java and ServerThread.java should look something like these: import java.io.*; import java.net.*; import java.util.*; class Server { public static void main(String argv[]) { } public void run() { } } class ServerThread extends Thread public ServerThread (Socket client Sock, PrintWriter logfile) //constructor { } public void run() { 6. Log file entries may look something like these: Got a connection: Fri Sep 05 13:40:16 CDT 2018 /192.168.100.250 Port: 9312 Connection closed. Port: 9312 Got a connection: Mon Sep 15 13:32:41 CDT 2018 /192.168.100.234 Port: 11132 ServerThread Exception: java.net.SocketException: Connection reset 7. You can run and test your Server and Client on one machine. To do so, bring up your Server first and run your Clients with "localhost" (127.0.0.1) and port 5525. W M 8. If your programs work on the same machine, run and test your Server and Client with different machines in the labs. You could use the "ipconfig" command at the command prompt under Windows to find out the IP address of the machine running your Server program. Run your Client program with that IP address to connect to your Server. 9. You must zip all files for Program 2 and submit the zip file to Program 2-Zip File by the due date.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply