-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServer.java
251 lines (225 loc) · 5.77 KB
/
Server.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package intranet.source.server;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import intranet.source.users.ConnectedClients;
import java.util.LinkedHashMap;
import intranet.source.database.MessageDatabase;
class CatchClient extends Thread
{
private Socket client;
private DataOutputStream dos;
private DataInputStream dis;
private LinkedHashMap<String,ConnectedClients> users;
private LinkedHashSet<String> userlist;
private String bmsg;
private MessageDatabase db;
private String msg;
// private LinkedHashMap<String,String> pendingMessages;
private int oneTime = 0;
CatchClient(Socket client,LinkedHashMap<String,ConnectedClients> users,LinkedHashSet<String> userlist)
{
this.client = client;
try
{
this.dos = new DataOutputStream(this.client.getOutputStream());
this.dis = new DataInputStream(this.client.getInputStream());
}
catch(Exception e)
{
System.out.println("Constructor try");
e.printStackTrace();
}
this.users = users;
this.userlist = userlist;
// db = new MessageDatabase();
}
public void run()
{
try
{
boolean t = true;
System.out.println("Run Started");
System.out.println("Reading from");
String from = dis.readUTF();
System.out.println("From:"+from);
userlist.add(from);
ConnectedClients user = new ConnectedClients(from,client,dis,dos);
if(!users.containsValue(user))
{
users.put(from,user);
}
new Thread(new Runnable(){
public void run()
{
for(String s:userlist)
{
String data = new String();
for(String s1:userlist)
{
data = data+s1+"@";
}
ConnectedClients c = users.get(s);
try{
DataOutputStream doo = new DataOutputStream(c.getSocket().getOutputStream());
doo.writeUTF("userlistfromserver");
doo.writeUTF(data);
}
catch(Exception e)
{
System.out.println("In sending userlist");
}
}
}
}).start();
while(client.isConnected())
{
try
{
/*if(oneTime==0)
{
pendingMessages = db.selectMessage();
for(int i = 0;i<pendingMessages.size();i++)
{
dos.writeUTF(pendingMessages.getKey(i));
dos.writeUTF(pendingMessages.getValueAt(i));
}
}*/
boolean check = true;
String command = dis.readUTF();
String fromwhere = dis.readUTF();
System.out.println("From where:"+fromwhere);
String toClient = dis.readUTF();
System.out.println("to client:"+toClient);
if (command.equals("@messageTo@"))
{
ConnectedClients c = users.get(toClient);
if (c!=null)
{
Socket s = c.getSocket();
DataInputStream todis = new DataInputStream(s.getInputStream());
DataOutputStream todos = new DataOutputStream(s.getOutputStream());
todos.writeUTF("getMesSagefrom");
todos.writeUTF("p");
todos.writeUTF(from);
msg = dis.readUTF();
todos.writeUTF(msg);
System.out.println("messgage:"+msg);
}
/* else
{
new Thread(new Runnable()
{
public void run()
{
addToDatabase(fromwhere,toClient,msg);
}
}).start();
}*/
}
if(command.equals("bcast"))
{
bmsg = dis.readUTF();
for(String s:userlist)
{
if(!s.equals(fromwhere))
{
new Thread(new Runnable()
{
public void run()
{
try{
Thread.sleep(100);
ConnectedClients c1 = users.get(s);
if(c1!=null)
{
DataOutputStream doo = new DataOutputStream(c1.getSocket().getOutputStream());
doo.writeUTF("getMesSagefrom");
doo.writeUTF("b");
doo.writeUTF(fromwhere);
doo.writeUTF(bmsg);
System.out.println("messgage:"+bmsg);}
}
catch(Exception e)
{
System.out.println("In sending userlist");
}
}
}).start();
}
}
}
}
catch(Exception e)
{
System.out.println("In first try");
client.close();
dos.close();
dis.close();
if(!client.isConnected())
{
System.out.println("User disconnected");
}
return;
}
}
}
catch(Exception e)
{
System.out.println("In CatchClient second try");
if(!client.isConnected())
{
System.out.println("User not connected");
}
return;
}
}
/* public void addToDatabase(String from,String to,String message)
{
db.insertMessage(from,to,message);
}*/
}
class Server
{
private ServerSocket serversocket;
private LinkedHashMap<String,ConnectedClients> users;
private LinkedHashSet<String> userlist;
Server()
{
users = new LinkedHashMap<String,ConnectedClients>();
userlist = new LinkedHashSet<String>();
try
{
serversocket = new ServerSocket(4005);
}
catch(Exception e)
{
System.out.println("Creating server failed...");
e.printStackTrace();
}
}
public void acceptClient()
{
while(true)
{
try
{
Socket client = serversocket.accept();
System.out.println("Connected : "+client.getInetAddress());
new Thread(new CatchClient(client,users,userlist)).start();
}
catch(Exception e)
{
System.out.println("In Server class");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Server server = new Server();
server.acceptClient();
}
}