先上效果图
SocketCommunication.gifDemo说明
有三个模拟器,通过Socket,将信息相互发送到对方的设备上。其中的认证客户端是指将此客户端的信息传到服务端,以便在客户端一对一发送消息时,服务端可以找到相应的设备。
定义客户端代码
public class ClientConnector {
private Socket mClient;
/**
* 服务端的ip
*/
private String mDstName;
/**
* 服务端端口号
*/
private int mDesPort;
private ConnectLinstener mListener;
public ClientConnector(String dstName, int dstPort) {
this.mDstName = dstName;
this.mDesPort = dstPort;
}
/**
* 与服务端进行连接
*
* @throws IOException
*/
public void connect() throws IOException {
if (mClient == null) {
mClient = new Socket(mDstName, mDesPort);
}
//获取其他客户端发送过来的数据
InputStream inputStream = mClient.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
String data = new String(buffer, 0, len);
//通过回调接口将获取到的数据推送出去
if (mListener != null) {
mListener.onReceiveData(data);
}
}
}
/**
* 认证方法,这个方法是用来进行客户端一对一发送消息的
* 在实际项目中进行即时通讯时都需要进行登录,这里就是
* 模拟客户端的账号
*
* @param authName
*/
public void auth(String authName) throws IOException {
if (mClient != null) {
//将客户端账号发送给服务端,让服务端保存
OutputStream outputStream = mClient.getOutputStream();
//模拟认证格式,以#开头
outputStream.write(("#" + authName).getBytes());
}
}
/**
* 将数据发送给指定的接收者
*
* @param receiver 信息接数者
* @param data 需要发送的内容
*/
public void send(String receiver, String data) throws IOException {
OutputStream outputStream = mClient.getOutputStream();
//模拟内容格式:receiver+ # + content
outputStream.write((receiver + "#" + data).getBytes());
}
/**
* 断开连接
*
* @throws IOException
*/
public void disconnect() throws IOException {
if (mClient != null) {
mClient.close();
mClient = null;
}
}
public void setOnConnectLinstener(ConnectLinstener linstener) {
this.mListener = linstener;
}
/**
* 数据接收回调接口
*/
public interface ConnectLinstener {
void onReceiveData(String data);
}
}
定义服务端代码
public class SocketServer {
/**
* 用来保存不同的客户端
*/
private static Map<String, Socket> mClients = new LinkedHashMap<>();
public static void main(String[] args) {
int port = 9999;
try {
//创建服务器
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
//获取客户端的连接
final Socket socket = serverSocket.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
//读取从客户端发送过来的数据
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
String data = new String(buffer, 0, len);
//先认证客户端
if (data.startsWith("#")) {
mClients.put(data, socket);
} else {
//将数据发送给指定的客户端
String[] split = data.split("#");
Socket c = mClients.get("#" + split[0]);
OutputStream outputStream = c.getOutputStream();
outputStream.write(split[1].getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
网友评论