程序永远不会到达某个点
Program never reaches a certain point
更新:
事实证明,我同时在我的服务器和客户端上初始化我的输入流,这就是导致问题的原因。
在我正在制作的应用程序的服务器端:我从未到达代码的一部分,这导致我的客户端在该点阻塞。最初我假设没有足够的资源来 运行 程序,所以我退出所有其他应用程序并重试。我把它放在 运行ning 一个小时了,仍然无济于事。
我怎样才能找出问题所在?我不明白它可能在哪里阻塞,也不能在那个点之前的任何地方陷入无限循环。
String[][] coordinates1 = new String[10][10], coordinates2 = new String[10][10];
public void run() {
//initializes the 10x10 grids for the player
synchronized (this) {
//outer loop is for the rows
for (int loop = 0; loop < 10; loop++) {
//the inner loop is for the columns
for (int loop2 = 0; loop2 < 10; loop2++) {
coordinates1[loop][loop2] = "~ ";
coordinates2[loop][loop2] = "~ ";
}
}
}
if (player == 1) {
//deals with the client assuming it is player1
//declares Object i/o streams
ObjectInputStream in = null;
ObjectOutputStream out = null;
try {
//initializes i/o streams
in = new ObjectInputStream(client.getInputStream());
out = new ObjectOutputStream(client.getOutputStream());
//writes the two 10x10 grids to the client
out.writeObject(coordinates2);
out.writeObject(coordinates1);
out.flush();
...
}
它从未到达的部分是 "out.writeObject(..);" 行。
可以找到完整代码 here,以及客户端。
没有阻塞,您的程序正在等待。
您永远不会到达 writeObject 行,因为前面的两行(getInputStream 和 getOutputStream)将 "wait" 为套接字建立有效连接。您可以通过在其周围放置两条日志行来验证这一点:
in = new ObjectInputStream(client.getInputStream());
要解决此问题,我建议您检查建立连接的代码(ServerSockets 和套接字)。
更新: 事实证明,我同时在我的服务器和客户端上初始化我的输入流,这就是导致问题的原因。
在我正在制作的应用程序的服务器端:我从未到达代码的一部分,这导致我的客户端在该点阻塞。最初我假设没有足够的资源来 运行 程序,所以我退出所有其他应用程序并重试。我把它放在 运行ning 一个小时了,仍然无济于事。
我怎样才能找出问题所在?我不明白它可能在哪里阻塞,也不能在那个点之前的任何地方陷入无限循环。
String[][] coordinates1 = new String[10][10], coordinates2 = new String[10][10];
public void run() {
//initializes the 10x10 grids for the player
synchronized (this) {
//outer loop is for the rows
for (int loop = 0; loop < 10; loop++) {
//the inner loop is for the columns
for (int loop2 = 0; loop2 < 10; loop2++) {
coordinates1[loop][loop2] = "~ ";
coordinates2[loop][loop2] = "~ ";
}
}
}
if (player == 1) {
//deals with the client assuming it is player1
//declares Object i/o streams
ObjectInputStream in = null;
ObjectOutputStream out = null;
try {
//initializes i/o streams
in = new ObjectInputStream(client.getInputStream());
out = new ObjectOutputStream(client.getOutputStream());
//writes the two 10x10 grids to the client
out.writeObject(coordinates2);
out.writeObject(coordinates1);
out.flush();
...
}
它从未到达的部分是 "out.writeObject(..);" 行。
可以找到完整代码 here,以及客户端。
没有阻塞,您的程序正在等待。
您永远不会到达 writeObject 行,因为前面的两行(getInputStream 和 getOutputStream)将 "wait" 为套接字建立有效连接。您可以通过在其周围放置两条日志行来验证这一点:
in = new ObjectInputStream(client.getInputStream());
要解决此问题,我建议您检查建立连接的代码(ServerSockets 和套接字)。