放上源程序代碼
服務端:
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)如何在服務方對退出的用戶進行處理,保證聊天發送的消息只發給在場的用戶。
Java網絡編程以及簡單的聊天程序
JAVA網絡編程——簡單的聊天程序
JAVA 網絡編程 UDP(聊天程序)
----------------------同志你好,我是CSDN問答機器人小N,奉組織之命為你提供參考答案,編程尚未成功,同志仍需努力!