gpt4 book ai didi

java - 服务器套接字总是向客户端返回空值,Java

转载 作者:可可西里 更新时间:2023-11-01 02:49:38 24 4
gpt4 key购买 nike

我在做一些计算机网络作业,我必须开发某种分布式 DBMS,它们通过对等网络相互连接,所以我在一个 .java 文件中有一个 TCP 客户端和一个 TCP 服务器,接下来运行通过线程彼此。该类的 TCP 服务器总是从其他人那里监听其他 TCP 客户端并为他们提供服务,问题是当我 System.out 我必须在服务器端发送回客户端的字符串它按照它应该的方式但是在发送之后,客户端什么也得不到并打印空值。我根据我在网上找到的教程编写了我的代码,当我测试它们时它们运行良好但它不适用于我自己的代码。你能看出我的问题在哪里吗?谢谢

class CommandComp
{
int PORT = 1210;
int PORT2 = 1211;
String IPLocal = "";
String IPdest = "";
InetAddress IPAD;
InetAddress IPAD2;
int numOfNodes;
int numOfNodesnonchanged;
String[] Nodes;
Random rand = new Random();
int max = 2000;
int min = 1000;
String command;

Socket clientSocket;

CommandComp(String[] IPdest, String IPLocal, int numOfNodes, String command)
{
try
{
this.numOfNodes = numOfNodes;
numOfNodesnonchanged = numOfNodes;
this.IPLocal = IPLocal;
this.Nodes = IPdest;
this.command = command;
// this.IPAD = InetAddress.getByName(this.IPdest);
this.IPAD2 = InetAddress.getByName(this.IPLocal);
// clientSocket = new Socket(this.IPAD , PORT ,this.IPAD2 , PORT2 );
}
catch (Exception e)
{
// //e.printStackTrace();
}
}

public String call()
{

int i = 0;
while (numOfNodes > 0)
{
String response = "";
try
{
Thread.sleep(rand.nextInt(max - min + 1) + min);
i = numOfNodes - 1;
int max2 = 50;
int min2 = 10;
this.IPAD = InetAddress.getByName(Nodes[i]);
clientSocket = new Socket(this.IPAD, PORT, this.IPAD2, PORT2 + rand.nextInt(max2 - min2 + 1) + min2);
PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
outToServer.println(command);
System.out.println(inFromServer.readLine());
response = inFromServer.readLine();
outToServer.close();
inFromServer.close();
clientSocket.close();
numOfNodes--;
System.out.println(Nodes[i] + " Remote DBMS");
System.out.println(response);

}
catch (Exception e)
{
e.printStackTrace();
try
{
clientSocket.close();
}
catch (Exception e2)
{
// TODO: handle exception
}
}
}
return command;
}
}

class TCPListnerService
implements Callable<Object>
{
String from;

String to;
ServerSocket Server;
String IP = "";
int numOfNodes;
int numofNodesUnchanged;
static clientThread t[];

TCPListnerService(String IP, int numOfNodes)
{
try
{
this.IP = IP;
this.numOfNodes = numOfNodes;
numofNodesUnchanged = numOfNodes * 2;
this.t = new clientThread[numofNodesUnchanged];
InetAddress IPAD = InetAddress.getByName(IP);
Server = new ServerSocket(1210, 20, IPAD);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public String call()
throws Exception
{

String gotten = "";
while (numOfNodes > 0)
{
Socket connected = Server.accept();

for (int i = 0; i < numofNodesUnchanged; i++)
{
if (t[i] == null)
{
(t[i] = new clientThread(connected)).start();
break;
}
}
}
return gotten;
}

}

class clientThread
extends Thread
{

Socket clientSocket = null;
sqlite DB = new sqlite();
String response = "";
String fromclient;
String delims = "[ =)(',\n\t\r]+";
String[] tokens;

public clientThread(Socket clientSocket)
{
this.clientSocket = clientSocket;
}

public void run()
{

try
{
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true);
fromclient = inFromClient.readLine();

tokens = fromclient.split(delims);
if (tokens[0].equalsIgnoreCase("create")
|| tokens[0].equalsIgnoreCase("drop")
|| tokens[0].equalsIgnoreCase("delete")
|| tokens[0].equalsIgnoreCase("insert")
|| tokens[0].equalsIgnoreCase("update")
|| tokens[0].equalsIgnoreCase("select"))
{
response = DB.RunQuery(fromclient);
System.out.println(response);
outToClient.print(response);
clientS.close();

}
else if (tokens[0].equalsIgnoreCase("shut"))
{
System.exit(0);
}

inFromClient.close();
outToClient.close();
clientSocket.close();
}
catch (Exception e)
{
}
;
}
}

最佳答案

问题出在这里:

inFromClient.close();
outToClient.close();
clientSocket.close();

您正在关闭 (1) 输入流,它关闭套接字,(2) PrintWriter,它刷新它并关闭套接字,以及 (3) 套接字。如果套接字已经关闭,显然 (2) 不会成功。您发送给客户端的数据仍在缓冲中,从未被刷新,因此它从未被发送。

只需关闭 PrintWriter,并在 finally block 中关闭套接字,以防 (2) 以某种方式失败。根本不需要关闭输入。

关于java - 服务器套接字总是向客户端返回空值,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9218157/

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