public class SocketClient {
private static Log logger = LogFactory.getLog(SocketClient.class);
private static Socket socket; //socket连接
private static boolean connection = false;//socket是否连接
private static int connectcount = 0;
/**
* 打开socket
* @param data 能耗数据xml
* @return 是否成功 0-成功
* @throws IOException
* @throws DocumentException
*/
public static String csocket(String sendData, int type) throws IOException, DocumentException {
open();
if(connection==false) {
return null;
}
// 处理发送的报文 转换成byte[]
byte[] message = getTcpMessage(sendData, type);
// 发送报文并获取返回报文
Document receiveData = sendAndRecMsg(message);
return XmlService.formatXML(receiveData,"utf-8");
}
/**
* 发送数据
* @param message 报文
* @return 服务器回复 style
*/
public static byte[] sendAndRecMsg(byte[] message){
if (message == null) {
return null;
}
Document do
socket短连接客户端Java实现
最新推荐文章于 2024-04-17 00:16:59 发布