Read Data from a Java Socket

前端 未结 4 1412
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 13:53

I have a Socket listening on some x port.

I can send the data to the socket from my client app but unable to get any response from the server socket.



        
4条回答
  •  逝去的感伤
    2021-01-05 14:06

    For me this code is strange:

    bis.readLine()
    

    As I remember, this will try to read into a buffer until he founds a '\n'. But what if is never sent?

    My ugly version breaks any design pattern and other recommendations, but always works:

    int bytesExpected = clientSocket.available(); //it is waiting here
    
    int[] buffer = new int[bytesExpected];
    
    int readCount = clientSocket.read(buffer);
    

    You should add the verifications for error and interruptions handling too. With webservices results this is what worked for me ( 2-10MB was the max result, what I have sent)

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题