简体   繁体   English

Java ServerSocket,如何每次接受2个连接?

[英]Java ServerSocket, how to accept 2 connections each time?

I'm currently developing a TicTacToe multithreaded networked game for my university and I'm stuck on an annoying problem.我目前正在为我的大学开发一个井字游戏多线程网络游戏,但我遇到了一个烦人的问题。

I wish that my server accepts 2 connections each time and then waits for other 2 connections etc.我希望我的服务器每次接受 2 个连接,然后等待其他 2 个连接等。

The problem is that, in my code, if a client connects to the server and closes the connection while the second client is not yet connected to the server, the second client is not able to play because it "thinks" that the first client is connected and ready.问题是,在我的代码中,如果客户端连接到服务器并关闭连接,而第二个客户端尚未连接到服务器,则第二个客户端无法播放,因为它“认为”第一个客户端是已连接并准备就绪。

I thought to do something like this but I can't find a way to actually implement it.我认为做这样的事情这样,但我不能找到一种方法,实际上实现它。

在此处输入图片说明

The server is structured in 3 classes:服务器分为 3 个类:

  • TTT_Server (which cointains the main method and launches threads); TTT_Server(包含主要方法并启动线程);
  • TTT_ServerThread (which contains the thread(s) behaviour); TTT_ServerThread(包含线程行为);
  • TTT (which contains the TicTacToe board and some method). TTT(包含井字游戏板和一些方法)。

TTT_Server class: TTT_Server 类:

    public class TTT_Server()
    {
    private static ServerSocket serverSocket;
    private static boolean running = true;


    public static void main(String[] args)
    {
        try
        {
            // creazione ServerSocket sulla porta 8089
            serverSocket = new ServerSocket(8089);
            System.out.println("Server Socket creata e in ascolto sulla porta 8089");

            while(running)
            {
                TTT ttt = new TTT();
                Socket socket1 = serverSocket.accept();
                Socket socket2 = serverSocket.accept();
                TTT_ServerThread st1 = new TTT_ServerThread(socket1, 1, ttt, socket2);
                TTT_ServerThread st2 = new TTT_ServerThread(socket2, 2, ttt, socket1);
                st1.start();
                st2.start();
                System.out.println("thread lanciati");
            }

        }
        catch(IOException e)
        {
            System.out.println(e);
            running = false;
        }
        finally
        {
            try
            {
                serverSocket.close();
            }
            catch (IOException e)
            {
                System.out.println(e);
            }
        }
    }
}

(a part of) TTT_ServerThread class: (一部分)TTT_ServerThread 类:

private Socket socket;
private Socket socketOtherPlayer;

private BufferedReader in;
private PrintWriter out;
private PrintWriter outOtherPlayer;

private TTT TicTacToe;

private int player;
private boolean fineGioco;
private int counter = 0;

public TTT_ServerThread(Socket socket, int num_connessione, TTT ttt, Socket socketOtherPlayer)
{
    this.socket = socket;
    this.socketOtherPlayer = socketOtherPlayer;
    this.player = num_connessione;
    this.TicTacToe = ttt;
    try
    {
        this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        this.out = new PrintWriter(socket.getOutputStream());
        this.outOtherPlayer= new PrintWriter(socketOtherPlayer.getOutputStream());
    }
    catch(IOException e)
    {
        System.out.println(e);
    }
}
@Override
public void run()
{
   //[...]
}

(a part of) TTT class: (一部分) TTT类:

public class TTT
{
private int[] board = new int[9];
private int turno = 1;

public TTT()
{
    for (int i = 0; i < 9; i++)
        board[i] = 0;
}

protected synchronized void setBoard(int locazioneMossa, int giocatore)
{
    board[locazioneMossa] = giocatore;
}

protected synchronized int getBoard(int locazioneMossa)
{   
    return board[locazioneMossa];
}

protected synchronized void setTurno(int turnoRicevuto)
{
    turno = turnoRicevuto;
}

protected synchronized int getTurno()
{
    return turno;
}
//[...]
}

Is there a way to implement a sort of that diagram that i thought?有没有办法实现我认为的那种图表?

Thank you all in advance for the help!在此先感谢大家的帮助! And sorry for my English, it's not my primary language.对不起,我的英语不是我的主要语言。

Instead of trying to "accept two connections at a time", you should accept one connection at a time.与其尝试“一次接受两个连接”,不如一次接受一个连接。 If there's no waiting connection, the connection should be put to a waiting list.如果没有等待连接,则应将连接放入等待列表。 If there is a waiting connection, pair them up.如果一个等待连接,配对起来。

That way you'll have a single accept() per loop, but 2 possible ways to handle them.这样你每个循环只有一个accept() ,但有 2 种可能的方法来处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM