Reputation: 13
Was writing a server for my X O game. The basic idea was that I have one computer run as a server wit ha static ip
and others could connect to it.
It needs to be able to send the state of the game to all the connected clients, but I ran into a blocking problem.
If you take a look at the client side
Scanner input = new Scanner();
int test = input.nextInt();
is blocking the code.
The server is able to send the message to all the clients but it is being prevented by the nextInt() as it waits for the Int to be passed in order to echo the state of the game(jsut a set of ints, coordinates).
Could anyone help me out here, I am really stuck, have been reading up on blocking problems, on the net and here on the forums I found that some people were saying that I have to check the inputStream on the client side whether it contains something or not but I could not implement it.
Any idea, suggestions are welcomed.
Thank you all for your time.
-------SERVER-----------
import java.net.*;
import java.io.*;
public class Server extends SerResponse {
public static void main(String[] args) {
int port = 1234;
int cliNum = 1;
try {
ServerSocket sock = new ServerSocket(port);
System.out.println("SERVER WORKING......");
int index0 = 0;
while (true) {
SerResponse ser = new SerResponse();
Socket connection = sock.accept();
soc[index0] = connection;
index0++;
if (connection != null) {
System.out.println("Client " + cliNum + " connected " + connection);
cliNum++;
}
Runnable runnable = new Server(connection, ++ser.count);
Thread t = new Thread(runnable);
t.start();
}
} catch (Exception e) {
System.out.println("SERVER FAIL " + e);
}
}
public Server(Socket s, int count) {
this.connection = s;
this.ID = count;
}
}
------SERVER RESPONSE--------------
import java.io.*;
import java.net.Socket;
public class SerResponse implements Runnable {
public static Socket connection;
public int ID;
public final static int size = 10;
public static Socket[] soc = new Socket[size];
public int count = 0;
public int test;
public void run() {
try {
while (true) {
DataInputStream in = new DataInputStream(connection.getInputStream());
test = in.readInt();
System.out.println("Recieved from " + connection + " " + test + "\n");
for (int index0 = 0; index0 < size; index0++) {
if (soc[index0] != null) {
DataOutputStream out = new DataOutputStream(soc[index0].getOutputStream());
out.writeInt(test);
System.out.println("Sent to client " + soc[index0] + " " + test);
}
}
}
} catch (Exception e) {
System.out.println("Runnable FAIL " + e);
}
}
}
-----CLIENT--------
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class client {
public static void main(String[] args) {
int[][] arr = {{3, 4}, {6, 2}, {9, 2}};
int[][] arr1 = new int[3][2];
int port = 1234;
int test;
int test1;
try {
System.out.println("Starting client");
Socket sock = new Socket("localhost", port);
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
DataInputStream in = new DataInputStream(sock.getInputStream());
Scanner input = new Scanner(System.in);
int turn = 0;
while (true) {
test = input.nextInt();
out.writeInt(test);
System.out.println(test);
test1 = in.readInt();
System.out.println(test1);
turn++;
}
} catch (Exception e) {
System.out.println("CLIENT FAIL " + e);
}
}
}
Upvotes: 0
Views: 149
Reputation: 183
Here's a rework of your client class that should give the asynchronous behaviour that you're looking for.
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Scanner;
public class Client extends Thread {
private final Socket sock;
private boolean shutdown = false;
public Client(Socket sock) {
this.sock = sock;
}
public void shutdown() {
shutdown = true;
}
public void writeDataToServer(int test) {
try {
synchronized(Client.class) {
//don't allow reading and writing at the same time.
DataOutputStream out = new DataOutputStream(sock.getOutputStream());
out.writeInt(test);
sock.getOutputStream().flush();
}
}catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
BufferedInputStream bis = new BufferedInputStream(sock.getInputStream());
while(!shutdown) {
if(bis.available() > 0) {
synchronized(Client.class) {
DataInputStream in = new DataInputStream(bis);
int test = in.readInt();
//you can do something with test hereafter.... but since the example just had it printing out
System.out.println(test);
}
} else {
Thread.sleep(500); //sleep for 500ms before polling again.
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
sock.close();
} catch(Exception ignored){}
}
}
public static void main(String[] args) {
int[][] arr = {{3, 4}, {6, 2}, {9, 2}};
int[][] arr1 = new int[3][2];
int port = 1234;
int test;
try {
System.out.println("Starting client");
Client client = new Client(new Socket("localhost", port));
client.start();
Scanner input = new Scanner(System.in);
int turn = 0;
while(turn < 10) {
test = input.nextInt();
client.writeDataToServer(test);
System.out.println(test);
turn++;
}
client.shutdown();
} catch(Exception e) {
System.out.println("CLIENT FAIL " + e);
}
}
}
Upvotes: 1