赞
踩
服务端类
- public class SocketServer {
- ServerSocket serverSocket = null;
- Socket socket = null;
- DataInputStream dis = null;
- DataOutputStream dos = null;
- // 构造函数
- public SocketServer() throws IOException {
- serverSocket = new ServerSocket(12345);
- }
- public void startService() throws IOException {
- System.out.println("Waiting for client to connect...");
- // 服务端在while里不断监听是否有新的客户端接入
- while(true) {
- // 等待一个客户端的连接,在连接之前,此方法是阻塞的
- socket = serverSocket.accept();
- // 收发消息线程
- new ConnectThread(socket).start();
- }
- }
-
- //向客户端发送信息
- class ConnectThread extends Thread
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。