-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleChatClient.java
103 lines (83 loc) · 3.29 KB
/
SimpleChatClient.java
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
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleChatClient{
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
String name;
public static void main(String[] args){
SimpleChatClient client = new SimpleChatClient();
client.go();
}
public void go(){
JFrame frame = new JFrame("Simple Chat Client");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15,40);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
JButton sendButton = new JButton("Send!");
sendButton.addActionListener(new SendButtonListener());
JButton nameButton = new JButton("Set your name!");
nameButton.addActionListener(new NameButtonListener());
mainPanel.add(nameButton);
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();
Thread readerThread = new Thread(new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(600, 300);
frame.setVisible(true);
} // close go
private void setUpNetworking(){
try{
sock = new Socket("127.0.0.1", 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("networking established");
} catch(IOException ex){
ex.printStackTrace();
}
} // close setUpNetworking
public class SendButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
writer.println(name + " said: " +outgoing.getText());
writer.flush();
} catch(Exception ex){
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
} //close inner class
public class NameButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ev){
name = JOptionPane.showInputDialog("What is your name?", null);
}
} //close inner class
public class IncomingReader implements Runnable{
public void run(){
String message;
try{
while((message = reader.readLine()) != null){
System.out.println("read " + message);
incoming.append(message + "\n");
} //close while
} catch (Exception ex) { ex.printStackTrace();}
} //close run
} // close inner class
}