JavaAWT简单Socket多人聊天程序(简单明了)
来源:互联网 发布:深圳龙华淘宝客服招聘 编辑:程序博客网 时间:2024/06/07 18:39
//-------------------------------------------Server-----------------------------------------------:
public class ChatServer {
//所有客户端对象List
List<Client> clients = new ArrayList<Client>();
//所有客户端对象Iterator
Iterator<Client> it;
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
boolean started = false;
ServerSocket ss = null;
try {
ss = new ServerSocket(5555);//端口号5555
started = true;
System.out.println("服务端已启动!");
} catch (SocketException e) {
System.out.println("端口正在被使用,请关闭相应程序!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while (started) {
Socket s = ss.accept();
//有客户端连了
Client c = new Client(s);
clients.add(c);
System.out.println("a client connected!");
new Thread(c).start();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 每个请求过来启用一个线程来处理<br>
*/
private class Client implements Runnable {
DataInputStream dis = null;
DataOutputStream dos = null;
boolean connected = false;
Client(Socket s) {
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
connected = true;
}catch(EOFException e){
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (connected) {
try {
// System.out.println("---------------------"+clients.size()+"---------------------");
it= clients.iterator();
//把客户端发的信息读进来
String msg = dis.readUTF();
//把信息写出去发给每个客户端
while (it.hasNext()) {
Client c = it.next();
try {
c.send(msg);
} catch (SocketException e) {
//从服务端发送信息失败,此client可能已关闭,所以要把它从Iterator里remove(不要用clients.remove会报ConcurrentModificationException)
it.remove();
}
}
} catch (EOFException eof) {
connected = false;
System.out.println("服务端EOFException:客户端已关闭");
}catch(SocketException e){
connected = false;
System.out.println("服务端读取信息失败:SocketException");
}catch (IOException e) {
System.out.println("服务端读取信息失败:IOException");
}
}
}
private void send(String str) throws IOException {
dos.writeUTF(str);
}
}
}
//-------------------------------------------Client-----------------------------------------------:
public class ChatClient extends Frame {
private static final long serialVersionUID = 5798410085795768914L;
public TextField tf = new TextField();
public TextArea ta = new TextArea();
DataOutputStream dos = null;
DataInputStream dis = null;
public Socket socket = null;
boolean connected = false;
Thread receiver = new Thread(new ReceiverThread());
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
this.setLocation(250, 100);
this.setSize(500, 600);
this.add(tf, BorderLayout.SOUTH);
this.add(ta, BorderLayout.NORTH);
this.pack();
this.addWindowListener(new WindowMonitor());
tf.addKeyListener(new KeyMonitor());
connect();
receiver.start();
this.setVisible(true);
}
public void connect() {
try {
socket = new Socket("127.0.0.1", 5555);//port5555
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
connected = true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void disconnect() {
try {
dis.close();
dos.close();
socket.close();
}catch(EOFException e){
System.out.println("这是合法的,客户端已经关闭");
}catch (IOException e) {
e.printStackTrace();
}
}
public void closeWindow(){
dispose();
}
private class WindowMonitor extends WindowAdapter {
@Override
public void windowClosing(WindowEvent e) {
disconnect();
// System.exit(0);
closeWindow();
}
}
private class KeyMonitor extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (KeyEvent.VK_ENTER == keyCode) {
try {
dos.writeUTF(tf.getText().trim());
dos.flush();
} catch (IOException ioe) {
System.out.println("客户端发送信息失败:"+"\n");
ioe.printStackTrace();
}
tf.setText("");
}
}
}
private class ReceiverThread implements Runnable {
public void run() {
try {
while (connected) {
String str = dis.readUTF();
ta.setText(ta.getText() + str + '\n');
}
} catch (SocketException e) {
System.out.println("客户端断开连接!SokectException");
connected = false;
} catch (EOFException e) {
System.out.println("客户端断开连接!EOFException");
connected = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- JavaAWT简单Socket多人聊天程序(简单明了)
- socket 简单聊天程序
- c#基于socket编程实现简单多人聊天程序
- Swing+socket简单聊天程序
- 简单的多人聊天(C#.Socket).
- 简单的多人聊天(C#.Socket).
- 简单的多人聊天(C#.Socket)
- 简单的Socket聊天程序(客户端)
- 基于Socket简单的聊天程序
- 一个简单的socket通信聊天程序
- [Java] Socket/ServerSocket 控制台简单聊天程序
- 非常简单的Socket聊天程序
- Java 基于socket thread 简单聊天程序
- Scala编写的简单Socket聊天程序
- 一个简单的局域网Socket聊天程序
- 学习socket(一) TCP简单聊天程序
- C#简单多人聊天程序
- java Socket ServerSocket 聊天程序 。 太简单的程序了
- 解决 Python.h:没有那个文件或目录 错误的方法
- Linux tcpdump命令详解
- 网页解析类库HtmlCleanner
- 深度理解C++对象模型读书笔记——对象模型
- 记录nodejs中async的几个接口用法
- JavaAWT简单Socket多人聊天程序(简单明了)
- linux中获取命令帮助的几种方式1
- Linux下一些有用的Shell命令
- 云计算
- Linux Shell - Shell脚本调试技术
- 查看多光谱图像同一像元的不同波段的值大小
- Java序列化(一)
- 深入探讨在集群环境中使用 EhCache 缓存系统
- jsPDF导出pdf