使用套接字传递的消息无法正常工作

时间:2015-03-11 07:18:03

标签: android sockets

我已经使用socket实现了消息传递系统。但这并不是一直都在运作。我有时会收到这些异常连接失败:ETIMEDOUT(连接超时)连接失败:EHOSTUNREACH(无主机路由)

这是我的留言服务

@Override
    protected void onHandleIntent(Intent intent) {

        while(true)
        {
            try 
            {
                Log.v(TAG, "Robo is ready to listen...");
                OutputLog.appendLog(TAG, "Robo is ready to listen....");

                socket = serverSocket.accept();

                Log.v(TAG, "Robo got the socket");
                OutputLog.appendLog(TAG, "Robo got the socket");

                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                message = reader.readLine();

                mHandler.post(new DisplayToast(this, "Message is "+message));

                Log.v(TAG, "Received message is "+message);
                OutputLog.appendLog(TAG, "Recevied message is "+message);

                //send broadcast here
                BroadcastMessage();

            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                Log.v(TAG, "Robo got exception");
                OutputLog.appendLog(TAG, "Robo got     exception......"+e.toString());

                e.printStackTrace();
            }
            finally
            {   
                Log.v(TAG, "Robo finally closed the connection");
                OutputLog.appendLog(TAG, "Robo finally closing connection");

                closeConnection();
            }


        }
    }

这就是我发送邮件的方式。

public class SendMessage extends Thread
{
    private Socket socket;
    private PrintWriter writer;
    private String message;
    private Handler mHandler;
    private Context context;
    private static int threadcount;

    private final static String TAG = "SendMessage";
    private static String IP = "10.0.0.173"; 


    public SendMessage(Context context, String message)
    {

        super("SendingThread "+(threadcount++));
        this.message = message;  
        this.context = context;
        mHandler = new Handler();
    }


    public void run()
    {
        try 
        {
            Log.v(TAG, "Sending message to robohr...");
            OutputLog.appendLog(TAG, "Sending message...");

            socket = new Socket(InetAddress.getByName(IP), 4891);
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            writer.println(message);

            writer.flush();
            Log.v(TAG, "Message sent to robohr");
            OutputLog.appendLog(TAG, "message sent to hr");
        }
        catch (SocketTimeoutException ex)
        {
            ex.printStackTrace();
            OutputLog.appendLog(TAG, ex.toString());
        }
        catch (UnknownHostException ex) 
        {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            OutputLog.appendLog(TAG, ex.toString());
        } 
        catch (IOException ex) 
        {
            // TODO Auto-generated catch block
            ex.printStackTrace();
            OutputLog.appendLog(TAG, ex.toString());
        }
        finally
        {
            closeConnection();
            OutputLog.appendLog(TAG, "Close connection (finally)");
        }

    }


    private void closeConnection()
    {
        if(socket != null)
        {
           try 
           {
            socket.close();
           } 
           catch (IOException e) 
           {
            // TODO Auto-generated catch block
            e.printStackTrace();
            OutputLog.appendLog(TAG, "WHEN CLOSING...."+e.toString());
           }    
        }

        if(writer != null)
        {
            writer.close();
        }
    }   

}

0 个答案:

没有答案