簡體   English   中英

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和map的大小(這里不重要)

我創建了一個本地服務器,一切都很好,但是當我發送我的文件並且當我的老師測試它時,他告訴我有一個錯誤:

Serveur : accept() du serveur pour le second joueur (n° 1) 不可能; java.net.SocketTimeoutException:接受超時

我想我的代碼很簡單,所以我尋求幫助。

為了連接我,我使用下面的兩個文件“Launching.jaa”和“Connection.java”:

啟動.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" ,而不是參數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類不會把它們扔掉。 不要寫多余的代碼。 消除。

 Game game = new Game(connection.getInputStream(), connection.getOutputStream(), Integer.parseInt(parameters[2]));

在這里,您(大概)正在圍繞Connection.創建一個Thread Connection.

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM