1、ConnectToServerByTcp 这个方法是连接socket服务的
// 直接上代码
public static Socket ConnectToServerByTcp(String serverIp, int serverPort, int timeOutSecond,
int connectCS) {
// 建立通讯连接
Socket otherSocket = null;
boolean connectOk = false;
try {
// 创建一个流套接字并将其连接到指定主机上的指定端口号
otherSocket = new Socket();
System.out.println("serverIp " + serverIp + ", serverPort" + serverPort);
SocketAddress socketAddress = new InetSocketAddress(serverIp, serverPort);
// 接收超时限制在5到180秒,连接超时限制在5秒
timeOutSecond = (timeOutSecond < 1 || timeOutSecond > 180) ? 1 : timeOutSecond;
for (int i = 0; i < connectCS; i++) {
try {
otherSocket.connect(socketAddress, 5000); // 连接超时限制在5秒
otherSocket.setSoTimeout(1000 * timeOutSecond);//设置读操作超时时间5到180秒
connectOk = true;
break;
} catch (Exception e1) {
log.error(e1);
}
}
if (!connectOk) {
if (otherSocket != null)
otherSocket.close();
otherSocket = null;
}
return otherSocket;
} catch (Exception e) {
log.error(e);
e.printStackTrace();
}
return otherSocket;
}
2、上一步的连接成功后,返回的Socket传给下边这个方法,并且加上你与服务器的交互数据。socketS 是连接的对象,bufS是要发送给服务器的数据,bufR 是服务器返回给的数据
public static void SockSend(Socket socketS, Buf bufS,Buf bufR) throws PosException {
// 发送网络数据
boolean oK = false;
String sErr = "";
DataOutputStream out = null;
try {
if (bufS.get_dataLen() < 1)
sErr = "数据长度[" + bufS.get_dataLen() + "]无效";
// 向服务器端发送数据
out = new DataOutputStream(socketS.getOutputStream());
out.write(bufS.get_dataBuf(), 0, bufS.get_dataLen());
//Thread.sleep(200);
out.flush();
oK = true;
//关闭客户端的输出流。相当于给流中加入一个结束标记-1.这个样子服务器的输入流的reaLine方法就会读到一个-1,然后结束readLIne方法
socketS.shutdownOutput();
} catch (Exception e) {
log.error(e);
oK = false;
sErr = e.toString();
e.printStackTrace();
}
if (!oK)
throw new PosException("", "发送银行数据异常", sErr);
// 接收报文
int getLen = 0;
InputStream inputStream = null;
DataInputStream bs = null;
try {
inputStream = socketS.getInputStream();
bs = new DataInputStream(inputStream);
bufR.set_dataLen(0);
byte[] data = new byte[4096];
getLen = bs.read(data);
bufR.set_bufLen(getLen);
log.info("TcpReadLen======" + getLen);
if (getLen > 1) {
System.arraycopy(data, 0, bufR.get_dataBuf(), 0, getLen);
bufR.set_dataLen(getLen);
log.info("TcpReceiveMsg======" + ByteUtils.getString(bufR.get_dataBuf()));
} else {
sErr = "接收数据失败:实收长度[" + getLen + "]无效";
getLen = 0;
bufR.set_dataLen(0);
}
bs.close();
inputStream.close();
socketS.close();
log.info("-------主动断开socket通讯 ");
} catch (IOException e2) {
log.error(e2);
e2.printStackTrace();
}
if (getLen < 2) {
throw new PosException("", "接收数据失败", sErr);
}
}
3,Buf实体类
/**
* 封装处理过的数据缓冲类
*
*/
public class Buf{
/**
* 缓冲区长度 4*1024 固定長度
*/
protected int _bufLen = 0;
/**
* 数据长度
*/
protected int _dataLen = 0;
/**
* Byte类型缓冲数组
*/
protected byte[] _dataBuf = null;
public int get_bufLen() {
return _bufLen;
}
public void set_bufLen(int _bufLen) {
//数据缓冲区长度无效时,不作任何操作
if (_bufLen > this._bufLen && _bufLen > 0){
//数据缓冲区长度不能小于64个字节
if (_bufLen > 64) {
this._bufLen = _bufLen;
} else {
this._bufLen = 64;
}
//当数据缓冲区内有数据时,复制元数据到新的据缓数冲
if (_dataLen > 0){
byte[] bufOld = new byte[_dataLen + 1];
System.arraycopy(_dataBuf, 0, bufOld, 0, _dataLen);
_dataBuf = new byte[this._bufLen + 1];
System.arraycopy(bufOld, 0, _dataBuf, 0, _dataLen);
_dataBuf[_dataLen] = 0x0;
} else {
_dataBuf = new byte[this._bufLen + 1];
}
}
}
/**
* 数据长度
*/
public int get_dataLen() {
return _dataLen;
}
public void set_dataLen(int _dataLen) {
if (_dataLen >= 0) {
this._dataLen = _dataLen;
} else {
this._dataLen = 0;
}
if (this._dataLen > _bufLen)
{
this._dataLen = _bufLen;
}
}
/// <summary>
/// 缓冲区
/// </summary>
public byte[] get_dataBuf() {
return _dataBuf;
}
public void set_dataBuf(byte[] _dataBuf) {
this._dataBuf = _dataBuf;
}
/// <summary>
/// 构造方法
/// 设置缓冲区长度为64,数据长度为0
/// </summary>
public Buf(){
_bufLen = 64;
_dataLen = 0;
}
/// <summary>
/// 构造方法
/// 设置缓冲区长度为指定长度,数据长度为0
/// </summary>
/// <param name="bufLen">缓冲区长度</param>
public Buf(int bufLen){
_bufLen = bufLen;
_dataLen = 0;
}
}
3、模拟测试接收数据正常,OK