-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.java
More file actions
73 lines (69 loc) · 1.59 KB
/
server.java
File metadata and controls
73 lines (69 loc) · 1.59 KB
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
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
private static Socket client = null;
private static ServerSocket socket = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
//default port
int port = 1222;
if (args.length != 1) {
System.err.println("Considering Port 1222");
// System.exit(1);
}
else {
port = Integer.parseInt(args[0]);
}
try {
socket = new ServerSocket(port);
} catch(IOException e) {
System.out.println(e.getMessage());
}
int countClients = 0;
while(true) {
try {
client = socket.accept();
countClients++;
ClientThread thr = new ClientThread(client,
countClients);
thr.start();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
}
class ClientThread extends Thread {
private Socket client = null;
private int clientNo = 0;
public ClientThread(Socket socket, int n) {
clientNo = n;
client = socket;
}
public void run() {
try {
System.out.printf("Client %d Connected\n", clientNo);
BufferedReader in = new BufferedReader(new
InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(),
true);
String input;
while ((input = in.readLine()) != null) {
System.out.printf("Received from client %d: %s\n",
clientNo, input);
out.println(input);
}
in.close();
out.close();
System.out.printf("Client %d Disconnected\n", clientNo);
client.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}