Problema with java.nio

Discussion in 'Java' started by MassimilianoPerrone@gmail.com, Feb 5, 2007.

  1. Guest

    Before i illustre my problem: I'm an italian boy and i don't speak
    english very well..
    So...
    I've a server NIO and a client NON nio.
    How can send a msg from/to client server? what classes i can use to do
    it?
    Thanks :)
    , Feb 5, 2007
    #1
    1. Advertising

  2. Mark Space Guest

    wrote:
    > Before i illustre my problem: I'm an italian boy and i don't speak
    > english very well..
    > So...
    > I've a server NIO and a client NON nio.
    > How can send a msg from/to client server? what classes i can use to do
    > it?
    > Thanks :)
    >


    Try this here, and look at the Examples:

    http://java.sun.com/j2se/1.4.2/docs/guide/nio/index.html
    Mark Space, Feb 5, 2007
    #2
    1. Advertising

  3. Guest

    > Try this here, and look at the Examples:
    >
    > http://java.sun.com/j2se/1.4.2/docs/guide/nio/index.html


    import java.net.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.charset.*;

    public class Server {

    /** sempre da assisini */
    public static void main(String[] args) throws Throwable {
    ServerSocket server = new ServerSocket(9999);
    Socket client = server.accept();
    ReadableByteChannel channel =
    Channels.newChannel(client.getInputStream());
    ByteBuffer len = ByteBuffer.allocate(4); //4 byte = 1 int
    channel.read(len);
    len.flip();
    ByteBuffer message =
    ByteBuffer.allocateDirect(len.asIntBuffer().
    while(message.hasRemaining()) {
    channel.read(message);
    }
    message.flip();
    Charset ch = Charset.forName("ISO-8859-1");
    channel.close();
    server.close();
    }
    }

    package client;

    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io_OutputStreamWriter;
    import java.net.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.nio.charset.*;

    public class Client {
    public static void main(String[] args) throws Throwable {
    Socket socket = new Socket("localhost", 9999);
    DataOutputStream os = new
    DataOutputStream(socket.getOutputStream());
    String messaggio = "ciao";
    os.writeBytes(messaggio);
    os.flush();
    os.close();
    socket.close();
    }
    }

    Exception:
    Exception in thread "main" java.nio.BufferUnderflowException
    at java.nio.Buffer.nextGetIndex(Unknown Source)
    at java.nio.ByteBufferAsIntBufferB.get(Unknown Source)
    at server.Server.main(Server.java:26)


    I look the examples at the java guide but there isn't an example that
    explain like a client io can send a msg to server NIO.
    , Feb 5, 2007
    #3
  4. Lew Guest

    wrote:
    > I look the examples at the java guide but there isn't an example that
    > explain like a client io can send a msg to server NIO.


    The client can use different I/O libraries than the server. The client doesn't
    even have to be written in the same language as the server. It simply has to
    send and receive the right byte seuqences as expected at the other end.

    - Lew
    Lew, Feb 6, 2007
    #4
  5. Esmond Pitt Guest

    wrote:
    > I've a server NIO and a client NON nio.
    > How can send a msg from/to client server? what classes i can use to do
    > it?


    So why do you have to write your server in NIO when you don't know how
    to do it?
    Esmond Pitt, Feb 6, 2007
    #5
  6. Guest

    > So why do you have to write your server in NIO when you don't know how
    > to do it?


    Because this is an example for understanding the comunication. The my
    real problem is that i've a framework implemented with NIO and i must
    implementing a client J2ME that send msg to it.
    , Feb 6, 2007
    #6
  7. Rogan Dawes Guest

    wrote:
    >> So why do you have to write your server in NIO when you don't know how
    >> to do it?

    >
    > Because this is an example for understanding the comunication. The my
    > real problem is that i've a framework implemented with NIO and i must
    > implementing a client J2ME that send msg to it.
    >


    It makes absolutely no difference to your client how the server is
    implemented.

    As far as the client is concerned, all it needs to do is speak TCP/IP
    (or UDP/IP), using whatever programming model/API it chooses.

    On the server side, one might choose to use NIO for the performance
    efficiencies possible by avoiding large numbers of threads. This is
    unlikely to be a factor in a J2ME app.

    Rogan
    Rogan Dawes, Feb 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juan

    Problema de ConfiguraciĆ³n!

    Juan, Mar 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,496
    Carlos Campos
    Mar 3, 2004
  2. Jose Joaquin de Haro

    Problema con archivos dbx

    Jose Joaquin de Haro, Jan 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,569
    Lionel LASKE
    Jan 28, 2005
  3. Fabio Cirillo

    problema con vb net e system.net.socket

    Fabio Cirillo, Mar 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    649
    Fabio Cirillo
    Mar 29, 2005
  4. [XaToA]
    Replies:
    1
    Views:
    2,436
    TT \(Tom Tempelaere\)
    Feb 29, 2004
  5. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,248
    iksrazal
    Jun 18, 2004
Loading...

Share This Page