Problema with java.nio

  • Thread starter MassimilianoPerrone
  • Start date
M

MassimilianoPerrone

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 :)
 
M

MassimilianoPerrone

Try this here, and look at the Examples:

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.
 
L

Lew

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
 
E

Esmond Pitt

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?
 
M

MassimilianoPerrone

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.
 
R

Rogan Dawes

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top