日期:2014-05-20  浏览次数:20768 次

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)
                    {
                // 先读取报文长度