gpt4 book ai didi

java - 多次读取时,从 TCP 服务器的读取挂起

转载 作者:可可西里 更新时间:2023-11-01 02:44:50 26 4
gpt4 key购买 nike

我正在测试一些 TCP 代码,除了一个问题外,它似乎工作正常。当没有更多内容可读时,从套接字的读取挂​​起在其中一种方法中:

这是TCP代码:

package com.comp424.service;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TCPService implements Runnable
{
protected int serverPort;
protected InetAddress bindAddress;

protected ServerSocket serverSocket = null;
protected boolean isStopped = false;
protected Thread runningThread = null;

protected ExecutorService threadPool = Executors.newFixedThreadPool(10);

public TCPService(String host,int port)
{
serverPort = port;

try
{
bindAddress = InetAddress.getByName(host);
}
catch (UnknownHostException e)
{
throw new RuntimeException("Failed to get bind address", e);
}
}

private void start()
{
try
{
serverSocket = new ServerSocket(serverPort, 10, bindAddress);
}
catch (IOException e)
{
throw new RuntimeException("Cannot open port " + serverPort, e);
}
}

public void run()
{
synchronized (this)
{
runningThread = Thread.currentThread();
}

start();

while (!isStopped())
{
Socket clientSocket = null;

try
{
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
if (isStopped())
{
System.out.println("Server Stopped.");
break;
}
throw new RuntimeException("Error accepting client connection", e);
}

threadPool.execute(new ClientHandler(clientSocket));
}
threadPool.shutdown();

System.out.println("Server Stopped.");
}

public synchronized void stop()
{
isStopped = true;

try
{
serverSocket.close();
}
catch (IOException e)
{
throw new RuntimeException("Error closing server", e);
}
}

private synchronized boolean isStopped()
{
return isStopped;
}
}

package com.comp424.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import com.comp424.impl.dao.DaoFactory;
import com.comp424.intf.dao.ICourseDao;
import com.comp424.intf.dao.IPersonDao;
import com.comp424.intf.dao.IRegisterCourseDao;
import com.comp424.model.Course;
import com.comp424.model.Person;

public class ClientHandler implements Runnable
{
private static IRegisterCourseDao registrationDao;
private static IPersonDao personDao;
private static ICourseDao courseDao;

protected Socket clientSocket = null;

public ClientHandler(Socket socket)
{
registrationDao = DaoFactory.getInstance().getCourseRegistrationDao();
personDao = DaoFactory.getInstance().getPersonDao();
courseDao = DaoFactory.getInstance().getCourseDao();
clientSocket = socket;
}

public void run()
{
try
{
String command = null;

OutputStream output = clientSocket.getOutputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

command = buffer.readLine();

while (command != null)
{
String separator = ":";

StringTokenizer tokenizer = new StringTokenizer(command, separator);

List<String> tokens = new ArrayList<>();

while (tokenizer.hasMoreElements())
{
tokens.add((String) tokenizer.nextElement());
}

int operation = Integer.parseInt(tokens.get(0));

switch (operation)
{
case 1:
try
{
Person person = personDao.findByID(Long.parseLong(tokens.get(1)));
Course course = courseDao.findByID(Long.parseLong(tokens.get(2)));

registrationDao.register(person, course);
output.write(("0\r\n").getBytes());
}
catch (Exception e)
{
e.printStackTrace();
output.write(("1\r\n").getBytes());
}
break;

case 2:
try
{
Person person = personDao.findByID(Long.parseLong(tokens.get(1)));
Course course = courseDao.findByID(Long.parseLong(tokens.get(2)));

registrationDao.register(person, course);
output.write(("0\r\n").getBytes());
}
catch (Exception e)
{
e.printStackTrace();
output.write(("1\r\n").getBytes());
}
break;

case 3:
try
{
Person person = personDao.findByID(Long.parseLong(tokens.get(1)));

List<Course> courses = registrationDao.findByPerson(person);

for (Course c : courses)
{
output.write((c.getName() + "\r\n").getBytes());
}
}
catch (Exception e)
{
e.printStackTrace();
output.write(("1\r\n").getBytes());
}
break;

}
command = buffer.readLine();
}

output.close();
}
catch (IOException e)
{
// report exception somewhere.
e.printStackTrace();
}
}
}

这是在读取返回的两个字符串而不是退出 while 循环后卡在 findRegisteredCourses() 中的代码:

    while (response != null)
{
result.add(response);
System.out.println("findRegisteredCourses():Response = " + response);
response = reader.readLine();

}

findRegisteredCourses() 的完整代码:

    @Override
public List<String> findRegisteredCourses(String personID) throws Exception
{
try (Socket server = new Socket("localhost", 7000))
{
List<String> result = new ArrayList<>();

DataOutputStream writer = new DataOutputStream(server.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream()));

String operation = "3:" + personID + "\r\n";
writer.writeBytes(operation);
writer.flush();

String response = reader.readLine();

while (response != null)
{
result.add(response);
System.out.println("findRegisteredCourses():Response = " + response);
response = reader.readLine();

}
server.close();
return result;
}
}

最佳答案

您将继续尝试从服务器读取数据,直到它关闭套接字 - 而服务器正在等待来自客户端的另一个命令。双方都不会做任何事情,因为他们都在等待对方。

基本上,你需要改变你的协议(protocol),要么有一些“这里是响应的结尾”指示(比如一个空行,如果它不是响应数据中的有效值),要么只有一个每个连接的请求/响应。

您建议的使用 ready() 方法的“修复”非常糟糕 - 这基本上意味着您假设一旦暂停就没有更多数据。可能服务器需要一段时间才能找到下一个项目。也许网络有延迟 - 或者可能已经结束。您无法辨别,并且基本上您违反了流式传输协议(protocol)(例如 TCP)的设计,因为您试图从现在没有可用数据这一事实推断信息。不要那样做 - 修正你的协议(protocol)。

关于java - 多次读取时,从 TCP 服务器的读取挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39124858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com