-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
171 lines (133 loc) · 3.59 KB
/
Server.java
File metadata and controls
171 lines (133 loc) · 3.59 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
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
//in java connections are referred as Sockets
//constructor
public Server(){
super("Y@$# Messenger");
userText=new JTextField();
//Before connection setup u can not send message, threfore fistly the textfield is noneditible
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
//getActionCommand() method retrieves the entered text in the field
sendMessage(e.getActionCommand());
userText.setText("");
}
}
);
add(userText,BorderLayout.NORTH);
chatWindow=new JTextArea();
add(new JScrollPane(chatWindow));
setSize(400,400);
setVisible(true);
}
//set up and run the server
public void startRunning(){
/**For creating the server
* setup first the port no. here it is 1234
* and the max no. of people can wait to talk is 100 at once
*/
try{
server = new ServerSocket(1234,100);
while(true){
try{
//connect and have conversation
waitForConnection();
setupStreams();
whileChatting();
}
catch(EOFException e2){
showMessage("\n Server ended the connection!");
}
finally{
close();
}
}
}catch(IOException e){
//to print the error
e.printStackTrace();
}
}
//wait for connection, then display the connection information
private void waitForConnection()throws IOException{
showMessage("Waiting for someone to connect.....!\n");
//set up the socket: it is the connection b/w u and other computer
connection =server.accept();
//hostname gets the IP address
showMessage("Now connected to "+connection.getInetAddress().getHostName());
}
//get stream to sent and recieve data
private void setupStreams()throws IOException{
output=new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup! \n");
}
//while chatting with server
private void whileChatting()throws IOException{
String message="";
ableToType(true);// for able to type
do{
try{
message=(String) input.readObject();
showMessage("\n"+message);
}catch(ClassNotFoundException e){
showMessage("\n I Dont know the type of Object");
}
}while(!message.equals("SERVER: END"));
}
//close streams and sockets after chatting
private void close(){
showMessage("\nClosing Connections..\n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException e){
e.printStackTrace();
}
}
//send messages to client
private void sendMessage(String message){
try{
//sends a message through output stream to the client
output.writeObject("SERVER: "+message);
output.flush();
showMessage("\n SERVER: "+message);
}catch(IOException e){
chatWindow.append("\n ERROR: I CANNOT SEND MESSAGE!");
}
}
//updates chatWindow
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(text);
}
}
);
}
//let the user type
private void ableToType(final boolean t){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(t);
}
}
);
}
}