import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ChatServer {
boolean started = false;
ServerSocket serverSocket = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
serverSocket = new ServerSocket(8888);
started = true;
} catch (BindException e) {
System.out.println("端口使用中......系统正忙!");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
started = true;
while (started) {
boolean bConnected = false;
Socket socket = serverSocket.accept();
Client client = new Client(socket);
System.out.println("a client connnected!");
new Thread(client).start();
clients.add(client);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket socket = null;
private DataInputStream dataInputStream = null;
private DataOutputStream dataOutputStream = null;
private boolean bConnected = false;
public Client(Socket socket) {
this.socket = socket;
try {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(
socket.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String str) throws IOException {
dataOutputStream.writeUTF(str);
}
public void run() {
Client client = null;
try {
while (bConnected) {
String str = dataInputStream.readUTF();
System.out.println(str);
for(int i=0; i<clients.size(); i++){
client = clients.get(i);
client.send(str);
}
}
}catch(SocketException e){
clients.remove(this);
System.out.println("a client quit!");
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dataInputStream != null)
dataInputStream.close();
if(dataOutputStream != null)
dataOutputStream.close();
if (socket != null){
socket.close();
socket = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
==================================
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
public class ChatClient extends Frame {
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
private boolean bConnected = false;
TextField textField = new TextField();
TextArea textArea = new TextArea();
Thread tRecv = new Thread(new RecvThread());
public static void main(String[] args) {
new ChatClient().lauchFrame();
}
public void lauchFrame() {
setLocation(400, 300);
setSize(300, 300);
add(textField, BorderLayout.SOUTH);
add(textArea, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
textField.addActionListener(new TextFieldListener());
setVisible(true);
connect();
tRecv.start();
}
public void connect(){
try {
socket = new Socket("127.0.0.1", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
System.out.println("connected!");
bConnected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect(){
try {
dataOutputStream.close();
dataInputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TextFieldListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s = textField.getText().trim();
textField.setText("");
try {
dataOutputStream.writeUTF(s);
dataOutputStream.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
private class RecvThread implements Runnable{
public void run() {
try {
while(bConnected){
String str = dataInputStream.readUTF();
textArea.setText(textArea.getText() + str + "\n");
}
} catch (SocketException e) {
System.out.println("退出来,bye!");
}catch(EOFException e){
System.out.println("退出了,bye - bye!");
}catch(IOException e){
e.printStackTrace();
}
}
}
}