第一种,PC作为服务器,APP做客户端
服务端代码:
try {
mServerSocket = new ServerSocket(8002);
while (mCanRun) {
System.out.println("等待连接..." + mServerSocket.getInetAddress());
Socket socket = mServerSocket.accept();
System.out.println("已连接...");
ReadThread socketThread = new ReadThread(socket);
if (mList == null) {
mList = new ArrayList<>();
}
mList.add(socketThread);
socketThread.start();
socketThread.sendMsg("你是第" + mList.size() + "个");
}
} catch (Exception e) {
e.printStackTrace();
}
客户端代码:Socket socket = new Socket("127.0.0.1", 8002); 即可连接成功。