Skip to content

Commit 09dfbde

Browse files
authored
Done with all methods
1 parent 24c24fc commit 09dfbde

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

Server.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
import java.io.*;
3+
import java.net.*;
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
import javax.swing.*;
7+
8+
class Server extends JFrame {// server is a standalone computer which anyone can access
9+
public JTextArea chatWindow;
10+
public JTextField userText;
11+
private ObjectOutputStream output;// outputstream is the data that flows from your computer to your friends
12+
// computer
13+
private ObjectInputStream input;
14+
private ServerSocket server;
15+
private Socket connection;// in java they actually name connection socket
16+
// setup the socket means setting up the connection
17+
18+
public Server() {
19+
super("Jinit's Messaging App ");
20+
userText = new JTextField();
21+
userText.setEditable(false);// before u r connected to anyone u r not allowed to talk to anyone
22+
userText.addActionListener(new ActionListener() {
23+
24+
@Override
25+
public void actionPerformed(ActionEvent event) {
26+
sendMessage(event.getActionCommand());// returns command string associated with it i.e textfield
27+
userText.setText("");
28+
29+
}
30+
});
31+
add(userText, BorderLayout.NORTH);
32+
chatWindow = new JTextArea();
33+
add(new JScrollPane(chatWindow));
34+
setSize(300, 150);
35+
setVisible(true);
36+
}
37+
38+
// setting up the server
39+
public void startRunning() {
40+
try {
41+
server = new ServerSocket(6789, 100);// server is a computer which can be accessed by a lot of people on
42+
// which there r many applications and u connect to a single appn by
43+
// port number 2nd parameter is the num of people who can access it
44+
while (true) {
45+
try {
46+
// connect and have conversation with some1 else
47+
waitForConnection();
48+
setupStreams();
49+
whileChatting();
50+
} catch (EOFException eofException) {// end of stream
51+
showMessage("\n Server ended the connection ");
52+
} finally {
53+
closeCrap();
54+
}
55+
}
56+
} catch (IOException ioException) {
57+
ioException.printStackTrace();
58+
}
59+
}
60+
61+
public void waitForConnection() throws IOException {
62+
showMessage("Waiting for sm1 to connect.... ");
63+
connection = server.accept();// connection is the socket and server.accept() accepts sm1 request to connect
64+
// to u and sends it to socket
65+
showMessage("now connected to " + connection.getInetAddress().getHostName());
66+
}
67+
68+
public void setupStreams() throws IOException {
69+
output = new ObjectOutputStream(connection.getOutputStream());// creating a conncection to whatever computer we
70+
// want to connect to
71+
output.flush();// smtimes data gets stop this sends it
72+
input = new ObjectInputStream(connection.getInputStream());
73+
showMessage("\n Streams are setup \n");
74+
75+
}
76+
77+
//during the conversation
78+
public void whileChatting() throws IOException {
79+
String message = "Wou are now connected ";
80+
sendMessage(message);
81+
ableToType(true);
82+
do {
83+
// have conversation
84+
try {
85+
message = (String) input.readObject();// input is socket from which they will send ...it views it as
86+
// object and store it in string
87+
showMessage("\n " + message);
88+
} catch (ClassNotFoundException classNotFoundException) {
89+
showMessage("\n idk what it sends ");
90+
}
91+
} while (!message.equals("CLIENT - END"));
92+
93+
}
94+
95+
// closing the streamss and sockets
96+
public void closeCrap() {
97+
showMessage("\n Closing connections....\n ");
98+
ableToType(false);
99+
try {
100+
output.close();
101+
input.close();
102+
connection.close();
103+
} catch (IOException ioexception) {
104+
ioexception.printStackTrace();
105+
}
106+
}
107+
108+
// send a message to client
109+
public void sendMessage(String message) {
110+
try {
111+
output.writeObject("SERVER- " + message);// sends message to output stream
112+
output.flush();
113+
showMessage("\nSERVER -" + message);
114+
} catch (IOException ioException) {
115+
chatWindow.append("\n ERROR ,I cant send it ");
116+
}
117+
}
118+
119+
// update chat window
120+
public void showMessage(final String text) {
121+
SwingUtilities.invokeLater(new Runnable() {
122+
123+
@Override
124+
public void run() {
125+
126+
chatWindow.append(text);
127+
}
128+
});
129+
}
130+
131+
public void ableToType(final boolean tof) {
132+
// let the user type
133+
SwingUtilities.invokeLater(new Runnable() {
134+
135+
@Override
136+
public void run() {
137+
138+
userText.setEditable(tof);
139+
}
140+
});
141+
142+
}
143+
}
144+

0 commit comments

Comments
 (0)