-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerHandle.java
92 lines (87 loc) · 2.63 KB
/
ServerHandle.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
package Assignment4;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.NoSuchElementException;
public class ServerHandle extends Thread
{
private Socket clientsocket;
private ObjectOutputStream output;
private BufferedReader input;
private Server server;
private String fromClient,clientName;
private int count = 1;
private HashMap<Socket,String> connected_client ;
public ServerHandle(Server server,Socket clientsocket)throws IOException{
this.server = server;
this.clientsocket = clientsocket;
this.output = new ObjectOutputStream(clientsocket.getOutputStream());
this.input = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
this.connected_client = new HashMap<Socket,String>();
}
@SuppressWarnings("deprecation")
@Override
public void run() {
try {
/*send current user all other logins
for(ServerHandle obj: server.get_connectedList())
{
if((obj.clientsocket != clientsocket) && clientsocket != null )
{
String msgString = clientName + " online!!\n";
obj.send(msgString);
}
}*/
do
{
fromClient = input.readLine();
//System.out.println("In Server");
//System.out.println("HI");
//System.out.println(fromClient);
if(fromClient != null && fromClient.length()!=0)
{
int index = fromClient.indexOf(" ");
if(count == 1)
{
this.clientName = fromClient.substring(0,index);
count--;
}
System.out.println("Client Name:" + clientName);
connected_client.put(clientsocket,clientName);
System.out.println(fromClient);
int index1 = fromClient.lastIndexOf(" ");
// send message to all connected clients
for(ServerHandle obj: server.get_connectedList())
{
if((obj.clientsocket != clientsocket)&& obj.clientsocket!=null)
obj.send(fromClient);
}
if(fromClient.substring(index1+1,fromClient.length()).equalsIgnoreCase("Quit"))
{
server.get_connectedList().remove(this);
break;
}
}
}while(true);
} catch (SocketException e) {
System.out.println(connected_client.get(this.clientsocket) + " disconnected abruptly....!!");
}catch (IOException e) {
e.printStackTrace();
}
catch (NoSuchElementException e) {
System.out.println("Quitting!!!");
}finally {
try {
this.clientsocket.close();
this.input.close();
this.output.close();
Thread.currentThread().stop();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void send(String msg)throws IOException {
output.writeObject(msg);
}
}