I have problem with receiving response from server. My app takes picture, sends it to server and waits for response. Unfortunately I get socket is closed exception
whenever I try to receive response in Inputstream
. I am struggling with this for 2 days. I wast trying different types of Input
and Output
streams but I have no idea whi this is happening. I was trying different solutions from other similar topics but none of them works. I am not good in Android because this is one of my first apps.
Asked
Active
Viewed 201 times
0

SimpleCoder
- 33
- 1
- 8
-
Well, is the server running and accepting incoming connections? – OneCricketeer Aug 11 '16 at 16:36
-
server is written in c#. Server is running, receiving sended image and it also is sending response – SimpleCoder Aug 11 '16 at 16:37
-
a. what protocol are you using b. did you include the required permission in the manifest – donald Aug 11 '16 at 16:38
-
1`Community help me please. Thank you.`. In short you will delete this post like you did with several the last days? – greenapps Aug 11 '16 at 17:41
2 Answers
1
Closes this output stream and releases any system resources associated with this stream
You are closing the OutputSteam before getting the InputStream (which is where the error is thrown.
I'd recommend closing your socket and streams in the finally
block.

OneCricketeer
- 179,855
- 19
- 132
- 245
-
if I comment `outputstream` there is weird situation that nothing is happening - image is not sent and nothing is shown in Logcat – SimpleCoder Aug 11 '16 at 16:42
-
I didn't say remove the OutputSteam, you obviously need it to write out the byte array of the image. I simply said don't close the stream prematurely. – OneCricketeer Aug 11 '16 at 16:44
-
1ok, please check updated code because if you mean something like that it doesn't work and by saying this I mean that nothing is happening - image is not sent and Logcat is empty – SimpleCoder Aug 11 '16 at 16:49
-
Okay, no exception is a good thing, then, right? How have you checked that data is/isn't being received by the server? – OneCricketeer Aug 11 '16 at 16:53
-
Also, sockets seem very low-level... certainly you could send an HTTP Multipart Entity, no? Like [this post](http://stackoverflow.com/questions/2935946/sending-images-using-http-post), maybe? – OneCricketeer Aug 11 '16 at 16:57
-
I am sending picture to c# server and then displaying it on picturebox, and it that case nothing is happening, but if I take picture and after that close client image is sent to server.... its weird – SimpleCoder Aug 11 '16 at 17:18
-
Not sure what to tell you. Don't have much experience with C#. I was reading this post about Camera uploading. Perhaps you could get some use out of it. http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ – OneCricketeer Aug 11 '16 at 17:26
-
I don't think so that problem is on C# side because with c# client - server evrything works fine – SimpleCoder Aug 11 '16 at 17:30
-
You could try a simpler Socket application with Android. Just try to send and receive a string. You might discover the problem then. Then think of adding in the image as the "next step". – OneCricketeer Aug 11 '16 at 17:36
-
1ok, new information ! If I use synchronous server image is sent without closing the client application. If I use asynchronous server I have to close application to send image so it look likes that problem is in asynchronous server – SimpleCoder Aug 11 '16 at 18:06
-
1ok, it's working, I had to close outputstream before creating `InputStream` by using this: `socket.shutdownOutput();` – SimpleCoder Aug 12 '16 at 09:14
-
I'm not sure why that would work. I've written several codes where I get both the streams and can use them without that method. For example, https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html – OneCricketeer Aug 12 '16 at 12:19
0
java.net.SocketException arises only when u close the OutputStream (or) Socket before reading from the server.so check your code

Mayank Sharma
- 2,735
- 21
- 26