views:

712

answers:

1

I have a simple client-server app on android. the android service communicates with the server via tcp sockets. the service sends a simple String to server which works. the server processes the string and sends back an object to the android service. the object implements the serializable interface. the object "leaves" the server successfully but at the point where the android service receives the object (socket.readObject()) I get to following exception:

java.net.SocketException: Bad socket

I've never seen this. What does that mean?

Edit:

Method where exception gets thrown:

private static void startContextListener(){
 new Thread(){
  public void run(){
   try{
    while(contextsocket != null && !contextsocket.isClosed() && inContext.readObject() != null){
     kontext = (Kontext) inContext.readObject();
    }
   }
   catch(Exception e){
    Log.e(TAG, "startContextListener(): " + e.toString());
   }
  }
 }.start();
}
+2  A: 

This is usually a result of read(2) on invalid socket descriptor ([EBADF] fildes is not a valid file or socket descriptor open for reading) so it looks that some layer of the app is closing the socket. Can you post more code to demonstrate how you work with the socket?

Nikolai N Fetissov
private static void startContextListener(){ new Thread(){ public void run(){ try{ while(contextsocket != null }}catch(Exception e){Log.e(TAG, "startContextListener(): " + e.toString());}}}.start();}This is the method that throws the exceptionI implemented this app and the server app in two different eclipse packages. The serializable classes are the same written java code but they are two different files, one file in each project each. Is that the problem?
@moppel: you'd be much better off editing the question itself to add the code. Now to the code - I don't see explicit socket manipulation in the snippet you posted, BUT it looks like you are trying to read your object TWICE in each loop iteration - once in the conditional (and losing it), and second time in the body of the loop. That might be a problem.
Nikolai N Fetissov