ServerSocket java not reading inputStream?

后端 未结 3 1995
小蘑菇
小蘑菇 2021-01-27 13:06

I have written a server is java here is the code:

public mainClass() 
{ 
    try 
    { 
        ss = new ServerSocket(8080); 

        while (true) 
        { 
         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-27 14:09

    BufferedReader can be dangerous; the buffering can cause short lines to get "stuck" if you're only reading a little data at a time, or if the data is coming across a network. If you're only using BufferedReader to get readLine(), then do this:

    new BufferedReader(new InputStreamReader(socket.getInputStream()), 1);
    

    That extra argument sets the buffer size to 1 character, effectively turning it off. That generally solves this kind of problem.

提交回复
热议问题