-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
296 lines (260 loc) · 5.94 KB
/
Copy pathChatServer.java
File metadata and controls
296 lines (260 loc) · 5.94 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.lang.*;
public class ChatServer
{
public static void main(String args[])
{
LoginFrame frm=new LoginFrame("Chat Server");
Toolkit theKit=frm.getToolkit();
Dimension wndSize=theKit.getScreenSize();
frm.setBounds(wndSize.width/3,wndSize.height/4,wndSize.width/3,wndSize.height/4);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}
class LoginFrame extends JFrame
{
public String tStr;
public JPanel pan;
public JTextField tLogin;
public JLabel lLogin;
public JButton bLogin;
public LoginFrame(String str)
{
super(str);
pan=new JPanel(new FlowLayout(FlowLayout.CENTER,30,20));
lLogin=new JLabel("Enter the User Name");
tLogin=new JTextField(20);
bLogin=new JButton("LOGIN");
pan.add(bLogin);
Box top=Box.createHorizontalBox();
top.add(Box.createHorizontalStrut(20));
top.add(lLogin);
top.add(Box.createHorizontalStrut(20));
top.add(tLogin);
top.add(Box.createHorizontalStrut(20));
Box ver=Box.createVerticalBox();
ver.add(Box.createVerticalStrut(40));
ver.add(top);
Container content=getContentPane();
content.setLayout(new BorderLayout(30,10));
content.add(ver,BorderLayout.CENTER);
content.add(pan,BorderLayout.SOUTH);
MyBtnListener listen=new MyBtnListener();
bLogin.addActionListener(listen);
}
private class MyBtnListener implements ActionListener
{
public void actionPerformed(ActionEvent evn)
{
tStr=tLogin.getText();
MyFrame frm1=new MyFrame("Chat Server");
Toolkit theKit=frm1.getToolkit();
Dimension wndSize=theKit.getScreenSize();
frm1.setBounds(wndSize.width/4,wndSize.height/4,wndSize.width/2,wndSize.height/2);
frm1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm1.setVisible(true);
try
{
frm1.startService(tStr);
}
catch(Exception e)
{}
dispose();
}
}
}
class MyFrame extends JFrame
{
public JPanel pan;
public JLabel lSend;
public JLabel lWanna;
public JTextArea tSend;
public JTextArea tLog;
public JTextArea tWanna;
public JScrollPane sSend;
public JScrollPane sLog;
public JScrollPane sWanna;
public JButton bSend;
public JButton bLogout;
public Box top;
public Box down;
public Box mn;
public Container content;
public String name;
public MultiThreadClient mult;
public MyFrame(String str)
{
super(str);
pan=new JPanel();
pan.setLayout(new FlowLayout(FlowLayout.CENTER,100,10));
lSend=new JLabel("Send Items",JLabel.CENTER);
lWanna=new JLabel("Wanna Send",JLabel.CENTER);
tSend=new JTextArea(5,10);
tLog=new JTextArea(5,5);
tWanna=new JTextArea(5,15);
sSend=new JScrollPane(tSend);
sLog=new JScrollPane(tLog);
sWanna=new JScrollPane(tWanna);
JButton bSend=new JButton("SEND");
JButton bLogout=new JButton("LOGOUT");
pan.add(bSend);
pan.add(bLogout);
top=Box.createHorizontalBox();
top.add(Box.createHorizontalStrut(20));
top.add(lSend);
top.add(Box.createHorizontalStrut(10));
top.add(sSend);
top.add(Box.createHorizontalStrut(70));
top.add(sLog);
top.add(Box.createHorizontalStrut(100));
down=Box.createHorizontalBox();
down.add(Box.createHorizontalStrut(20));
down.add(lWanna);
down.add(Box.createHorizontalStrut(10));
down.add(sWanna);
down.add(Box.createHorizontalStrut(10));
mn=Box.createVerticalBox();
mn.add(Box.createVerticalStrut(30));
mn.add(top);
mn.add(Box.createVerticalStrut(20));
mn.add(down);
mn.add(Box.createVerticalStrut(30));
content=getContentPane();
content.setLayout(new BorderLayout());
content.add(mn,BorderLayout.CENTER);
content.add(pan,BorderLayout.SOUTH);
MyBtnSendListener listenSend=new MyBtnSendListener();
MyBtnLogoutListener listenLogout=new MyBtnLogoutListener();
bSend.addActionListener(listenSend);
bLogout.addActionListener(listenLogout);
}
class MyBtnSendListener implements ActionListener
{
public void actionPerformed(ActionEvent evn)
{
try
{
mult.clientChat();
}
catch(Exception e)
{}
}
}
class MyBtnLogoutListener implements ActionListener
{
public void actionPerformed(ActionEvent evn)
{
try
{
mult.logOut();
}
catch(Exception e)
{}
}
}
public void startService(String str4) throws Exception
{
name=str4;
mult=new MultiThreadClient();
}
class MultiThreadClient
{
Socket s;
DataInputStream din;
DataOutputStream dout;
public MultiThreadClient()
{
try
{
s=new Socket("localhost",10);
//dout.writeUTF(name);
dout=new DataOutputStream(s.getOutputStream());
din=new DataInputStream(s.getInputStream());
dout.writeUTF(name);
dout.flush();
My m=new My(din);
Thread t1=new Thread(m);
t1.start();
}
catch(Exception e)
{}
}
public void clientChat() throws Exception
{
String s1="";
s1=tWanna.getText();
s1=name+" : "+s1;
dout.writeUTF(s1);
dout.flush();
tWanna.setText("");
}
public void logOut() throws Exception
{
dout.writeUTF("^");
dout.flush();
dispose();
return;
}
}
class My implements Runnable
{
DataInputStream din;
public My(DataInputStream din)
{
this.din=din;
}
public void run()
{
String s2="";
do
{
try
{
s2=din.readUTF();
FMatch mat=new FMatch();
if(mat.findMatch(s2))
{
tLog.setText("");
s2=din.readUTF();
while(!mat.findMatch(s2))
{
s2=tLog.getText()+"\n"+s2+" is logged in";
tLog.setText(s2);
s2=din.readUTF();
}
}
else if(s2.equals("%"))
{
s2=din.readUTF();
s2=tLog.getText()+"\n"+s2;
tLog.setText(s2);
}
else
{
s2=tSend.getText()+"\n"+s2;
tSend.setText(s2);
}
}
catch(Exception e)
{}
}while(!s2.equals("stop"));
}
}
}
class FMatch
{
public boolean findMatch(String str)
{
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='^')
return true;
}
return false;
}
}