-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.java
More file actions
297 lines (267 loc) · 6.86 KB
/
server.java
File metadata and controls
297 lines (267 loc) · 6.86 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
297
/*
Author : Amandeep Anguralla
Version : 1.5
Description : Implementation of server.
Must be Run on a single machine in a neetwork.
Lables Must be placed in same folder as that of code.
*/
// Import header files
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class server
{
// Declaring global variables
Socket sock;
JButton exit,clear;
JFrame frame;
JTextArea info;
BufferedReader br;
PrintWriter pw;
TrayIcon ic;
// setting port where all communication takes place
static final int PORT=1025;
// Stores all the connected clients
ArrayList connected;
// Main meathod
public static void main(String[] args)
{
server ser=new server();
ser.go();
ser.networking();
}
public void go()
{
//Setting frame and its Properties
frame=new JFrame("Server");
frame.setResizable(false);
frame.setLocation(750,370);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Adding buttons to frame
clear=new JButton("Clear");
clear.setToolTipText("Clear the information window.");
// Adding Images and icons
ImageIcon ico=new ImageIcon("labels/info.gif");
ImageIcon icon=new ImageIcon("labels/server.png");
frame.setIconImage(icon.getImage());
// Setting Lables
JLabel l1=new JLabel("Information Messages",ico,JLabel.CENTER);
l1.setToolTipText("It displays users information those who are connecting or leaving server.");
// Setting fonts for text area
Font f=new Font("Comic Sans MS",Font.BOLD,15);
Font f1=new Font("Ariel",Font.BOLD,15);
l1.setFont(f1);
ImageIcon ex=new ImageIcon("labels/exit.png");
// Adding buttons and text fields to Frame
exit=new JButton("Exit",ex);
info=new JTextArea(13,30);
info.setFont(f);
info.setToolTipText("Information Window.");
exit.setToolTipText("Stop and Exit Server");
info.setEditable(false);
info.setLineWrap(true);
// Adding vertical and Horizontal scroll bars and setting their properties
JScrollPane jp=new JScrollPane(info);
jp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
// If supported then minimize to System tray on pressing minimize Button
if(SystemTray.isSupported())
{
ic=new TrayIcon(icon.getImage());
ic.setToolTip("Chat Server");
ic.addMouseListener(new MouseAdapter()
{
// Setting Mouse Handler
@Override
public void mouseClicked(MouseEvent ae)
{
frame.setVisible(true);
frame.setExtendedState(frame.NORMAL);
SystemTray.getSystemTray().remove(ic);
}
});
}
// Set Theme for icons
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
}
catch(Exception e)
{
info.append(e+"\n");
}
// Adding pannels to Frame
JPanel pane=new JPanel();
JPanel pane1=new JPanel();
JPanel pane2=new JPanel();
// Setting adjustment listener to control vertical scrollbar
jp.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()
{
public void adjustmentValueChanged(AdjustmentEvent ae)
{
ae.getAdjustable().setValue(ae.getAdjustable().getMaximum());
}
});
// set window handler
frame.addWindowListener(new WindowAdapter()
{
// Action on closing window
@Override
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog(frame,"Server Turned off!!");
}
// Action on maximizing Window
public void windowIconified(WindowEvent e)
{
frame.setVisible(false);
try
{
SystemTray.getSystemTray().add(ic);
}
catch(Exception ex){}
}
});
// Adding handlers to text field
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==clear)
{
info.setText("");
}
}
});
// Register button Handlers
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
// When Exit button is pressed
if(ae.getSource()==exit)
{
int c=JOptionPane.showConfirmDialog(frame,"Are you Sure?","Conformation",JOptionPane.YES_NO_OPTION);
if(c==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
});
// Set allignment of components in frame
pane2.add(BorderLayout.NORTH,l1);
pane.add(BorderLayout.CENTER,jp);
frame.add(BorderLayout.NORTH,pane2);
pane1.add(clear);
pane1.add(exit);
frame.getContentPane().add(BorderLayout.CENTER,pane);
frame.getContentPane().add(BorderLayout.SOUTH,pane1);
frame.pack();
frame.setVisible(true);
}
// Add new threads when new user enters
class reader implements Runnable
{
BufferedReader br;
public reader(Socket clientsocket)
{
try
{
sock=clientsocket;
// Getting input stream
InputStreamReader ir=new InputStreamReader(sock.getInputStream());
br=new BufferedReader(ir);
}
catch(IOException ex)
{
info.append(ex+"\n");
}
}
// thread handler
public void run()
{
String msg,n;
try
{
// If message is not empty then broadcast message
while((msg=br.readLine())!=null)
{
telleveryone(msg);
}
}
catch(SocketException ex)
{
info.append("Client Disconnected.\n");
}
catch(IOException ex)
{
info.append(ex+"\n");
}
}
}
//Meathod to handle networking Activities
public void networking()
{
connected=new ArrayList();
String con;
try
{
// Create new socket
ServerSocket ss=new ServerSocket(PORT);
info.append("Server Started at port : "+PORT+"\n");
while(true)
{
try
{
// Get socket connection
Socket clientsocket=ss.accept();
InetAddress a=clientsocket.getInetAddress();
info.append(a.getHostName()+" at IP Address "+a.getHostAddress()+" Connected to server.\n");
// Get output steam
PrintWriter pw=new PrintWriter(clientsocket.getOutputStream());
connected.add(pw);
//Create and start new thread
Thread t1=new Thread(new reader(clientsocket));
t1.start();
}
catch(NullPointerException ex)
{
info.append("Null pointer Exception ");
}
catch(SocketException e)
{
info.append("Connection Reset\n");
}
}
}
catch(Exception ex)
{
info.append("Inside Exception\n");
info.append("Exception Raised : "+ex+"\n");
}
}
// Meathod to broadcast messages
public void telleveryone(String str)
{
try
{
// Iterate through list and send message to each user
Iterator ir=connected.iterator();
while(ir.hasNext())
{
PrintWriter pw=(PrintWriter)ir.next();
pw.println(str);
pw.flush();
}
}
catch(Exception ex)
{
info.append(ex+"\n");
}
}
}