import java.io.DataInputStream;
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.util.ArrayList;
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(7777);
started = true;
}
catch(BindException e2) {
System.out.println("端口使用中 ... ...");
System.out.println("请关闭双关程序!");
System.exit(0);
}
catch(Exception e) {
e.printStackTrace();
}
try{
while(started) {
Socket socket = serverSocket.accept();
Client client = new Client(socket);
System.out.println("连接上了... ...");
new Thread(client).start();
clients.add(client);
}
} catch (IOException e) {
System.out.println("Server 关闭");
}finally {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket socket;
private DataInputStream dis = null;
private DataOutputStream dos = null;
boolean bConnected = false;
public Client(Socket socket) {
this.socket = socket;
try {
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.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 c = clients.get(i);
c.send(str);
}
}
}catch (EOFException e) {
System.out.println("Server 关闭");
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
if(dis != null){
dis.close();
}
if(dos != null){
dos.close();
}
if(socket != null){
socket.close();
socket = null;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
客户端 :
import java.awt.*;
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.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatClient extends Frame {
Socket socket = null;
TextField textField = new TextField();
TextArea textArea = new TextArea();
DataOutputStream dos = null;
DataInputStream dis = null;
boolean bContented = false;
public void MainFrame() {
setLocation(200, 200);
setSize(400, 300);
add(textArea,BorderLayout.NORTH);
add(textField,BorderLayout.SOUTH);
textField.addActionListener(new TextFieldListener());
pack();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
disConnect();
System.exit(0);
}
});
setVisible(true);
connect();
new Thread(new ClientTalk()).start();
}
private class TextFieldListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str = textField.getText();
//textArea.setText(str);
textField.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void disConnect() {
try {
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void connect() {
try {
socket = new Socket("127.0.0.1",7777);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
bContented = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
class ClientTalk implements Runnable {
public void run() {
try{
while(bContented) {
String str = dis.readUTF();
//System.out.println(str);
textArea.setText(textArea.getText() + str + '\n');
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new ChatClient().MainFrame();
}
}