概述
基于socket服务端、客户端的调用使用情况,我们在做项目中经常会遇到,这里基于一个示例和大家做一个分享和学习。
1.socket服务端定义
public class ServerHandler implements ClientCommandHandler, ClientEventHandler {
public static HashMap<String, ClientHandler> ConnectPool = new HashMap<String, ClientHandler>();
ServerMsgLogicDao dao = new ServerMsgLogicDao();
public ServerHandler() {
}
public void gotConnected(ClientHandler handler) throws SocketTimeoutException, IOException {
handler.setDataMode(DataMode.STRING, DataType.IN);
handler.setDataMode(DataMode.STRING, DataType.OUT);
String ip = handler.getHostAddress();
System.out.println("::socket::获得一个连接,IP地址:" + ip);
if (ConnectPool.get(ip) != null) {
// 如果来自同一个ip,比如本机开多个客户端连接,后面加个唯一串区别
ConnectPool.put(ip + "-" + java.util.UUID.randomUUID(), handler);
} else {
ConnectPool.put(ip, handler);
}
}
public void lostConnection(ClientHandler handler) throws IOException {
System.out.println("::socket::失去一个连接");
String ip = handler.getHostAddress();
if (ConnectPool.get(ip) != null) {
ConnectPool.remove(ip);
}
}
public void closingConnection(ClientHandler handler) throws IOException {
System.out.println("::socket::关闭一个连接");
String ip = handler.getHostAddress();
if (ConnectPool.get(ip) != null) {
ConnectPool.remove(ip);
}
}
public void handleCommand(ClientHandler handler, String command_src) throws SocketTimeoutException, IOException {
String command = URLDecoder.decode(command_src, "UTF-8");
dao.tcpReceiveMsg(command, handler);
}
}
2.socket客户端定义
package com.test.socket.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URLDecoder;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSON;
import com.test.socket.msg.MsgBean;
public class ClientHandlerTest implements Runnable {
public Socket socket;
private BufferedReader in;
public PrintWriter out;
private String serverIP;
private int serverPort;
ClientMsgLogicDao dao;
public ClientHandler(String ip, int port) {
this.serverIP = ip;
this.serverPort = port;
dao = new ClientMsgLogicDao(this);
}
public BufferedReader getBufferedReader() throws IOException {
Reader inputStream = new InputStreamReader(socket.getInputStream());
return new BufferedReader(inputStream);
}
public PrintWriter getPrintWriter() throws IOException {
OutputStream outputStream = socket.getOutputStream();
return new PrintWriter(outputStream, true);
}
/**
* 执行相关的业务的方法的信息
*/
public void startConnect() {
try {
socket = new Socket(this.serverIP, this.serverPort);
in = getBufferedReader();
out = getPrintWriter();
String line = null;
while ((line = in.readLine()) != null) {
receiveMsg(line);
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
// System.exit(1);
}
}
public void receiveMsg(String rsMsg) {
dao.receiveMsg(rsMsg);
}
public void sendServerMsg(String rsMsg) {
String msg =rsMsg;
System.out.println(msg);
try {
msg = URLEncoder.encode(msg, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (socket != null && socket.isConnected() && !socket.isClosed() && out != null) {
try {
out.println(msg);
//System.out.println("::==>client to server message:" + msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
startConnect();
}
}
3.socket调用说明
a、demo运行说明:
1)、eclipse加载demo工程
2)、服务端启动,mian方法运行 com.test.socket.server.ServerStart.java
3)、客户端启动,mian方法运行 com.test.socket.client.ClientStart.java
4)、运行成功验证标准:
服务端控制台打印:
::socket::获得一个连接,IP地址:127.0.0.1
::==>client to server message:01001
客户端控制台打印:
::发送成功
::发送成功
最后
以上就是飘逸大神为你收集整理的socket服务的注册、调用使用实例的全部内容,希望文章能够帮你解决socket服务的注册、调用使用实例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复