import
java.io.EOFException;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.net.BindException;
import
java.net.ServerSocket;
import
java.net.Socket;
import
java.net.SocketException;
import
java.util.ArrayList;
public class Chat_Server {
ServerSocket server=null;
//ObjectInputStream input=null;
boolean isStart=false;//服务端是否启动
boolean isConnect=false;//客户端是否连接上
ArrayList<Client_Thread> clients=
new ArrayList<Client_Thread>();
public static void main(String[] args) {
new Chat_Server().startServer();
}
private void startServer(){
System.out.println("服务端已启动");
try{
server=new ServerSocket(8888);
isStart=true;
}catch(BindException e){
System.out.println("端口使用中....");
System.exit(0);
}catch(IOException e){
e.printStackTrace();
System.exit(0);
}
try {
while(isStart){
Socket client=server.accept();
//客户端连接成功后,为客户端创建一个独立的线程接受消息
Client_Thread client_thread=
new Client_Thread(client);
new Thread(client_thread).start();//启动线程
clients.add(client_thread);//添加
}
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
* 每当一个客户端连接到
服务器都会创建一个如此的线程
*/
class Client_Thread implements Runnable{
//类里边保留socket、inputStream
private Socket s=null;
private ObjectInputStream in=null;
private ObjectOutputStream output=null;
private boolean isConnect=false;
Client_Thread(Socket s){
this.s=s;
try {
in=new ObjectInputStream(s.getInputStream());
output=new ObjectOutputStream(s.getOutputStream());
isConnect=true;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Client_Thread");
e.printStackTrace();
}
}
public void send(String str){
try {
output.writeObject(str);
output.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
System.out.println("服务端监听已启动");
String str;
try {
while(isConnect){
str = (String)in.readObject();
System.out.println(str);
for(int i=0;i<clients.size();i++){
Client_Thread c=clients.get(i);
c.send(str);
}
}
} catch (SocketException e) {
System.out.println("客户端已关闭");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(in!=null) in.close();
if(output!=null) output.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}Chat_Client.
javaimport
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.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.net.Socket;
import
java.net.UnknownHostException;
public class Chat_Client extends Frame{
TextField textField=new TextField();
TextArea textArea=new TextArea();
String str=null;
Socket client;
ObjectOutputStream output=null;
ObjectInputStream input=null;
boolean isConnected=false;
public static void main(String[] args) {
new Chat_Client().launchFrame();
}
public Chat_Client(){
connect();
new Thread(new ClientOwnThread()).start();
}
public void launchFrame(){
this.setLocation(400,300);
this.setSize(300,300);
this.add(textArea,BorderLayout.NORTH);
this.add(textField,BorderLayout.SOUTH);
this.pack();
textArea.setEditable(false);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
textField.addActionListener(new TextFieldListener());
}
private void connect(){
try {
client=new Socket("127.0.0.1",8888);
output=new ObjectOutputStream(client.getOutputStream());
input=new ObjectInputStream(client.getInputStream());
isConnected=true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void disConnect(){
try {
if(output!=null) output.close();
if(input!=null) input.close();
if(client!=null) client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class ClientOwnThread implements Runnable{
public void run() {
try {
while(isConnected){
String str=(String)input.readObject();
textArea.append(str+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
disConnect();
}
}
}
private class TextFieldListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
str=textField.getText().trim();
textField.setText("");
try {
output.writeObject(str);
output.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
摘自 xpp02