首页 > 解决方案 > 如果在 Java 中的套接字编程中没有机会,如何给所有客户端一个一个发送数字并阻止客户端发送数字的机会

问题描述

用于宾果游戏的多个客户端和一台服务器

服务器.java

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;
import java.util.ArrayList;

public class Server {

    int[][] card = new int[5][5];
    int[][] bingo = new int[5][5];

    public int[][] bingoCard() {

        ArrayList<Integer> alreadyUsed = new ArrayList<>();
        boolean valid = false;
        int tmp = 0;

        for (int i = 0; i <= 4; i++) {
            for (int row = 0; row < card.length; row++) {
                while (!valid) {
                    tmp = (int) (Math.random() * 25) + 1;
                    if (!alreadyUsed.contains(tmp)) {
                        valid = true;
                        alreadyUsed.add(tmp);
                    }
                }
                card[row][i] = tmp;
                bingo[row][i] = tmp;
                valid = false;
            }
        }

        //create array to make title.  
        String title[] = {"B", "I", "N", "G", "O"};

        for (int i = 0; i < title.length; i++) {
            System.out.print(title[i] + "\t");
        }

        System.out.println();

        for (int row = 0; row < card.length; row++) {
            for (int col = 0; col < card[row].length; col++) {
                System.out.print(card[row][col] + "\t");

            }
            System.out.println();

        }
        return bingo;

    }

    public static void main(String args[]) {
        try {

            ServerSocket serversct = new ServerSocket(1000);
            int counter = 0;
            System.out.println("Server Started ....");
            while (counter <= 2) {
                counter++;
                Socket serverClient = serversct.accept();  //server accept the client connection request
                System.out.println(" >> " + "Client No:" + counter + " started!");
                DataInputStream inStream = new DataInputStream(serverClient.getInputStream());
                DataOutputStream outStream = new DataOutputStream(serverClient.getOutputStream());
                //bingo=Server.bingoCard();
                ObjectOutputStream os = new ObjectOutputStream(serverClient.getOutputStream());

                ServerClientThread sct = new ServerClientThread(serverClient, counter, inStream, outStream, os, new Server().bingoCard()); //send  the request to a separate thread

                ServerClientThread.sockets.add(sct);
                if (counter == 1) {
                    ServerClientThread.chance.add(1);
                } else {
                    ServerClientThread.chance.add(0);
                }

            }


            ServerClientThread.distribute();

            //ServerClientThread.sockets.get(1).start();
            ServerClientThread.transferring();

            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

class ServerClientThread extends Thread {

    Socket serverClient;
    int clientNo;
    DataInputStream inStream;
    DataOutputStream outStream;
    ObjectOutputStream oos;
    static ArrayList<ServerClientThread> sockets = new ArrayList<>();
    static ArrayList<Integer> chance = new ArrayList<>();

    static ArrayList<ObjectOutputStream> oosal = new ArrayList<>();
    static ArrayList<DataInputStream> datainputal = new ArrayList<>();
    static ArrayList<DataOutputStream> dataoutputal = new ArrayList<>();
    static int received;
    int[][] card = new int[5][5];
    int[][] bingo = new int[5][5];

    ServerClientThread(Socket inSocket, int counter, DataInputStream dis, DataOutputStream dos, ObjectOutputStream oos, int abc[][]) {//,int abc[][]) {
        this.serverClient = inSocket;
        this.clientNo = counter;

        this.oos = oos;
        oosal.add(oos);

        this.inStream = dis;
        this.outStream = dos;
        ServerClientThread.datainputal.add(dis);
        ServerClientThread.dataoutputal.add(dos);
        this.bingo = abc;
    }
//i will use this run method later for checking winner of bingo card
    @Override
    public void run() {
        try {


            System.out.println("thread is running");


        } catch (Exception ex) {
            System.out.println(ex);
        } finally {
            System.out.println("Client :" + clientNo + " exit!! ");
        }
    }

    public static void distribute() throws Exception {
        System.out.println("distributing");

        for (int i = 0; i <= ServerClientThread.sockets.size() - 1; i++) {
            oosal.get(i).writeObject(ServerClientThread.sockets.get(i).bingo);
            System.out.println("i is +" + i);
        }

    }

    public static synchronized void transferring() throws Exception {



        while (true) {
                int chance = 1;
            int received = 10000;
            for (int i = 0; i <= ServerClientThread.datainputal.size() - 1; i++) {
                
                ServerClientThread.dataoutputal.get(i).writeInt(chance);
                ServerClientThread.dataoutputal.get(i).flush();
                
                for (int l = 0; l <= ServerClientThread.chance.size() - 1; l++) {

                    
                    if (ServerClientThread.chance.get(l) == 1)
                {
                    received = ServerClientThread.datainputal.get(i).readInt();
                }
                    if (l == i + 1)
                    {
                        ServerClientThread.chance.set(l, 1);
                    } 
                    else if (i == ServerClientThread.chance.size() - 1) {
                        ServerClientThread.chance.set(l, 0);
                        ServerClientThread.chance.set(0, 1);
                    } else {
                        ServerClientThread.chance.set(l, 0);
                    }


                }

                
                for (int n = 0; n< ServerClientThread.datainputal.size(); n++)
                {
                    for (int j = 0; j< ServerClientThread.datainputal.size(); j++)
                if(ServerClientThread.chance.get(n)==1 && ServerClientThread.datainputal.get(j).available()>0)
                {
                        received=ServerClientThread.datainputal.get(j).read();
                }}
                
               System.out.println("this is received" + received);
                

                chance = 0;
                for (int j = 0; j <= ServerClientThread.dataoutputal.size() - 1; j++) {

                    ServerClientThread.dataoutputal.get(j).writeInt(chance);
                    
                    ServerClientThread.dataoutputal.get(j).writeInt(received);
                    
                }

                chance = 1;
                for (int k = 0; k <= ServerClientThread.chance.size() - 1; k++) {
                    System.out.println("here is given chance" + ServerClientThread.chance.get(k));
                }
            }

        }
    }



    }

}

客户端.java

import java.io.*;
import java.net.*;
import java.util.Scanner;

class Client {

    static String completed[] = new String[5];

    static int tosend;
    static int received1;

    public static void main(String args[]) throws Exception {

        try {

            // Scanner scn = new Scanner(System.in);
            // getting localhost ip 
            InetAddress ip = InetAddress.getByName("localhost");
            int flag = 1;
            // establish the connection with server port 9999 
            Socket s = new Socket(ip, 1000);
            DataInputStream discl = new DataInputStream(s.getInputStream());
            DataOutputStream doscl = new DataOutputStream(s.getOutputStream());

            ObjectInputStream is = new ObjectInputStream(s.getInputStream());
            int[][] array = (int[][]) is.readObject();
        
            //create array to make title.  
            String title[] = {"B", "I", "N", "G", "O"};

            for (int i = 0; i < title.length; i++) {
                System.out.print(title[i] + "\t");
            }

            System.out.println();

            for (int row = 0; row < array.length; row++) {
                for (int col = 0; col < array[row].length; col++) {
                    System.out.print(array[row][col] + "\t");

                }
                System.out.println();

            }
        
            while (true) {

                int i = discl.readInt();
                    
                if (i == 1 ) 
                {
                       
                    System.out.println("inside i==1");
                    System.out.println("in if condition 1");
                    System.out.println("opening  scanner");
                   
                    tosend = new Scanner(System.in).nextInt();
                    
                    
                    doscl.writeInt(tosend);
 
                } else if (i == 0) {

                    //sendComplete();
                    System.out.println("closing scanner");

                    System.out.println("inside i==2");
                    //completed = Bingo;
                    System.out.println("in if condition 0");

                    received1 = discl.readInt();
                    System.out.println(received1);


                }

            }

        } catch (IOException | ClassNotFoundException e) {
        }

    }

}

Aactually 我正在开发宾果纸牌游戏,在将宾果卡分发给每个客户端后,游戏开始,每个客户端将一个接一个地玩并向服务器发送一个数字,然后服务器将该数字发送给所有客户端这是重复的过程,因为我说如果客户遵循此流程,所有客户将一一发送,然后程序的执行就像我想要的那样当机会出现时,输入两个号码的特定客户将没有机会,该客户输入的第二个号码将被视为他的投注号码。如果客户端一次输入多个数字,我想停止从服务器端收听以接受数字。

标签: javaarraysmultithreadingsocketsarraylist

解决方案


推荐阅读