-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatServer.java
More file actions
190 lines (158 loc) · 5.4 KB
/
Copy pathChatServer.java
File metadata and controls
190 lines (158 loc) · 5.4 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
package chatApps;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ChatServer extends JFrame{
private static final long serialVersionUID = 1L;
private ObjectInputStream in;
private ObjectOutputStream out;
private Socket connection;
private ServerSocket server;
private int clients = 100;
private int port = 1011;
public ChatServer() {
initComponents();
this.setTitle("Server");
this.setVisible(true);
jlStatus.setVisible(true);
}
public void startRunning()
{
try
{
server = new ServerSocket(port, clients);
while(true)
{
try
{
jlStatus.setText(" Waiting for Someone to Connect...");
connection = server.accept();
jlStatus.setText(" Now Connected to "+connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
whileChatting();
}catch(EOFException eofException)
{
}
}
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
private void whileChatting() throws IOException
{
String message="";
jTextField1.setEditable(true);
do{
try
{
message = (String) in.readObject();
chatArea.append("\n"+message);
}catch(ClassNotFoundException classNotFoundException)
{
}
}while(!message.equals("Client - END"));
}
private void initComponents() {
jPanel1 = new JPanel();
jScrollPane1 = new JScrollPane();
chatArea = new JTextArea();
jTextField1 = new JTextField();
jButton1 = new JButton();
jlStatus = new JLabel();
jLabel2 = new JLabel();
jLabel1 = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBackground(new Color(51, 255, 204));
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
setForeground(new Color(102, 255, 204));
jPanel1.setBackground(new Color(154, 205, 49));
jPanel1.setLayout(null);
chatArea.setColumns(20);
chatArea.setRows(5);
jScrollPane1.setViewportView(chatArea);
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(10, 90, 480, 250);
jTextField1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jPanel1.add(jTextField1);
jTextField1.setBounds(10, 350, 400, 40);
jButton1.setBackground(new Color(216, 191, 216));
jButton1.setFont(new Font("Arial Black", 1, 12));
jButton1.setText("Send");
jButton1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
jButton1.setBounds(410, 350, 80, 40);
jlStatus.setForeground(new Color(255, 255, 255));
jlStatus.setText("...");
jPanel1.add(jlStatus);
jlStatus.setBounds(10, 60, 300, 40);
jLabel2.setFont(new Font("Myriad Pro", 1, 48));
jLabel2.setForeground(new Color(51, 0, 51));
jLabel2.setText("Server");
jLabel2.setToolTipText("");
jLabel2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
jPanel1.add(jLabel2);
jLabel2.setBounds(80, 10, 190, 60);
jLabel1.setBackground(new Color(153, 255, 204));
jPanel1.add(jLabel1);
jLabel1.setBounds(0, 35, 460, 410);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 500, GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 476, GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
setSize(new Dimension(500, 427));
setLocationRelativeTo(null);
}
private void jButton1ActionPerformed(ActionEvent evt) {
sendMessage(jTextField1.getText());
jTextField1.setText("");
}
private void jTextField1ActionPerformed(ActionEvent evt) {
sendMessage(jTextField1.getText());
jTextField1.setText("");
}
private void sendMessage(String message)
{
try
{
chatArea.append("\nME : "+ message);
out.writeObject("(server) : " + message);
out.flush();
}
catch(IOException ioException)
{
chatArea.append("\n Unable to Send Message");
}
}
private JTextArea chatArea;
private JButton jButton1;
private JLabel jLabel1;
private JLabel jLabel2;
private JPanel jPanel1;
private JScrollPane jScrollPane1;
private JTextField jTextField1;
private JLabel jlStatus;
}