Java 套接字异常:java.net.SocketTimeoutException:接受超时
Java Socket Exception : java.net.SocketTimeoutException: Accept timed out
我必须在游戏中创建 IA。
为此,我必须将我连接到服务器,但我不能..
我会解释的。我的老师拿走了我的 java 项目并执行了 Launching.class。
在文件 Launching.java 和 Connection.java 中,我尝试在服务器上连接我。
当我的老师执行文件时,参数是:服务器上的server_name、port_number和地图的大小(这里不重要)
我创建了一个本地服务器并且一切正常但是当我发送我的文件并且当我的老师测试它时,他告诉我有一个错误:
Serveur : accept() du serveur pour le second joueur (n° 1) impossible ; java.net.SocketTimeoutException: Accept timed out
我的代码很简单,我想,所以我寻求帮助。
为了连接我,我使用下面的两个文件 "Launching.jaa" 和 "Connection.java":
Launching.java
public class Launching
{
private String addressIP;
private int port;
public Launching()
{
this.addressIP = "";
this.port = 0;
}
public void setAddressIP(String addressIP)
{
this.addressIP = addressIP;
}
public String getAddressIP()
{
return this.addressIP;
}
public void setPort(int port)
{
this.port = port;
}
public int getPort()
{
return this.port;
}
public static void main(String[] parameters) throws Exception
{
Launching parametersLaunching = new Launching();
parametersLaunching.addressIP = parameters[0];
parametersLaunching.port = Integer.parseInt(parameters[1]);
try
{
Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port);
connection.setInputStream(connection.getSocket());
connection.setOutputStream(connection.getSocket());
if(connection.getInputStream() != null && connection.getOutputStream() != null)
{
Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));
game.start();
}
if(connection.getInputStream() != null)
{
connection.getInputStream().close();
}
if(connection.getOutputStream() != null)
{
connection.getOutputStream().close();
}
if(connection.getSocket() != null)
{
connection.getSocket().close();
}
connection.getSocket().close();
}
catch(UnknownHostException exception)
{
exception.printStackTrace();
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
}
Connection.java
package network;
import java.io.*;
import java.net.*;
public class Connection
{
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public Connection(String adressIP, int port) throws Exception
{
InetAddress adress = InetAddress.getByName("adressIP");
this.socket = new Socket(adress, port);
this.inputStream = null;
this.outputStream = null;
}
public InputStream getInputStream()
{
return this.inputStream;
}
public OutputStream getOutputStream()
{
return this.outputStream;
}
public Socket getSocket()
{
return this.socket;
}
public void setInputStream(Socket socket) throws IOException
{
this.inputStream = socket.getInputStream();
}
public void setOutputStream(Socket socket) throws IOException
{
this.outputStream = socket.getOutputStream();
}
}
你有什么想法可以帮助我吗?我想保留这个架构..
InetAddress adress = InetAddress.getByName("adressIP");
这总是将字符串 "adressIP"
分配给地址,而不是参数 adressIP
。
如果服务器出现 accept()
超时,这只能表示您连接失败,即 IP 地址或端口错误。
但是还有很多其他问题。
Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port);
你在这里(大概)创建了一个连接到目标的客户端Socket
。
connection.setInputStream(connection.getSocket());
connection.setOutputStream(connection.getSocket());
所有这些都应该发生在 Connection
的构造函数中。
if (connection.getInputStream() != null && connection.getOutputStream() != null)
这些测试毫无意义。它们都不可能是假的。套接字总是有输入和输出流,否则会抛出异常,并且可能您的 Connection
class 不会直接将它们丢弃。不要写冗余代码。删除。
Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));
你在这里(大概)在 Connection.
周围创建了一个 Thread
game.start();
您在这里启动线程。
if(connection.getInputStream() != null)
{
connection.getInputStream().close();
}
if(connection.getOutputStream() != null)
{
connection.getOutputStream().close();
}
if(connection.getSocket() != null)
{
connection.getSocket().close();
}
此处您正在阻止 Game
运行,因为您正在关闭其 Socket
及其两个流。去掉。完成后由 Game
关闭自己的套接字。删除。
connection.getSocket().close();
此处您将第二次关闭 Socket
。删除。
NB 没有必要关闭所有这三个项目:只需要输出流就足够了。关闭任何一个流都会关闭另一个流,并且 Socket
.
我必须在游戏中创建 IA。
为此,我必须将我连接到服务器,但我不能..
我会解释的。我的老师拿走了我的 java 项目并执行了 Launching.class。 在文件 Launching.java 和 Connection.java 中,我尝试在服务器上连接我。
当我的老师执行文件时,参数是:服务器上的server_name、port_number和地图的大小(这里不重要)
我创建了一个本地服务器并且一切正常但是当我发送我的文件并且当我的老师测试它时,他告诉我有一个错误:
Serveur : accept() du serveur pour le second joueur (n° 1) impossible ; java.net.SocketTimeoutException: Accept timed out
我的代码很简单,我想,所以我寻求帮助。
为了连接我,我使用下面的两个文件 "Launching.jaa" 和 "Connection.java":
Launching.java
public class Launching
{
private String addressIP;
private int port;
public Launching()
{
this.addressIP = "";
this.port = 0;
}
public void setAddressIP(String addressIP)
{
this.addressIP = addressIP;
}
public String getAddressIP()
{
return this.addressIP;
}
public void setPort(int port)
{
this.port = port;
}
public int getPort()
{
return this.port;
}
public static void main(String[] parameters) throws Exception
{
Launching parametersLaunching = new Launching();
parametersLaunching.addressIP = parameters[0];
parametersLaunching.port = Integer.parseInt(parameters[1]);
try
{
Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port);
connection.setInputStream(connection.getSocket());
connection.setOutputStream(connection.getSocket());
if(connection.getInputStream() != null && connection.getOutputStream() != null)
{
Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));
game.start();
}
if(connection.getInputStream() != null)
{
connection.getInputStream().close();
}
if(connection.getOutputStream() != null)
{
connection.getOutputStream().close();
}
if(connection.getSocket() != null)
{
connection.getSocket().close();
}
connection.getSocket().close();
}
catch(UnknownHostException exception)
{
exception.printStackTrace();
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
}
Connection.java
package network;
import java.io.*;
import java.net.*;
public class Connection
{
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public Connection(String adressIP, int port) throws Exception
{
InetAddress adress = InetAddress.getByName("adressIP");
this.socket = new Socket(adress, port);
this.inputStream = null;
this.outputStream = null;
}
public InputStream getInputStream()
{
return this.inputStream;
}
public OutputStream getOutputStream()
{
return this.outputStream;
}
public Socket getSocket()
{
return this.socket;
}
public void setInputStream(Socket socket) throws IOException
{
this.inputStream = socket.getInputStream();
}
public void setOutputStream(Socket socket) throws IOException
{
this.outputStream = socket.getOutputStream();
}
}
你有什么想法可以帮助我吗?我想保留这个架构..
InetAddress adress = InetAddress.getByName("adressIP");
这总是将字符串 "adressIP"
分配给地址,而不是参数 adressIP
。
如果服务器出现 accept()
超时,这只能表示您连接失败,即 IP 地址或端口错误。
但是还有很多其他问题。
Connection connection = new Connection(parametersLaunching.addressIP, parametersLaunching.port);
你在这里(大概)创建了一个连接到目标的客户端Socket
。
connection.setInputStream(connection.getSocket());
connection.setOutputStream(connection.getSocket());
所有这些都应该发生在 Connection
的构造函数中。
if (connection.getInputStream() != null && connection.getOutputStream() != null)
这些测试毫无意义。它们都不可能是假的。套接字总是有输入和输出流,否则会抛出异常,并且可能您的 Connection
class 不会直接将它们丢弃。不要写冗余代码。删除。
Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));
你在这里(大概)在 Connection.
Thread
game.start();
您在这里启动线程。
if(connection.getInputStream() != null)
{
connection.getInputStream().close();
}
if(connection.getOutputStream() != null)
{
connection.getOutputStream().close();
}
if(connection.getSocket() != null)
{
connection.getSocket().close();
}
此处您正在阻止 Game
运行,因为您正在关闭其 Socket
及其两个流。去掉。完成后由 Game
关闭自己的套接字。删除。
connection.getSocket().close();
此处您将第二次关闭 Socket
。删除。
NB 没有必要关闭所有这三个项目:只需要输出流就足够了。关闭任何一个流都会关闭另一个流,并且 Socket
.