Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Problema with java.nio

Thread view: 
MassimilianoPerrone@gmail.com - 05 Feb 2007 21:28 GMT
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 :)
Mark Space - 05 Feb 2007 23:38 GMT
> Before i illustre my problem: I'm an italian boy and i don't speak
> english very well..
[quoted text clipped - 3 lines]
> it?
> Thanks :)

Try this here, and look at the Examples:

http://java.sun.com/j2se/1.4.2/docs/guide/nio/index.html
MassimilianoPerrone@gmail.com - 05 Feb 2007 23:48 GMT
> 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.
Lew - 06 Feb 2007 06:36 GMT
> 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
Esmond Pitt - 06 Feb 2007 09:49 GMT
> 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?
MassimilianoPerrone@gmail.com - 06 Feb 2007 10:28 GMT
> 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.
Rogan Dawes - 06 Feb 2007 11:05 GMT
>> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.