当前位置: 代码迷 >> J2SE >> SOCKET短连接的有关问题
  详细解决方案

SOCKET短连接的有关问题

热度:75   发布时间:2016-04-23 22:33:45.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)
                    {
                // 先读取报文长度
  相关解决方案