-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHost-Server~
More file actions
147 lines (132 loc) · 4.52 KB
/
Host-Server~
File metadata and controls
147 lines (132 loc) · 4.52 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
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 JButton send;
private ObjectOutputStream output; //stream that flows from my computer to friends computer
private ObjectInputStream input;
private ServerSocket server;
private Socket connection; //socket= connection between two computers
//constructor
public Server(){
super("Grrreat Instant Messenger");
userText = new JTextField();
send = new JButton("Send");
send.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
sendMessage(userText.getText());
}
}
);
userText.setEditable(false); //doesn't allow user to click in
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
sendMessage(e.getActionCommand());
userText.setText(""); //after send text, text area clears
}
}
);
add(userText, BorderLayout.NORTH);
add(send, BorderLayout.SOUTH);
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
setSize(500, 250);
setVisible(true);
}
//set up and run the server
public void startRunning(){
try{
server = new ServerSocket(6789, 100); //6789 is the port number(where do you want to connect), 100 is backlog(# people can wait to talk to you)
while(true){
try{
//connect/have convo w/ someone else
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){ //end of connection or end of stream
showMessage("\n Server ended the connection.");
}finally{
closeUneeded();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException{
showMessage("Waiting for someone to connect. \n");
connection = server.accept(); //blocks a connection until it's made
showMessage("Connected to " + connection.getInetAddress().getHostName()); //returns your address once connected & host ip adress
}
//get stream to send and receive data/messages
private void setupStreams()throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush(); //send leftover data/bytes to the other person
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now connected \n");
}
//during the chat conversation
private void whileChatting() throws IOException{
String message = "You are now connected.";
sendMessage(message);
ableToType(true); //will allow user to type in text box
do{
//have a conversation
try{
message = (String) input.readObject(); //reads whatever user sends and stores it in message var
showMessage("\n " + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n Error, user did not send");
}
}while(!message.equals("Client - END")); //if user types END program will stop
}
//close streams and sockets after done chatting
private void closeUneeded() throws IOException{
showMessage("\n Closing connections \n");
ableToType(false);
try{
output.close();
input.close();
connection.close(); //if these aren't closed, it will waste memory
}catch(IOException ioException){
ioException.printStackTrace(); //printStackTrace shows the error message
}
}
//send a message to client(comp connected to server)
private void sendMessage(String message){
try{
output.writeObject("Server - " + message); //writeObject is a java method that sends message to output stream
output.flush(); //in case any bytes are left over, flush them out
showMessage("\nServer- " + message); //will show previous messages in Text Field
}catch(IOException ioException){
chatWindow.append("\n ERRROR: CAN'T SEND MESSAGE"); //append adds to chatWindow
}
}
//updates chatWindow the only part of the GUI that is updated
private void showMessage(final String text){ //everything that shows up in chatWindow will be a stream
SwingUtilities.invokeLater( //updates text in chat window
new Runnable(){ //creates a thread to update the GUI
public void run(){
chatWindow.append(text);
}
}
);
}
//let user type into their box
private void ableToType(final boolean trueOrFalse){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(trueOrFalse);
}
}
);
}
}