Original Post
Hi im very new to java and im trying to make a card game that consists of a server that basically just draws cards from a deck then passes the cards ID to the client which then displays the card etc. Ive got the basic interface working but the network side of it is really puzzling me ive come up with some code thats just shot together from my java book at the moment. This is what im after, Server >>>>>> Random Card Id(int 0 - 52) >>>>>>>> Client Client picks higher or lower than the last card delt. Client >>>>>> Request new cards >>>> Server then it just repeats over and over till the player quits. Why doesnt this part of the code work ? IntBuffer Out = IntBuffer.allocate(1); Out.clear(); Out.put(i); clientChannel.write(Out); says cannot resolve symbol .write it works if i use a bytebuffer though instead of an int buffer but i want to send ints between client and server ><. Having real trouble understanding how to just send anything to the client and back. How do i cast values in java ?, im used to doing (int) but that doesnt seem to work :(. The code below doesnt compile its just a starting point from what ive gathered so far. thx alot for any help.
public void run()
{
int temp = generator.nextInt(51);
int i = 0;
IntBuffer CardID;
//START UP SERVER
ServerSocketChannel Server;
try {
Server = ServerSocketChannel.open();
Server.configureBlocking(true);
InetAddress addr = InetAddress.getLocalHost();
Server.socket().bind(new InetSocketAddress(addr, 10997));
}
catch (Exception ex) {
System.out.println("error initializing server" + ex);
return;
}
System.out.println("Server Started");
//CONNECT TO PLAYER
SocketChannel clientChannel;
try{
clientChannel = Server.accept();
clientChannel.configureBlocking(true);
}
catch (Exception ex) {
System.out.println("error during accept" + ex);
return;
}
System.out.println("Connected to client");
//SEND AND RECIEVE TEST MSG
IntBuffer In = IntBuffer.allocate(1);
IntBuffer Out = IntBuffer.allocate(1);
ByteBuffer buffer = ByteBuffer.allocate(8);
Out.clear();
Out.put(i);
clientChannel.write(Out); //error here doesnt like .write
In.clear();
Server.read(In); //error here doesnt like .read
i = In.get(0);