-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
130 lines (120 loc) · 3.16 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
import java.io.*;
import java.net.*;
import java.util.LinkedList;
public class Server implements Runnable{
ServerSocket server;
Socket socket;
LinkedList<CustomClient> clients;
Thread startThread;
CustomClient temp;
public Server(ServerSocket server){
this.server=server;
this.clients=new LinkedList<>();
this.startThread=new Thread(this);
}
public static void main(String[] args){
try{
Server serv=new Server(new ServerSocket(8080));
serv.startThread.start();
}catch(Exception e){
System.out.println(e);
}
}
@Override
public void run(){
int id=0;
while(this.clients.size()<=100){
try{
this.socket=this.server.accept();
this.temp=new CustomClient(this.socket,this.socket.getInetAddress().getHostAddress(),this.socket.getPort(),new DataInputStream(this.socket.getInputStream()),id,new MessageThread(this));
String result="";
if(this.clients.size()==0){
result="#Server : No Users are currently available !";
}else{
result+="#Server : Available Users = ";
for(CustomClient cc:this.clients){
cc.dos.writeUTF("#Server : "+temp.name+" joined the chat!");
cc.dos.flush();
result+=cc.name+";";
}
}
temp.dos.writeUTF(result);
temp.dos.flush();
this.clients.add(temp);
id++;
}catch(Exception e){}
}
try{
this.startThread.join();
}catch(Exception e){}
}
}
class CustomClient{
int id;
Socket socket;
String ipv4Address;
int port;
String name;
DataInputStream dis;
DataOutputStream dos;
MessageThread mt;
Thread thread;
public CustomClient(Socket socket,String ipv4Address,int port,DataInputStream dis,int id,MessageThread mt){
this.socket=socket;
this.ipv4Address=ipv4Address;
this.port=port;
this.dis=dis;
this.id=id;
this.mt=mt;
this.mt.client=this;
try{
this.dos=new DataOutputStream(this.socket.getOutputStream());
this.name=(String)dis.readUTF();
}catch(Exception e){}
this.thread=new Thread(this.mt);
this.mt.thread=this.thread;
this.thread.start();
}
}
class MessageThread implements Runnable{
Server parent;
CustomClient client;
Thread thread;
public MessageThread(Server parent){
this.parent=parent;
}
@Override
public void run(){
while(!this.client.socket.isClosed()){
try{
String message=this.client.dis.readUTF();
if(message!=null){
if(message.length()>0){
System.out.println(this.client.name+" : "+message);
for(CustomClient cc:this.parent.clients){
if(cc!=this.client){
try{
cc.dos.writeUTF("> "+this.client.name+" : "+message);
cc.dos.flush();
}catch(Exception exc){}
}
}
if(message.equals("quit")||message.equals("bye")){
try{
this.parent.clients.remove(this.client);
for(CustomClient cc:this.parent.clients){
cc.dos.writeUTF("Server : "+this.client.name+" left the chat!");
cc.dos.flush();
}
this.thread.join();
this.client.socket.close();
}catch(Exception e){}
}
}
}else{
continue;
}
}catch(Exception e){}
}
}
}