accept()之后,你应该启动一个线程,让其去继续运行,然后继续accept,等待下一个连接。你可以参考这个多线程的猜数游戏程序。 http://www.java2000.net/p686# while (true) { // 等待连接 socket = serverSocket.accept(); ServerThread st = new ServerThread(socket); new Thread(st).start(); } 这个就是核心
java code import java.awt.*; import java.awt.event.*; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.*;public class ChatClient extends Frame { Socket s = null; DataOutputStream dos = null; DataInputStream dis=null; private boolean bConnected=false; Thread tRecv=new Thread(new RecvThread()); TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); public static void main(String[] args) { new ChatClient().launchFrame(); } public void launchFrame() { setLocation(800, 500); this.setSize(400, 300); add(tfTxt, BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack();
// 等待连接
socket = serverSocket.accept();
ServerThread st = new ServerThread(socket);
new Thread(st).start();
} 这个就是核心
import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;public class ChatClient extends Frame {
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis=null;
private boolean bConnected=false;
Thread tRecv=new Thread(new RecvThread());
TextField tfTxt = new TextField();
TextArea taContent = new TextArea(); public static void main(String[] args) {
new ChatClient().launchFrame();
} public void launchFrame() {
setLocation(800, 500);
this.setSize(400, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
tfTxt.addActionListener(new KeyEnterListener());
setVisible(true);
connect();
tRecv.run();
} public void connect() {
try {
Socket s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
bConnected=true;
System.out.println("connect");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public void disconnect() {
try {
bConnected=false;
tRecv.join();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
try{
dos.close();
dis.close();
s.close();
}catch (IOException e) {
e.printStackTrace();
}
}
} private class KeyEnterListener implements ActionListener { public void actionPerformed(ActionEvent arg0) {
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try {
dos.writeUTF(str);
dos.flush(); } catch (IOException e) {
e.printStackTrace();
} } } private class RecvThread implements Runnable{
public void run(){
try {
while(bConnected){
String str;
str = dis.readUTF();
taContent.setText(taContent.getText()+"\n"+str);
// System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.*;
import java.util.*;
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients=new ArrayList<Client>();
public static void main(String[] args) { new ChatServer().start();
} public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while (started) {
Socket s = ss.accept();
Client client = new Client(s);
System.out.println("a client connected");
new Thread(client).start();
clients.add(client);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos=null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
} public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (bConnected) {
String str = dis.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++){
Client client=clients.get(i);
client.send(str);
}
}
}catch(SocketException e){
System.out.println("退出");
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if(dos!=null)
dos.close();
if (s != null)
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} }
}
}