服务端接收多个客户端的连接请求,同时也可以发送数据到客户端。使用socket发送数据,awt绘制图形界面。
package chat;
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class ChatServer extends Frame {
TextArea ta = new TextArea();
ServerSocket server = null;
List clients = new ArrayList<>();
ChatServer(int port) throws Exception {
server = new ServerSocket(port);
launchFrame();
}
void startServer() throws Exception {
while(true) {
Socket s = server.accept();
clients.add(new ClientConn(s));
ta.append("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort());
ta.append("\n" + "CLIENTS-COUNT: " + cClient.size() + "\n\n");
}
}
void launchFrame() {
add(ta, BorderLayout.CENTER);
setBounds(0, 0, 200, 300);
this.addWindowListener({
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
});
setVisible(true);
}
class ClientConn implements Runnable {
Socket s = null;
ClientConn(Socket s) {
this.s = s;
(new Thread(this)).start();
}
void send(String str) throws IOException {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
void dispose() {
try {
if (s != null) s.close();
clients.remove(this);
ta.append("A client out! \n");
ta.append("CLIENT-COUNT: " + cClient.size() + "\n\n");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void run() {
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while (str != null && str.length() !=0) {
System.out.println(str);
for (ClientConn client:clients) {
if (this != client) {
client.send(str);
}
}
str = dis.readUTF();
}
this.dispose();
} catch (Exception e) {
System.out.println("Client Quit.");
this.dispose();
}
}
}
public static void main(String[] args) throws Exception {
ChatServer cs = new ChatServer(8888);
cs.startServer();
}
}
package chat;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class ChatClient extends Frame {
TextArea ta = new TextArea();
TextField tf = new TextField();
Socket s = null;
void launchFrame() {
this.add(ta, BorderLayout.CENTER);
this.add(tf, BorderLayout.SOUTH);
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String message = tf.getText();
if (message.trim().length() == 0) return;
this.send(message);
tf.setText("");
ta.append(message + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
setBounds(300, 300, 300, 400);
setVisible(true);
tf.requestFocus();
}
ChatClient() throws Exception {
s = new Socket("127.0.0.1", 8888);
launchFrame();
(new Thread(new ReceiverThread())).start();
}
void send(String str) throws Exception {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}
void disconnect() throws Exception {
s.close();
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
ChatClient cc = new ChatClient();
String str = br.readLine();
while(str != null && str.length() != 0)
{
cc.send(str);
str = br.readLine();
}
cc.disconnect();
}
class ReceiveThread implements Runnable {
public void run() {
if(s == null) return;
try {
DataInputStream dis = new DataInputStream(s.getInputStream());
String str = dis.readUTF();
while (str != null && str.length() != 0)
{
ChatClient.this.ta.append(str + "\n");
str = dis.readUTF();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}