forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleChatClient.java
More file actions
75 lines (75 loc) · 2.31 KB
/
Copy pathSimpleChatClient.java
File metadata and controls
75 lines (75 loc) · 2.31 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
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleChatClient{
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public void go(){
JFrame frame = new JFrame("Simple Chat Client");
JPanel mainPanel = new JPanel();
incoming=new JTextArea(15,50);
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());
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,500);
frame.setVisible(true);
}
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();
}
}
public class SendButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
writer.println(outgoing.getText());
writer.flush();
}
catch(Exception ex){
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
}
public static void main(String[] args) {
new SimpleChatClient().go();
}
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");
}
}
catch(Exception ex){ex.printStackTrace();}
}
}
}