error while downloading file in java -java.net.SocketException: Connection reset

后端 未结 2 1787
予麋鹿
予麋鹿 2021-01-26 05:02

I\'m trying to download a file using socket and server in java.

myClient = new Socket(address,port);
   myClient.setSoTimeout(MyFileManager.TIME_OUT);
   in = ne         


        
相关标签:
2条回答
  • 2021-01-26 05:50

    Why do you think that this line returns the number of bytes in the stream??

    long size = in.readLong(); //get the size
    

    You should do in.read() until it returns -1.

    0 讨论(0)
  • 2021-01-26 05:57

    I think that the loop should be something like:

     for (int i=0; i<size; i++)...
    

    Since streams are 0 based.

    Also, you might want to keep reading until you hit the EOF rather than reading a specific number of bytes. See this tutorial to learn how :)

    0 讨论(0)
提交回复
热议问题