   [java] Server client reading/sending questions..
  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) >>>>>>>> ClientClient 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.    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); thx alot for any help.   
  1. You have to flip() before writing!Quote:Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.2. SocketChannel.write does require ByteBuffer. You should use it as it is the most flexible and it doesnt really have any negative sides.Don't want to do any unwanted recruitment but over at javagaming.org you would probably find more help.   ; Out.clear();    Out.putInt(i);    Out.flip();clientChannel.write(Out); <<< errror hereIn.clear();    clientChannel.read(In); <<< error herei = In.get(); Im using bytebuffers now instead of int buffers.unreported exception java.io.IOException; must be caught or declared to be thrown    clientChannel.write(Out)Anyone have any idea how i get around this ?   ;  Please read the compiler erros!It tells you that clientChannel.write and clientChannel.read both throw exceptions and must be cought.Put them into try-catches.;  Sorry i did say i was new but yeah your right didnt even realise what the try and catch did to be honest but with a good old read up i figured it out.Thanks for your help bro works fine now.Maybe some help for people who bump into this thread i used 2 bytebuffers one for reading and one for writing. I used these functions to write and read putInt() putChar() getChar() getInt() etc.thx againfishy   ;  NP! Just trying to pass the help I once recieved.Maybe you could post your code here so others might see the results?Are you working twards a specific goal or just dabbling around?What kind of IDE are you using? Eclipse, Netbeans or other?If not, then I suggest you do so as they would warn you about any uncaught exceptions.