我正在使用 TCP java 服务器从客户端接收数据并将数据发送回客户端。问题是,当服务器写入客户端套接字的速度快于客户端读取的速度时,服务器会挂起/冻结,并且不再处理来自其他客户端的任何数据。
例如,服务器向客户端套接字写入 3 kb/s,而客户端只能读取 1 kb/s。我如何识别这个,所以我可以关闭这个套接字并停止写入它?
怎么了?数据是否被缓冲?
我使用以下代码写入数据:
this.out_ = new PrintWriter(this.clientSocket_.getOutputStream(), true);
this.out_.print(msg);
编辑:完整代码
Main.java
import java.net.Socket;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.util.HashMap;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
public class Main
{
static ServerSocket serverSocket_;
static HashMap<String, ServerInformation> servers_;
static int verboseLevel_;
static int threadTimeout_;
static int masterPort_;
static int serverNumber_;
static int socketTimeOut_;
static {
Main.serverSocket_ = null;
Main.servers_ = new HashMap<String, ServerInformation>();
Main.verboseLevel_ = 5;
Main.threadTimeout_ = 10;
Main.masterPort_ = 6510;
Main.serverNumber_ = 1;
Main.socketTimeOut_ = 6000;
}
public static void main(final String[] args) {
try {
setupServerAndCleanup(Main.masterPort_);
while (true) {
handleIncomingConnection();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
static void setupServerAndCleanup(final int port) throws IOException {
(Main.serverSocket_ = new ServerSocket()).setReuseAddress(true);
Main.serverSocket_.bind(new InetSocketAddress(Main.masterPort_));
System.out.println("Server socket up and running on port " + Main.masterPort_);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
if (Main.serverSocket_ != null) {
try {
Main.serverSocket_.close();
System.out.println("Server socket closed, port released");
}
梵蒂冈之花
相关分类