聊天程序服务器端
Server5.java
import java.net.*;
import java.io.*;
import java.util.*;
public class Server5 {
private ServerSocket server;
private BManager bMan=new BManager();
public Server5() {}
void startServer(){
try{
server=new ServerSocket(7777);
System.out.println("服务器套节字创建完毕");
while(true){
Socket socket=server.accept();
new Chat_Thread(socket).start();
bMan.add(socket);
bMan.sendClientInfo();
}
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] args) {
Server5 server=new Server5();
server.startServer();
}
class Chat_Thread extends Thread{
Socket socket;
private BufferedReader reader;
private PrintWriter writer;
Chat_Thread(Socket socket){
this.socket=socket;
}
public void run(){
try{
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(),true);
String msg;
while((msg=reader.readLine())!=null){
System.out.println(msg);
bMan.sendToAll(msg);
}
}catch(Exception e){
}finally{
try{
bMan.remove(socket);
if(reader!=null) reader.close();
if(writer!=null) writer.close();
if(socket!=null) socket.close();
reader=null; writer=null; socket=null;
System.out.println("客户机离开");
bMan.sendClientInfo();
}catch(Exception e){}
}
}
}
class BManager extends Vector{
BManager(){}
void add(Socket socket){
super.add(socket);
}
void remove(Socket socket){
super.remove(socket);
}
synchronized void sendToAll(String msg){
PrintWriter writer=null;
Socket socket;
for(int i=0;i<size();i++){
socket=(Socket)elementAt(i);
try{
writer=new PrintWriter(socket.getOutputStream(),true);
}catch(IOException e){}
if(writer!=null) writer.println(msg);
}
}
synchronized void sendClientInfo(){
String info="当前聊天人数"+size();
System.out.println(info);
sendToAll(info);
}
}
} 聊天程序客户端:
Client5.java
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Client5 extends Frame implements Runnable{
private TextField nameBox=new TextField("<名字>");//显示客户端名字,可以改.
private TextField sendBox=new TextField();
private TextArea msgView=new TextArea();
private BufferedReader reader;
private PrintWriter writer;
private Socket socket;
public Client5(String title) {
super(title);
add(nameBox,"North");
add(msgView,"Center");
add(sendBox,"South");
sendBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
writer.println(nameBox.getText()+":"+sendBox.getText());
sendBox.setText("");
}catch(Exception ie){}
}
});
pack();
}
public void run(){
while(true){
try{
msgView.append(reader.readLine()+"\n");
}catch(IOException ie){}
}
}
public void connect(){
try{
msgView.append("尝试与服务器连接\n");
socket=new Socket("192.168.1.22",7777);
msgView.append("聊天准备完毕\n");
reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer=new PrintWriter(socket.getOutputStream(),true);
new Thread(this).start();
}catch(Exception e){
System.out.println("连接失败");
}
}
public static void main(String[] args) {
Client5 client=new Client5("聊天");
client.setVisible(true);
client.connect();
}
}
注意:客户端有多个,可复制,粘贴客户端程序.但是要把名字和最上面的文本框的名字改了.