-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoChat.java
More file actions
143 lines (123 loc) · 4.76 KB
/
Copy pathTwoChat.java
File metadata and controls
143 lines (123 loc) · 4.76 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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.EtchedBorder;
public class TwoChat extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
ServerSocket ss;
Socket s;
JTextField text = new JTextField();
JButton send = new JButton();
JTextArea messages = new JTextArea();
PrintWriter pout;
BufferedReader br;
ActionListener al;
String ipstring;
boolean ready2send = false;
TwoChat pt;
boolean HorC; // host or client
String cliOrServ;
public TwoChat(boolean hostOrConnect, String ip) {
ipstring = ip;
// Frame setup
setLayout(null);
setSize(600, 550);
setTitle("Client-Server Chat");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(text);
text.setLocation(5, 5);
text.setSize(getWidth() - 40 - 60, 30);
text.setEnabled(false);
add(send);
send.setLocation(10 + text.getWidth(), text.getY());
send.setText("Send");
send.setSize(70, 30);
add(messages);
messages.setEditable(false);
messages.setBorder(new EtchedBorder());
messages.setLocation(5, text.getHeight() + text.getY() + 5);
messages.setSize(getWidth() - 70, getHeight() - text.getY() - text.getHeight() - 100);
// Send button action
al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
ready2send = true;
}
};
send.addActionListener(al);
pt = this;
HorC = hostOrConnect;
if (HorC)
cliOrServ = "\nClient: ";
else
cliOrServ = "\nServer: ";
Messenger.start();
}
public static void main(String args[]) {
int inp = JOptionPane.showConfirmDialog(null,
"Do you want to host the chat?\nYes - Act as server\nNo - Act as client",
"Want to host a chat?",
JOptionPane.YES_NO_OPTION);
if (inp == 0) {
new TwoChat(true, null).setVisible(true);
} else {
String ipstring = JOptionPane.showInputDialog("Please Enter the IP Address :) ");
try {
InetAddress.getByName(ipstring);
new TwoChat(false, ipstring).setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid or Unreachable IP");
}
}
}
Thread Messenger = new Thread() {
public void run() {
try {
if (HorC) {
messages.setText("Waiting for an incoming connection.\nEnter my IP at client side.\nMy IP: "
+ InetAddress.getLocalHost().getHostAddress());
ss = new ServerSocket(9999);
s = ss.accept();
s.setKeepAlive(true);
} else {
messages.setText("Connecting to: " + ipstring + ":9999");
s = new Socket(InetAddress.getByName(ipstring), 9999);
}
text.setEnabled(true);
pout = new PrintWriter(s.getOutputStream(), true);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
messages.setText(messages.getText() + "\nConnected to: " + s.getInetAddress().getHostAddress() + ":"
+ s.getPort());
while (true) {
if (ready2send == true) {
pout.println(text.getText());
messages.setText(messages.getText() + "\nMe: " + text.getText());
text.setText("");
ready2send = false;
}
if (br.ready()) {
messages.setText(messages.getText() + cliOrServ + br.readLine());
}
Thread.sleep(80);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(pt, ex.getMessage());
messages.setText("Cannot connect!");
try {
Thread.sleep(3000);
} catch (InterruptedException ex1) {
}
System.exit(0);
}
}
};
}