mercredi 11 mars 2015

Message passing using socket not working properly


Vote count:

0




I have implemented message passing system using socket. But this is not working all the the time . I am getting these exception some times connect failed: ETIMEDOUT (Connection timed out) and connect failed: EHOSTUNREACH (No route to host)


This is my message receving service



@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();
}


}
}


This is how i am sending the message.



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();
}
}

}


asked 1 min ago







Message passing using socket not working properly

Aucun commentaire:

Enregistrer un commentaire