Skip to content

Commit b248459

Browse files
committed
This is first multithreading server
0 parents  commit b248459

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

Client.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Client contains the class to create a client
3+
*/
4+
import java.io.*;
5+
import java.net.*;
6+
import java.util.*;
7+
8+
9+
// Client Class
10+
public class Client {
11+
12+
//drive code
13+
public static void main(String[] args) {
14+
// established a connection by provided host and port number
15+
try( Socket socket = new Socket("localhost", 1234)) {
16+
17+
// writing to server
18+
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
19+
20+
// reading from server
21+
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
22+
23+
// object of scanner class
24+
Scanner sc = new Scanner(System.in);
25+
String line = null;
26+
27+
while(!"exit".equalsIgnoreCase(line)) {
28+
29+
// reading from user
30+
line = sc.nextLine();
31+
32+
// sending the user input to server
33+
out.println(line);
34+
// clearing stream
35+
out.flush();
36+
37+
// displaying server reply
38+
System.out.println("Server replied " + in.readLine());
39+
}
40+
41+
// closing the scanner object
42+
sc.close();
43+
44+
}
45+
catch (IOException e){
46+
e.printStackTrace();
47+
}
48+
}
49+
50+
}

Server.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Has 2 classes one class:
3+
* Server to create a Server
4+
* CLientHandler ot handle clients using multithreading
5+
*
6+
* Life Cycle of multithreads:
7+
* New
8+
* - New Thread created, is in new state, the code has not been run yet
9+
* Active
10+
* -(New thread created when invokes start(), it moves from new to active state)
11+
* -Runnable: Ready to run and is moved to runnable state, ready to run at anytime
12+
* -Running: Thread gets CPU, moves from runnable to running state. (Generally from runnable to running and again back to runnable)
13+
* Blocked / Writing
14+
* - Whenever a thread is inactive for a span of time(not permanently) then the thread is either in blocked or waiting state
15+
* Timed Waiting
16+
* -Sometimes waiting for leads to starvation, For example, a thread (its name is A) has entered the critical section of a code and is not willing to leave that critical section. In such a scenario, another thread (its name is B) has to wait forever, which leads to starvation. To avoid such scenario, a timed waiting state is given to thread B. Thus, thread lies in the waiting state for a specific span of time, and not forever. A real example of timed waiting is when we invoke the sleep() method on a specific thread. The sleep() method puts the thread in the timed wait state. After the time runs out, the thread wakes up and start its execution from when it has left earlier.
17+
* -sleep() normally used for timed waiting
18+
* Terminated
19+
* - Multiple ways that a thread can terminate
20+
* - When a thread finishes its job, it exits normally
21+
* - Abnormal Termination:
22+
* - Occurs when some unusual events such as unhandled exception or segmentation fault
23+
*/
24+
import java.io.*;
25+
import java.net.*;
26+
27+
28+
// Server class
29+
class Server {
30+
public static void main(String[] args) {
31+
ServerSocket server = null;
32+
33+
try {
34+
35+
// server is listening on port "1234"
36+
server = new ServerSocket(1234);
37+
server.setReuseAddress(true);
38+
39+
40+
// running infinite loop for geting client request
41+
while(true) {
42+
43+
// returns new socket
44+
Socket client = server.accept();
45+
// returns remote IP address to which the socket is connected or returns null is the socket is not connected
46+
InetAddress addy = client.getInetAddress();
47+
// returns raw IP address in a string format
48+
String remoteIp = addy.getHostAddress();
49+
50+
51+
// Displaying that new client is connected to server
52+
System.out.println("New client connected" + addy + remoteIp);
53+
54+
// create a new thread object
55+
ClientHandler clientSock = new ClientHandler(client);
56+
57+
// This thread will handle the client seperately
58+
new Thread(clientSock).start();
59+
}
60+
} catch (IOException e) {
61+
e.printStackTrace();
62+
}
63+
finally {
64+
if (server != null) {
65+
try {
66+
server.close();
67+
} catch (IOException e) {
68+
e.printStackTrace();
69+
}
70+
}
71+
}
72+
}
73+
74+
// ClientHandler class
75+
private static class ClientHandler implements Runnable {
76+
77+
private final Socket clientSocket;
78+
79+
// Construtor
80+
public ClientHandler(Socket socket) {
81+
this.clientSocket = socket;
82+
}
83+
84+
public void run() {
85+
PrintWriter out = null;
86+
BufferedReader in = null;
87+
88+
try{
89+
90+
// get the outputstream of client
91+
out = new PrintWriter(clientSocket.getOutputStream(), true);
92+
93+
// get the inputStream of client
94+
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
95+
96+
String line;
97+
while ((line = in.readLine()) != null) {
98+
99+
// Writing the recieved message from client
100+
System.out.printf(" Sent from the client: %s\n", line);
101+
out.println(line);
102+
}
103+
} catch (IOException e) {
104+
e.printStackTrace();
105+
}
106+
finally {
107+
try {
108+
if (out != null) {
109+
out.close();
110+
}
111+
if (in != null) {
112+
in.close();
113+
}
114+
}
115+
catch (IOException e) {
116+
e.printStackTrace();
117+
}
118+
}
119+
}
120+
121+
}
122+
123+
}

0 commit comments

Comments
 (0)