ServerSocket循环收不到数据
ServerSocket receives no data in a loop
我在while循环中使用ServerSocket从客户端获取数据,它在第一轮工作运行,但在第二轮后失败。
我做了一些搜索,但仍然无法弄清楚发生了什么。
服务器端代码
package com.gorilla.main;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(44444);
while(true){
System.out.println("another round");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
System.out.println("available: "+ inputStream.available());
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
System.out.println(new String(b));
System.out.println("=======================");
socket.close();
}
}
}
客户端代码
package com.gorilla.main;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client2 {
public static void main(String [] args) throws Exception{
Socket socket = new Socket("127.0.0.1", 44444);
String s = "Hello World";
byte [] b = s.getBytes();
socket.getOutputStream().write(b);;
socket.close();
}
}
以及我 运行 客户端 3 次后服务器端控制台的输出。
another round
available: 11
Hello World
=======================
another round
available: 0
=======================
another round
available: 0
=======================
another round
如有任何建议,我们将不胜感激。谢谢
您使用 InputStream.available()
来调整缓冲区大小,而这不是从套接字读取数据的方式。您应该分配一个缓冲区(通常是静态大小,或者可能是可配置的)并循环读取
// server code
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) > -1) {
// do something
}
InputStream.available():
的 Javadoc
Returns an estimate of the number of bytes that can be read (or
skipped over) from this input stream without blocking by the next
invocation of a method for this input stream. The next invocation
might be the same thread or another thread. A single read or skip of
this many bytes will not block, but may read or skip fewer bytes.
如果您的协议是基于文本的,您可以将套接字的输入流包装在 Scanner
中,因此循环变为
while (scanner.hasNextLine()) {
String line = scanner.next();
}
我在while循环中使用ServerSocket从客户端获取数据,它在第一轮工作运行,但在第二轮后失败。
我做了一些搜索,但仍然无法弄清楚发生了什么。
服务器端代码
package com.gorilla.main;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(44444);
while(true){
System.out.println("another round");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
System.out.println("available: "+ inputStream.available());
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
System.out.println(new String(b));
System.out.println("=======================");
socket.close();
}
}
}
客户端代码
package com.gorilla.main;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client2 {
public static void main(String [] args) throws Exception{
Socket socket = new Socket("127.0.0.1", 44444);
String s = "Hello World";
byte [] b = s.getBytes();
socket.getOutputStream().write(b);;
socket.close();
}
}
以及我 运行 客户端 3 次后服务器端控制台的输出。
another round
available: 11
Hello World
=======================
another round
available: 0
=======================
another round
available: 0
=======================
another round
如有任何建议,我们将不胜感激。谢谢
您使用 InputStream.available()
来调整缓冲区大小,而这不是从套接字读取数据的方式。您应该分配一个缓冲区(通常是静态大小,或者可能是可配置的)并循环读取
// server code
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) > -1) {
// do something
}
InputStream.available():
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
如果您的协议是基于文本的,您可以将套接字的输入流包装在 Scanner
中,因此循环变为
while (scanner.hasNextLine()) {
String line = scanner.next();
}