Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
225 lines (166 loc) · 5.02 KB
/
Copy pathServer.java
File metadata and controls
225 lines (166 loc) · 5.02 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
/**
* @author Hamid(the6ixdev)
* This Program can sit on a public server
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
// This is gonna be the area that u type ur message
private JTextField userText;
// now the area that shows the conversation
private JTextArea chatWindow;
// we need to build a Stream
/*
* Stream as a method to send data between our 2 computers
* two types ->input stream & output Stream
* we send it packed like as a boat through output stream
* and vice versa
*/
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
// In java connections are called Socket, setup the socket
// means setup the connection
private Socket connection;
/*
* Setup the GUI
* constructor
* */
public Server() {
super("Server Side Instant Messenger!");
userText = new JTextField();
// by default before u connect don't edit this
userText.setEditable(false);
// now let's add actionListener to it
userText.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText,BorderLayout.NORTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
setSize(700,400);
setVisible(true);
}
// setup the application and run the server
public void startRunning() throws IOException {
try{
// time to set it
// when our package comes to stream,it wants a port number
// second variable is, how many are allowed to talk to me at a time?
// 100 is called a backlog or queue length
server = new ServerSocket(6789,100);
while(true) {
try {
// connect and have conversation
waitForConnection();
setupStreams();
whileChatting();
} catch (EOFException eofException) {
// when conversation ,end of stream
showMessage("\nServer ended the connection! ");
}finally{
closeStuff();
}
}
}catch(IOException ioException) {
// print so we know where we went wrong
ioException.printStackTrace();
}
}
// wait for connection, then display connection information
private void waitForConnection() throws IOException {
showMessage("Waiting for someone to connect... \n");
// now we need to setup the connection between u and the other computer(aka socket)
// the loop is called over and over and accepts connections
connection = server.accept();
//socket is created
showMessage("Now connected to "+connection.getInetAddress().getHostName());
}
// setting up the streams
private void setupStreams() throws IOException {
// setting up the pathway to send stuff
output = new ObjectOutputStream(connection.getOutputStream());
// when data is stuck
// just in case you will flush out of streams
output.flush();
//setting up the pathway to receive stuff
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n the streams are not setup! \n");
}
// during the chat conversation
private void whileChatting() throws IOException {
String message = "You are now connected!";
sendMessage(message);
ableToType(true);
//during the conversation we r gonna put everything in a do-while loop
do {
// have a conversation while both of us want to
// if user says "end" its done
try {
// we wanna make stream a string type.
message = (String)input.readObject();
showMessage("\n" + message);
} catch (ClassNotFoundException classNotFoundException) {
showMessage("\n I don't know what user sent! ");
}
} while (!message.equals("MOZHGAN: END"));
}
// close streams and sockets after done chatting
private void closeStuff() {
showMessage("\n Closing connections.... \n");
ableToType(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException ioException) {
// TODO: handle exception
ioException.printStackTrace();
}
}
// send a message to client
private void sendMessage(String message) {
try {
// send message from output stream
output.writeObject("Hamid: "+message);
output.flush();
showMessage("\nHamid: "+message);
} catch(IOException ioException) {
// TODO: handle exception
chatWindow.append("\n ERROR: MESSAGE COULD NOT BE SENT! ");
}
}
// updates chat window
private void showMessage(final String text) {
// the only thing we wanna update is part of the GUI
// so we have to use one of the Javax swing stuff
// so it sets a thread that updates parts of the GUI
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
chatWindow.append(text);
}
}
);
}
// let the user type into their box
private void ableToType(final boolean tof) {
SwingUtilities.invokeLater(
new Runnable() {
@Override
public void run() {
userText.setEditable(tof);
}
}
);
}
}