Android如何重用Socket?

时间:2012-05-15 16:17:09

标签: java android sockets http-protocols

如何重新连接TCP Socket连接(可能的setReuseAddress(true),但对我来说不起作用)从服务器(Apache,HTTPD)中连接多个文件而不断开连接(并再次连接)。

我的代码如下..

public class ClientThread implements Runnable {
        public void run() {
            try {

                Log.d("ClientActivity", "C: Connecting...");
                SocketAddress sockaddr = new InetSocketAddress(serverIpAddress, SERVERPORT);
                socket = new Socket();

                socket.setReuseAddress(true);
                socket.setKeepAlive(true);
                socket.setSoLinger(true, 3000);
                socket.connect(sockaddr);

                if (socket.isConnected()) {
                    DataInputStream input = new DataInputStream(socket.getInputStream());
                    PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

                    output.println("GET /way/images/profile/1231 HTTP/1.1");
                    output.println("Host: 192.168.1.2");
                    output.println("User-Agent: Java");
                    output.println("Accept: */*");
                    output.println("Connection: Keep-Alive");
                    output.println("");
                    output.flush();

                    String line;
                    File file = new File("/sdcard/aaa.png");
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    FileOutputStream fileOut = new FileOutputStream(file);
                    System.out.println("Getting first file");
                    while ( (line = input.readLine()) != null ) {
                        System.out.println(line);
                        fileOut.write(line.getBytes());
                    }
                    System.out.println("First file finished");
                    fileOut.flush();
                    fileOut.close();

                    output.println("GET /way/images/profile/1231 HTTP/1.1");
                    output.println("Host: 192.168.1.2");
                    output.println("User-Agent: Java");
                    output.println("Accept: */*");
                    output.println("Connection: Keep-Alive");
                    output.println("");
                    output.flush();

                    file = new File("/sdcard/aaa1.png");
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    fileOut = new FileOutputStream(file);
                    System.out.println("Getting second file");
                    while ( (line = input.readLine()) != null ) {
                        System.out.println(line);
                        fileOut.write(line.getBytes());
                    }
                    System.out.println("Second file finished");

                    fileOut.flush();
                    fileOut.close();
                    input.close();
                    output.close();

                }
                Log.d("ClientActivity", "C: Closed.");
            } catch (Exception e) {
                Log.e("ClientActivity", "C: Error", e);
                connected = false;
            }

            finally {
                try {
                    Log.d("ClientActivity", "Socket: Closed.");
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

...谢谢

1 个答案:

答案 0 :(得分:0)

绑定套接字之前的

setReuseAddress(true)允许套接字绑定,即使先前的连接处于超时状态。

documentation of setReuseAddress

直接使用socket进行HTTP通信我没有意义。我使用HttpURLConnection。在大多数情况下工作没有问题。