Aactually i am developing Bingo card game, after distributing bingo card to each client, game begins and each client will play one by one and send a number to server and then server will send that number to all clients this is the repeatedly process,
as i said all client will send number one by one if client follow this flow then the execution of the program is as i want **i am stucked on a situation that is if client enters two numbers at a time** **then after completing all other clients chance ,when chance comes to that particular client who entered two numbers will not get chance and the second number entered by this client will be considered as his played number. i want to stop listening from server side to accept numbers if a client enters more than one number at a time.
What I have tried:
<pre>
**Multiple clients and on ne server for bingo game**
**<pre lang="java">
Server.java**
```import java.io.DataInputStream;
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;
}
}
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();
System.out.println(" >> " + "Client No:" + counter + " started!");
DataInputStream inStream = new DataInputStream(serverClient.getInputStream());
DataOutputStream outStream = new DataOutputStream(serverClient.getOutputStream());
ObjectOutputStream os = new ObjectOutputStream(serverClient.getOutputStream());
ServerClientThread sct = new ServerClientThread(serverClient, counter, inStream, outStream, os, new Server().bingoCard());
ServerClientThread.sockets.add(sct);
if (counter == 1) {
ServerClientThread.chance.add(1);
} else {
ServerClientThread.chance.add(0);
}
}
ServerClientThread.distribute();
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[][]) {
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;
}
@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));
}
}
}
}
}
}
```
**Client.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 {
InetAddress ip = InetAddress.getByName("localhost");
int flag = 1;
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();
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) {
System.out.println("closing scanner");
System.out.println("inside i==2");
System.out.println("in if condition 0");
received1 = discl.readInt();
System.out.println(received1);
}
}
} catch (IOException | ClassNotFoundException e) {
}
}
}