package com.lyl.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.util.ArrayList;
import java.util.List;
public class ChatServer {
private ServerSocket serverSocket;
private boolean isStart;
// 所有在线的客户端
List<ClientThread> clients = new ArrayList<ClientThread>();
public static void main(String[] args) {
new ChatServer().startService();
}
public void startService() {
try {
serverSocket = new ServerSocket(9999);
isStart = true;
System.out.println("9999端口 等待客户端连接……");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (isStart) {
Socket socket = serverSocket.accept();
ClientThread client = new ClientThread(socket);
new Thread(client).start();
clients.add(client);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/* 处理单个客户端的线程类 */
class ClientThread implements Runnable {
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
private boolean isRun;
public ClientThread(Socket s) {
this.socket = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
isRun = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String s) {
try {
dos.writeUTF(s);
System.out.println(s);
} catch (IOException e) {
clients.remove(this);
e.printStackTrace();
}
}
@Override
public void run() {
try {
while (isRun) {
String s = dis.readUTF();
for (int i = 0; i < clients.size(); i++) {
// 对在线的客户端发送消息
ClientThread c = clients.get(i);
c.send(s);
}
}
} catch (EOFException e) {
clients.remove(this);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dis != null)
dis.close();
if (dos != null)
dos.close();
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
简易Android在线聊天(Client+Server).zip (36个子文件)
ChatQQ
.project 842B
bin
ChatQQ.apk 17KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
resources.ap_ 12KB
classes
com
lyl
chat
R$string.class 380B
ChatActivty$1.class 1KB
R$layout.class 404B
MainActivity.class 2KB
R$drawable.class 389B
ChatActivty.class 3KB
R.class 489B
R$attr.class 322B
ChatActivty$ReadServerMsg.class 1KB
R$id.class 495B
classes.dex 6KB
proguard.cfg 1KB
AndroidManifest.xml 1KB
src
com
lyl
chat
MainActivity.java 1KB
ChatActivty.java 2KB
project.properties 360B
res
values
strings.xml 114B
layout
chat.xml 1KB
main.xml 2KB
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 3KB
.classpath 364B
gen
com
lyl
chat
R.java 979B
assets
ChatQQServer
.project 388B
bin
com
lyl
chatServer
ChatServer.class 2KB
ChatServer$ClientThread.class 3KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
lyl
chatServer
ChatServer.java 2KB
.classpath 301B
共 36 条
- 1
刘云龙在搞机
- 粉丝: 56
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页