-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
121 lines (85 loc) · 2.77 KB
/
Client.java
File metadata and controls
121 lines (85 loc) · 2.77 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
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Client {
private String host = "localhost";
private int port = 3000;
JTextArea showTextArea;
JTextField sendTextArea;
BufferedReader reader;
PrintWriter writer;
Socket sock;
String userName;
public static void main(String[] args) {
Client client = new Client();
client.go();
}
public void go() {
Scanner getName = new Scanner(System.in);
System.out.println("Please enter user name: ");
while(userName == null || userName.trim().equals("")) {
userName = getName.nextLine();
if(userName.trim().equals("")){
System.out.println("Invalid name. Please try again: ");
}
} // close while loop
makeGUI(userName);
}
public void makeGUI(String frameName) {
JFrame frame = new JFrame(frameName);
JPanel mainPanel = new JPanel();
showTextArea = new JTextArea(20,30);
showTextArea.setLineWrap(true);
showTextArea.setWrapStyleWord(true);
showTextArea.setEditable(false);
JScrollPane scroller = new JScrollPane(showTextArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
sendTextArea = new JTextField(20);
JButton sendButton = new JButton("send");
sendButton.addActionListener(new SendButtonListener());
mainPanel.add(scroller);
mainPanel.add(sendTextArea);
mainPanel.add(sendButton);
setUpNetWorking();
Thread readThread = new Thread(new IncomingMessage());
readThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(400,500);
frame.setVisible(true);
} // close makeGUI method
private void setUpNetWorking(){
try {
sock = new Socket(host,port);
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 method
public class SendButtonListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
try {
writer.println(userName + " > " + sendTextArea.getText());
writer.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
sendTextArea.setText("");
sendTextArea.requestFocus();
}
} // close inner class
public class IncomingMessage implements Runnable {
public void run() {
String message;
try {
while((message = reader.readLine()) != null) {
showTextArea.append(message + "\n");
}
} catch (Exception ex) {ex.printStackTrace();}
}
} // close inner Class
} // close outer class