放上源程序代码
服务端:
import java.io.*;
import java.net.*;
import java.util.ArrayList;
public class TalkServer {
public static ArrayList<Client> allClient = new ArrayList<Client>();
public static int clientNum = 0;
/**
* @param args
*/
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5432);
while (true) {
Socket socket = serverSocket.accept();
DataOutputStream dataOutputStream = new DataOutputStream(
socket.getOutputStream());
DataInputStream dataInputStream = new DataInputStream(
socket.getInputStream());
Client client = new Client(clientNum, dataOutputStream,
dataInputStream);
allClient.add(client);
client.start();
clientNum++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client extends Thread {
int id;
DataOutputStream dataOutputStream;
DataInputStream dataInputStream;
public Client(int id, DataOutputStream dataOutputStream,
DataInputStream dataInputStream) {
this.id = id;
this.dataOutputStream = dataOutputStream;
this.dataInputStream = dataInputStream;
}
public void run() {
while (true) {
try {
String message = dataInputStream.readUTF();
int m = TalkServer.clientNum;
for (int i = 0; i < m; i++) {
TalkServer.allClient.get(i).dataOutputStream
.writeUTF(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class TalkClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException,
IOException {
Socket socket = new Socket("localhost", 5432);
DataInputStream dataInputStream = new DataInputStream(
socket.getInputStream());
final DataOutputStream dataOutputStream = new DataOutputStream(
socket.getOutputStream());
Frame frame = new Frame("client");
Panel panel = new Panel();
final TextField input = new TextField(20);
TextArea displayArea = new TextArea(5, 20);
displayArea.setEditable(false);
panel.add(input);
panel.add(displayArea);
frame.add(panel);
final String name = JOptionPane.showInputDialog("输入您的昵称:");
new receiveThread(dataInputStream, displayArea);
input.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
dataOutputStream.writeUTF(name + ":"
+ input.getText());
} catch (IOException exception) {
exception.printStackTrace();
}
}
});
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class receiveThread extends Thread {
DataInputStream dataInputStream;
TextArea displayArea;
public receiveThread(DataInputStream dataInputStream, TextArea displayArea) {
this.dataInputStream = dataInputStream;
this.displayArea = displayArea;
this.start();
}
public void run() {
for (;;) {
try {
String str = new String(dataInputStream.readUTF());
displayArea.append(str + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TalkServer是服务器代码
TalkClinet是客户端代码
这是一个简易的聊天程序。
(1)如何修改服务方,使用户自己发送的消息不显示在自己的文本域中。
(2)在客户方显示用户列表,可以选择将信息发送给哪些用户。
(3)如何在服务方对退出的用户进行处理,保证聊天发送的消息只发给在场的用户。