Java Socket抛出IOException有时候

时间:2014-04-06 13:34:53

标签: java android sockets network-programming ioexception

我正在制作一个需要在网络打印机上打印收据的Android应用程序。我使用的打印机是"热敏收据打印机Tysso PRP 300"。

我的问题是有时会打印,打印2到3次后会停止打印并开始接收IOException。我尝试重新启动Mobile / Application / Force-Stop / Printer / Pinged-Printer但它什么也没做。

使用AsyncTask进行沟通。

以下是我的套接字代码:

private class MyPrinter extends AsyncTask<String, String, String>
    {

    Socket sock;
    PrintWriter oStream;
    DataInputStream dis;

    public MyPrinter()
    {

    }

    protected String doInBackground(String... params) {

        try 
        {
            sock = new Socket(params[0], Integer.parseInt(params[1]));
            sock.setSoTimeout(300);


            if(sock.getRemoteSocketAddress() != null)
            {
            oStream = new PrintWriter(sock.getOutputStream());

            oStream.println(params[2]);

            }

            oStream.flush();
            oStream.close();

            sock.shutdownOutput();
            sock.close();

        }
        catch (UnknownHostException e) 
        {
            Log.i("cycle", "00");
            e.printStackTrace();
        } 
        catch (IOException e) 
        { 

            Log.i("cycle", "11");
            e.printStackTrace();
        }
        finally{

        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if(pDialog != null)
            if(pDialog.isShowing())
                pDialog.dismiss();

        currentOrder.dismiss();
    }

}

堆栈追踪:

04-06 18:58:55.009: W/System.err(5262): java.net.SocketException: failed to connect to /192.168.1.150 (port 9100) after 90000ms: isConnected failed: EHOSTUNREACH (No route to host)
04-06 18:58:55.018: W/System.err(5262):     at libcore.io.IoBridge.isConnected(IoBridge.java:256)
04-06 18:58:55.018: W/System.err(5262):     at libcore.io.IoBridge.connectErrno(IoBridge.java:178)
04-06 18:58:55.019: W/System.err(5262):     at libcore.io.IoBridge.connect(IoBridge.java:112)
04-06 18:58:55.020: W/System.err(5262):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-06 18:58:55.021: W/System.err(5262):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
04-06 18:58:55.021: W/System.err(5262):     at java.net.Socket.startupSocket(Socket.java:575)
04-06 18:58:55.022: W/System.err(5262):     at java.net.Socket.tryAllAddresses(Socket.java:130)
04-06 18:58:55.023: W/System.err(5262):     at java.net.Socket.<init>(Socket.java:180)
04-06 18:58:55.023: W/System.err(5262):     at java.net.Socket.<init>(Socket.java:152)
04-06 18:58:55.024: W/System.err(5262):     at com.example.pointofsale.fragments.HomeScreen_Current_Orders$MyPrinter.doInBackground(HomeScreen_Current_Orders.java:319)
04-06 18:58:55.024: W/System.err(5262):     at com.example.pointofsale.fragments.HomeScreen_Current_Orders$MyPrinter.doInBackground(HomeScreen_Current_Orders.java:1)
04-06 18:58:55.025: W/System.err(5262):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-06 18:58:55.026: W/System.err(5262):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-06 18:58:55.028: W/System.err(5262):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-06 18:58:55.028: W/System.err(5262):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-06 18:58:55.029: W/System.err(5262):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-06 18:58:55.033: I/SurfaceTextureClient(5262): [0x52739378] frames:53, duration:1.001000, fps:52.933781
04-06 18:58:55.034: W/System.err(5262):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-06 18:58:55.035: W/System.err(5262):     at java.lang.Thread.run(Thread.java:856)
04-06 18:58:55.036: W/System.err(5262): Caused by: libcore.io.ErrnoException: isConnected failed: EHOSTUNREACH (No route to host)
04-06 18:58:55.038: W/System.err(5262):     at libcore.io.IoBridge.isConnected(IoBridge.java:234)

0 个答案:

没有答案