-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReceiver.java
More file actions
92 lines (81 loc) · 2.9 KB
/
Receiver.java
File metadata and controls
92 lines (81 loc) · 2.9 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
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
class Gui_Receiver implements ActionListener {
JLabel label;
JButton button1, button2, button3;
JTextArea textArea;
JTextField textField;
Socket socket;
ServerSocket serverSocket;
Gui_Receiver() {
JFrame f = new JFrame("Receiver :)");
button1 = new JButton("Send >");
button2 = new JButton("Clear Text Field");
button3 = new JButton("Clear Chat Box");
label = new JLabel("Enter the message");
textField = new JTextField(50);
textArea = new JTextArea(10, 15);
f.setVisible(true);
f.setLayout(new FlowLayout());
f.setSize(600, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(label);
f.add(button1);
f.add(button2);
f.add(button3);
f.add(textArea);
f.add(textField);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
try {
// java.util.Date time = new java.util.Date(e.getWhen());
String s1 = textField.getText();
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF(s1);
textField.setText("");
textArea.append(/** time + "\n" + */
"You said: " + s1 + "\n");
} catch (Exception aae) {
}
} else if (e.getSource() == button2) {
textField.setText("");
} else if (e.getSource() == button3) {
textArea.setText("");
}
}
public void PutText(String s) {
textArea.append("Sender Says: " + s + "\n");
}
}
public class Receiver {
public static void main(String[] args) {
Gui_Receiver g = new Gui_Receiver();
try {
g.serverSocket = new ServerSocket(6666);
System.out.println("Waiting for the Client .....");
g.socket = g.serverSocket.accept();
System.out.println("Connected to the Client Successfully :)");
DataInputStream inputStream = new DataInputStream(g.socket.getInputStream());
while (true) {
String string = (String) inputStream.readUTF();
System.out.println(string);
g.PutText(string);
}
} catch (Exception ae) {
}
}
}