Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Problema with java.nio (http://www.velocityreviews.com/forums/t390638-problema-with-java-nio.html)

MassimilianoPerrone@gmail.com 02-05-2007 09:28 PM

Problema with java.nio
 
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 02-05-2007 11:41 PM

Re: Problema with java.nio
 
MassimilianoPerrone@gmail.com 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/...nio/index.html


MassimilianoPerrone@gmail.com 02-05-2007 11:48 PM

Re: Problema with java.nio
 
> Try this here, and look at the Examples:
>
> http://java.sun.com/j2se/1.4.2/docs/...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 02-06-2007 06:36 AM

Re: Problema with java.nio
 
MassimilianoPerrone@gmail.com 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

Esmond Pitt 02-06-2007 09:49 AM

Re: Problema with java.nio
 
MassimilianoPerrone@gmail.com 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?

MassimilianoPerrone@gmail.com 02-06-2007 10:28 AM

Re: Problema with java.nio
 
> 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 02-06-2007 11:05 AM

Re: Problema with java.nio
 
MassimilianoPerrone@gmail.com 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


All times are GMT. The time now is 07:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.