现在有个程序用的是长连接,我要把它改成短连接,改后的代码如下
public static String waiting(String xml, String uuid, String method)
throws Exception {
System.out.println("xml:\n"+xml);
String result=null;
// 默认等待超时时间
//int waitTime = Integer.parseInt(XMLConfig.getInstance().getParameter("SocketConn", "timeout"));
//REMOTEPORT = Integer.parseInt(XMLConfig.getInstance().getParameter("SocketConn", "remotePort"));
//REMOTEIP = XMLConfig.getInstance().getParameter("SocketConn", "remoteIP");
Socket socket = null;
DataInputStream reader;
DataOutputStream writer;
byte[] all = null;
try {
socket=new Socket("132.32.24.106",6001);
reader = new DataInputStream(socket.getInputStream());
writer = new DataOutputStream(socket.getOutputStream());
System.out.println(socket.isConnected());
byte[] packageMsg = xml.getBytes();
writer.write(packageMsg);// 发送请求
writer.flush();
System.out.println("write over");
for(int i=0;i<10;i++){
if (socket != null && !socket.isClosed() && !socket.isInputShutdown())
{
int available = 0;
try
{
available = reader.available();
}
catch (IOException e1)
{
e1.printStackTrace();
}
System.out.println("available:" + available);
if (available > 0)
{
// 先读取报文长度
byte[] recive = new byte[SocketUtil.LENGTH_TT];
reader.read(recive);
String lengths = new String(recive);
int length = Integer.parseInt(lengths);// 总长度
System.out.println("recive totallength:" + length);
all = new byte[length];
System.arraycopy(recive, 0, all, 0, SocketUtil.LENGTH_TT);
// 接收指定长度报文
if (length > SocketUtil.LENGTH_TT)
{
int leftL = length - SocketUtil.LENGTH_TT;// 每次接受还剩下的长度
while (leftL > 0)
{
recive = new byte[leftL];
int realL = reader.read(recive);
System.arraycopy(recive, 0, all, length - leftL, realL);
leftL = leftL - realL;
}
}
if (length == 32)
{
//HeartJump(all);
}
else
{
result=new String(all);
}
}
}
Thread.sleep(1000);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (socket != null)
{
if (!socket.isClosed())
{
System.out.println("关闭socket:");
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
System.out.println(socket.isInputShutdown());
System.out.println(socket.isOutputShutdown());
System.out.println(socket.isClosed());
System.out.println(socket.isConnected());
}
}
}
if (result == null) {
return ExceptionMsg.EXCEPTION + ExceptionMsg.TIME_OUT;
} else {
return result;
} }
第一个问题:
服务器serversocket端口是6001
socket=new Socket("132.32.24.106",6001);我这样建立socket连接,为什么到了服务器那边端口变成了23 而不是6001
netstat -an|grep 132.32.24.176
tcp 0 0 132.32.24.106.23 132.32.24.176.58984 ESTABLISHED
tcp 0 2 132.32.24.106.23 132.32.24.176.64735 ESTABLISHED
tcp 0 0 132.32.24.106.23 132.32.24.176.62611 ESTABLISHED
第二个问题:
这段代码有明显错误吗,为什么available 一直是0,没有返回数据?socket短连接
public static String waiting(String xml, String uuid, String method)
throws Exception {
System.out.println("xml:\n"+xml);
String result=null;
// 默认等待超时时间
//int waitTime = Integer.parseInt(XMLConfig.getInstance().getParameter("SocketConn", "timeout"));
//REMOTEPORT = Integer.parseInt(XMLConfig.getInstance().getParameter("SocketConn", "remotePort"));
//REMOTEIP = XMLConfig.getInstance().getParameter("SocketConn", "remoteIP");
Socket socket = null;
DataInputStream reader;
DataOutputStream writer;
byte[] all = null;
try {
socket=new Socket("132.32.24.106",6001);
reader = new DataInputStream(socket.getInputStream());
writer = new DataOutputStream(socket.getOutputStream());
System.out.println(socket.isConnected());
byte[] packageMsg = xml.getBytes();
writer.write(packageMsg);// 发送请求
writer.flush();
System.out.println("write over");
for(int i=0;i<10;i++){
if (socket != null && !socket.isClosed() && !socket.isInputShutdown())
{
int available = 0;
try
{
available = reader.available();
}
catch (IOException e1)
{
e1.printStackTrace();
}
System.out.println("available:" + available);
if (available > 0)
{
// 先读取报文长度
byte[] recive = new byte[SocketUtil.LENGTH_TT];
reader.read(recive);
String lengths = new String(recive);
int length = Integer.parseInt(lengths);// 总长度
System.out.println("recive totallength:" + length);
all = new byte[length];
System.arraycopy(recive, 0, all, 0, SocketUtil.LENGTH_TT);
// 接收指定长度报文
if (length > SocketUtil.LENGTH_TT)
{
int leftL = length - SocketUtil.LENGTH_TT;// 每次接受还剩下的长度
while (leftL > 0)
{
recive = new byte[leftL];
int realL = reader.read(recive);
System.arraycopy(recive, 0, all, length - leftL, realL);
leftL = leftL - realL;
}
}
if (length == 32)
{
//HeartJump(all);
}
else
{
result=new String(all);
}
}
}
Thread.sleep(1000);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (socket != null)
{
if (!socket.isClosed())
{
System.out.println("关闭socket:");
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
System.out.println(socket.isInputShutdown());
System.out.println(socket.isOutputShutdown());
System.out.println(socket.isClosed());
System.out.println(socket.isConnected());
}
}
}
if (result == null) {
return ExceptionMsg.EXCEPTION + ExceptionMsg.TIME_OUT;
} else {
return result;
} }
第一个问题:
服务器serversocket端口是6001
socket=new Socket("132.32.24.106",6001);我这样建立socket连接,为什么到了服务器那边端口变成了23 而不是6001
netstat -an|grep 132.32.24.176
tcp 0 0 132.32.24.106.23 132.32.24.176.58984 ESTABLISHED
tcp 0 2 132.32.24.106.23 132.32.24.176.64735 ESTABLISHED
tcp 0 0 132.32.24.106.23 132.32.24.176.62611 ESTABLISHED
第二个问题:
这段代码有明显错误吗,为什么available 一直是0,没有返回数据?socket短连接
使用一个线程,线程中有run方法,方法内一般写
while(条件){
//读取操作
}之所以这么写,是因为你不确定服务器端什么时候给你返回数据。