forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmserver.java
63 lines (53 loc) · 1.54 KB
/
mserver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;
import java.net.*;
import java.io.*;
public class mserver {
public static void main(String s[]) throws Exception {
Socket sa = null;
ServerSocket ss2 = null;
System.out.println("Host starts accepting response ");
try {
ss2 = new ServerSocket(9999);
} catch (IOException e) {
System.out.println("server error");
}
while (true) {
try {
sa = ss2.accept();
System.out.println("connetion established by"+ ss2.getInetAddress());
ServerThread st = new ServerThread(sa);
st.start();
} catch (Exception e) {
System.out.println("connetion error");
}
}
}
}
class ServerThread extends Thread {
String line = null;
DataInputStream is = null;
PrintWriter od = null;
Socket s1 = null;
public ServerThread(Socket s) {
s1 = s;
}
+
public void run() {
try {
is = new DataInputStream(s1.getInputStream());
od = new PrintWriter(s1.getOutputStream());
line = is.readLine();
while (!line.equals("QUIT")) {
od.println(line);
od.flush();
System.out.println("response to client " + line);
line = is.readLine();
}
is.close();
od.close();
s1.close();
} catch (IOException ie) {
System.out.println("socket close error");
}
}
}