-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientHandler.java
60 lines (51 loc) · 1.6 KB
/
clientHandler.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
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;
public class clientHandler extends Thread
{
private Socket socket;
final DataInputStream in;
final DataOutputStream out;
boolean islogin;
private String name;
public clientHandler(Socket socket,String name ,DataInputStream in,DataOutputStream out){
this.socket=socket;
this.in=in;
this.out=out;
this.name=name;
this.islogin=true;
}
public void run()
{
try {
while(true){
String recieved=in.readUTF();
if(recieved.equalsIgnoreCase("logout"))
{
islogin=false;
socket.close();
in.close();
out.close();
break;
}
int index=recieved.indexOf("#");
String recipient= recieved.substring(index+1);
String MsgToSend=recieved.substring(0,index);
for(clientHandler ch:Server.clientsList)
{
if(ch.name.equals(recipient) && ch.islogin== true){
ch.out.writeUTF(this.name+":"+MsgToSend);
break;
}else if(ch.name.equals(recipient) && ch.islogin==false){
out.writeUTF(ch.name+" is offline");
}
}
}
} catch (IOException e) {
System.out.println("this is exception in clientHandler");
}
}
}