-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientChat.java
More file actions
175 lines (150 loc) · 5.16 KB
/
ClientChat.java
File metadata and controls
175 lines (150 loc) · 5.16 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
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
/**
* ClientChat.java
*
* Copyright 2015 David Camacho
*
* This program makes the client that connects to the server so that it can chat with other clients
* It uses a GUI to send information to the client and from the client
*
*/
public class ClientChat extends JFrame implements ActionListener{
private JTextArea jtaSendText;
private JTextArea jtaRecvText;
private JButton jbSend;
private JButton jbExit;
private JButton jbConnect;
private Socket socket = null;
private PrintWriter pw = null;
private BufferedReader br = null;
ClientThread clientThread = null;
public static void main (String args[]) {
ClientChat cc = new ClientChat(); // starts the ClientChat constructor
}
/**
* ClientChat constructor makes the GUI to interact with
*/
public ClientChat(){
super("Chat Client");
JPanel topP = new JPanel();
JPanel middleP = new JPanel();
JPanel bottomP = new JPanel();
topP.setLayout(new FlowLayout());
middleP.setLayout(new GridLayout(0,2));
bottomP.setLayout(new FlowLayout());
JLabel title = new JLabel("Chatroom Client");
Font f = new Font("Sans", Font.BOLD, 20);
title.setFont(f);
topP.add(title);
Border border = BorderFactory.createLineBorder(Color.BLACK);
jtaRecvText = new JTextArea(30,10);
jtaSendText = new JTextArea(30,10);
jtaRecvText.setBorder(border);
jtaSendText.setBorder(border);
jtaSendText.setLineWrap(true);
jtaSendText.setWrapStyleWord(true);
jtaRecvText.setLineWrap(true);
jtaRecvText.setWrapStyleWord(true);
jtaRecvText.setEditable(false);
middleP.add(jtaRecvText);
middleP.add(jtaSendText);
jbSend = new JButton("Send");
jbExit = new JButton("Exit");
jbConnect = new JButton("Connect");
jbConnect.addActionListener(this);
jbSend.addActionListener(this);
jbExit.addActionListener(this);
bottomP.add(jbConnect);
bottomP.add(jbSend);
bottomP.add(jbExit);
add(topP, BorderLayout.NORTH);
add(middleP, BorderLayout.CENTER);
add(bottomP, BorderLayout.SOUTH);
setSize(550,400);
setLocation(200,300);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
/**
* Once a client clicks the connect button it goes here where we establish the connection
* and then starts the ClientThread class where we use threads to read from the server.
*
*/
if(ae.getActionCommand().equals("Connect")){
try{
socket = new Socket("localhost", 16457);
jtaRecvText.append("Connected: " + socket);
jtaRecvText.append("\n");
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
clientThread = new ClientThread();
clientThread.start();
}
catch(UnknownHostException uhe){
jtaRecvText.append("Host unknown: " + uhe.getMessage());
}
catch(IOException ioe){
jtaRecvText.append("Unexpected exception: " + ioe.getMessage());
}
}
/**
* When the user writes something to the JTextArea and then clicks send it
* sends the string to the server and then resets the text.
*
*/
if(ae.getActionCommand().equals("Send")){
try{
String sendMsg;
sendMsg = jtaSendText.getText();
pw.println(sendMsg);
pw.flush();
jtaSendText.setText("");
}
catch(Exception e){
jtaRecvText.append("Something messed up");
}
}
/**
* When the user clicks this button it closes the connection and exits the GUi
*/
if(ae.getActionCommand().equals("Exit")){
try{
socket.close();
System.exit(0);
}
catch(Exception e){
jtaRecvText.append("Could not exit");
}
}
}
class ClientThread extends Thread{
String getMsg;
public ClientThread(){
}
/**
* Once the thread is started when the user clicks connect it goes here to
* loop the readline so we can get the information from the server and put it in the
* the JTextArea.
*
*/
public void run(){
while(true){
try{
getMsg = br.readLine();
jtaRecvText.append(getMsg + "\n");
}
catch(Exception e){
jtaRecvText.append("Something went wrong.");
}
}
}
}
}