Skip to content Skip to sidebar Skip to footer

Weird Behavior : Sending Image From Android Phone To Java Server (code Working)

I'm writing an android app that sends an image to a server running a java app, and it's working in a very weird way! Here is what I do Compile and run the Java app on the desktop

Solution 1:

You're not closing any of the connections. Your Server is probably waiting until the connection is closed before it knows that data has finished being sent. When your Client runs the second time, the first connection is probably closed automatically by Android, this allowing your Server to process the image. However, if your Server doesn't process the image fast enough, maybe your Client gets 'stuck' on the second run because its waiting for the successful port connection to the Server but the Server is busy.

Long story short, how about you try to close() the Client connection when you're finished with it. Calling flush() doesn't really tell anything to the Server to say that the data has finished being sent.

If you want the socket to stay open even though you close the OutputStream, you could write a custom Socket that just overwrites the close() method, something like this...

publicclassMySocketextendsSocket {

    publicMySocket(String ipAddress, int port){
        super(ipAddress,port);
    }

    publicvoidclose(){
        // do nothing
    }

    publicvoidreallyClose(){
        super.close();
    }
}

In your code, change cliSock=new Socket(serverIP,8070); to cliSock=new MySocket(serverIP,8070);

When you call OutputStream.close(), it calls close() on MySocket, but as you can see it doesn't actually do anything. When you've completely finished with the Socket, you can call MySocket.reallyClose(); and it'll close it off properly.

Post a Comment for "Weird Behavior : Sending Image From Android Phone To Java Server (code Working)"