您的位置:首页 > 其它

在线聊天系统雏形!!!

2010-12-06 20:32 295 查看
ChatServer

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

/*
* 聊天雏形服务端
*/
public class ChatServer {
private boolean started = false;
private ServerSocket ss = null;
private List<Client> list = new ArrayList<Client>();

public static void main(String[] args) {
new ChatServer().start();
}

/*
* 接受连接
*/
public void start() {
try {
ss = new ServerSocket(8888);
started = true;
} catch (IOException e1) {
e1.printStackTrace();
}
try {
while (started) {
Socket s = ss.accept();
Client c = new Client(s);
list.add(c);
new Thread(c).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (ss != null) {
ss.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

/*
* 处理信息
*/
class Client implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean connected = false;

public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
connected = true;
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String t) {
try {
dos.writeUTF(t);
} catch (IOException e) {
System.out.println("Client is closed!");
}
}

public void run() {
try {
while (connected) {
String t = dis.readUTF();
for (Client c : list) {
c.send(t);
}
}
} catch (SocketException e) {
list.remove(this);
} catch (EOFException e) {
System.out.println("Client is 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 e) {
e.printStackTrace();
}
}
}
}
}


ChatClient

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 {
private TextField tf = new TextField();
private TextArea ta = new TextArea();
private Socket s = null;
private DataOutputStream dos = null;
private DataInputStream dis = null;
private boolean connected = false;
private Thread t = new Thread(new Recv());

public static void main(String[] args) {
new ChatClient().launchFrame();
}

/*
* 初始化窗口
*/
public void launchFrame() {
this.setLocation(400, 300);
this.setSize(300, 300);
this.add(ta, BorderLayout.NORTH);
this.add(tf, BorderLayout.SOUTH);
this.pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
unconnect();
System.exit(0);
}
});
tf.addActionListener(new TFListener());
this.setVisible(true);
this.connect();
t.start();
}

/*
* 连接
*/
public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
connected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/*
* 关闭连接
*/
public void unconnect() {
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (s != null) {
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

/*
* 发送信息
*/
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String t = tf.getText().trim();
tf.setText("");
try {
dos.writeUTF(t);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

/*
* 接受信息
*/
private class Recv implements Runnable {
public void run() {
try {
while (connected) {
String t = dis.readUTF();
ta.setText(ta.getText() + t + "/n");
}
} catch (SocketException e) {
System.out.println("退出了,bye!");
} catch (EOFException e) {
System.out.println("Client is closed!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: