java.net.SocketException:sendto 失败:ECONNRESET(连接被对等方重置)

java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)

我尝试使用此代码连接到服务器以上传图片。

    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("connection", "close");
        System.setProperty("http.keepAlive", "false");
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            sb = new StringBuilder();
            String response;
            while ((response = br.readLine()) != null){
                sb.append(response);
            }
        }
    } catch (IOException e){
        if (e.getMessage().indexOf("Connection reset by peer") > 0);
    } catch (Exception e) {
        e.printStackTrace();
    }

我总是得到 SocketException,如上。

04-26 18:53:26.091 13039-13092/kovacsdev.hu.facec W/System.err: java.net.SocketException: sendto failed: ECONNRESET (Connection reset by peer)
04-26 18:53:26.092 13039-13039/kovacsdev.hu.facec I/SurfaceTextureClient: [STC::queueBuffer] (this:0x53cf5360) fps:45.86, dur:1002.96, max:57.77, min:1.67
04-26 18:53:26.095 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:550)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:519)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:511)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.net.PlainSocketImpl.access0(PlainSocketImpl.java:46)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at java.io.ByteArrayOutputStream.writeTo(ByteArrayOutputStream.java:231)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.RetryableOutputStream.writeToSocket(RetryableOutputStream.java:70)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:814)
04-26 18:53:26.096 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:293)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:505)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at kovacsdev.hu.facec.RequestHandler.sendPostRequest(RequestHandler.java:43)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at kovacsdev.hu.facec.UploadUIUploadImage.doInBackground(UploadUI.java:108)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at kovacsdev.hu.facec.UploadUIUploadImage.doInBackground(UploadUI.java:82)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:287)
04-26 18:53:26.097 13039-13092/kovacsdev.hu.facec W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err:     at java.lang.Thread.run(Thread.java:838)
04-26 18:53:26.098 13039-13092/kovacsdev.hu.facec W/System.err: Caused by: libcore.io.ErrnoException: sendto failed: ECONNRESET (Connection reset by peer)
04-26 18:53:26.099 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.Posix.sendtoBytes(Native Method)
04-26 18:53:26.099 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.Posix.sendto(Posix.java:151)
04-26 18:53:26.100 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
04-26 18:53:26.100 13039-13092/kovacsdev.hu.facec W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:517)

UploadUI 在第 108 行包含此代码段:

String result = rh.sendPostRequest(UPLOAD_URL,data);

其中 rh 代表 RequestHandler。它应该 post 图片的详细信息在 SQL 数据库中,但服务器端代码似乎有效。

你好,我也面临着同样的问题 problme.Now 使用下面的方法解决了 code.I 希望它也能帮助你..试试吧....

将下面的代码放在 onCreate() 中

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload_documents);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

   //Statement or Controls Declaration or your code

}