我必须在游戏上创建一个IA。
为此,我必须将我连接到服务器,但不能。
我会解释。我的老师接受我的Java项目并执行Launching.class。在文件Launching.java和Connection.java中,我尝试在服务器上进行连接。
当我的老师执行文件时,参数为:server_name,服务器上的port_number和地图大小(此处不重要)
我创建了一个本地服务器,一切都很好,但是当我发送文件并由老师进行测试时,他向我发送了一个错误消息:
服务器:从服务器接受()为第二个播放器(n°1)不可能;java.net.SocketTimeoutException:接受超时
我认为我的代码很简单,所以我寻求帮助。
为了连接我,我使用下面的两个文件“ 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();
}
}
}
连接.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
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句