-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipleClientsToServer_S.java
97 lines (66 loc) · 1.51 KB
/
MultipleClientsToServer_S.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.io.*;
import java.net.*;
class Clients extends Thread
{
Socket socket;
String clientAddress;
Clients(Socket socket)
{
this.socket=socket;
clientAddress=socket.getRemoteSocketAddress().toString();
start();
}
public void run()
{
String inMessage;
try
{
System.out.println("Connected to client :"+clientAddress);
BufferedReader xxx=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while((inMessage=xxx.readLine())!=null)
System.out.println("The Client "+clientAddress+" says : "+inMessage);
socket.close();
System.out.println("Client :"+clientAddress +" disconnected!!!");
}
catch(SocketException e)
{
System.out.println("Client :"+clientAddress +" disconnected!!!");
}
catch(IOException e)
{
System.out.println("IOException occurred!");
e.printStackTrace();
}
}
}
class MultipleClientsToServer_S
{
public static void main(String args[])
{
ServerSocket serverSocket;
Socket socket;
//listenPort=Integer.parseInt(args[0]);
int listenPort=8080;
try
{
serverSocket=new ServerSocket(listenPort);
System.out.println("Listening to port : "+listenPort);
serverSocket.setSoTimeout(100000);
while(true)
{
socket=serverSocket.accept();
new Clients(socket);
}
}
catch(SocketTimeoutException e)
{
System.out.println("The Server ain't waiting forever, nigga!");
System.exit(0);
}
catch(IOException e)
{
System.out.println("IOException occurred!");
e.printStackTrace();
}
}
}