Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problema with java.nio

Reply
Thread Tools

Problema with java.nio

 
 
MassimilianoPerrone@gmail.com
Guest
Posts: n/a
 
      02-05-2007
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

 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      02-05-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
MassimilianoPerrone@gmail.com
Guest
Posts: n/a
 
      02-05-2007
> 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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-06-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Esmond Pitt
Guest
Posts: n/a
 
      02-06-2007
(E-Mail Removed) 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?
 
Reply With Quote
 
MassimilianoPerrone@gmail.com
Guest
Posts: n/a
 
      02-06-2007
> 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.

 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      02-06-2007
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problema con ISDN Y CONEXION PPTP ELR Cisco 2 07-28-2005 11:18 AM
problema con vb net e system.net.socket Fabio Cirillo ASP .Net 0 03-29-2005 07:44 PM
Problema con archivos dbx Jose Joaquin de Haro ASP .Net 1 01-28-2005 01:41 PM
Problema de Configuración! Juan ASP .Net 1 03-03-2004 04:03 PM
problema complicado: RMI y no localizacion de driver de acceso a BBDD-SORRY spanish [XaToA] Java 1 02-29-2004 11:31 AM



Advertisments