Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Stuck on socketChannel.read();

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Stuck on socketChannel.read();

    I have an applet that connects to a C/C++ server.

    The server sends packets but the applet can't read them for some reason.

    It gets stuck on socketChannel.read(); even though I set it to non-blocking.

                try
                {
                    socketChannel = SocketChannel.open();
                    socketChannel.configureBlocking(false);
                    socketChannel.connect(new InetSocketAddress(serverAddress, serverPort));
                }
                catch (UnknownHostException e) 
                {
                    message = "Don't know about host: " + serverAddress;
                    return;
                } 
                catch (IOException e) 
                {
                    message = "Couldn't get I/O for the connection to: " + serverAddress;
                    return;
                }
     
                try
                {
                    while(!socketChannel.finishConnect());
                }
                catch(IOException e)
                {
                    message = "Error on finishConnect();";
                    return;
                }
     
                try
                {
                    socketChannel.socket().setTcpNoDelay(true);
                }
                catch(SocketException e)
                {
                    message = "Error: " + e.toString();
                    return;
                }
     
                message = "Logging in...";
     
                int bytes;
     
     
                try 
                {
                    response.clear();
                    bytes = socketChannel.read(response); // <------- here
                }
                catch (IOException e) 
                {
                    message = "Exception: " + e.getMessage();
                    return;
                }
     
                message = "Done";


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Stuck on socketChannel.read();

    Oh, forgot to ByteBuffer.allocateDirect();

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Stuck on socketChannel.read();

    don't understand what you mean with "It gets stuck on socketChannel.read();". Exception? Hung? Garbage?

Similar Threads

  1. Replies: 7
    Last Post: July 12th, 2013, 01:43 AM
  2. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  3. Replies: 0
    Last Post: October 17th, 2011, 01:34 PM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM