Java NIO question...
Hey, I've just start reading and programming a client/server using NIO and got to a point where i need to keep a list of connected clients to send them messages as the server processes information it received... How could i handle this selection of clients that i want to send a specific message and how should I store those clients after they connect (I mean, when a client connects, a channel is started, but how can i identify later that a channel is related to THAT client?)
Thanks in advance
Message was edited by:
Lemmerich
Generally you will associate some kind of client session object with the channel via the attachment. This will also contain the input buffer and whatever you need in your application to identify the client.
Similar Messages
-
Java.nio read/write question
Hello,
I just started to learn the java.nio package so I decided to make a simple Echo server. I made a client which reads a line from the keyboard, sends it to the server and the server returns it. It all works except one little detail. Here's little code from the server:
int n = client.read(buffer);
if ( n > 0)
buffer.flip();
client.write(buffer);
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
charBuffer = decoder.decode(buffer);
System.out.println(charBuffer.toString());
buffer.clear();
}So that works, I send the data and then I receive it back. But only for the client. I also wanted the server to print the line which is the reason for the charset and the decoder. The above code however prints only a blank line. Thus I tried this:
int n = client.read(buffer);
if ( n > 0)
buffer.flip();
Charset charset = Charset.forName("ISO-8859-1");
CharsetDecoder decoder = charset.newDecoder();
charBuffer = decoder.decode(buffer);
System.out.println(charBuffer.toString());
client.write(buffer);
buffer.clear();
}Or in other words I just moved the write() part downwards. So far so good, now the server was actually printing the lines that the client was sending but nothing was sent back to the client.
The question is how to make both, the send back line and the print line on the server side to work as intended. Also a little explanation why the events described above are happening is going to be more than welcome :)
Thanks in advance!Strike notice
A number of the regular posters here are striking in protest at the poor
management of these forums. Although it is our unpaid efforts which make the
forums function, the Sun employees responsible for them seem to regard us as
contemptible. We hope that this strike will enable them to see the value
which we provide to Sun. Apologies to unsuspecting innocents caught up in
the cross-fire. -
Java.nio select() method return 0 in my client application
Hello,
I'm developing a simple chat application who echo messages
But my client application loop because the select() method return 0
This is my code
// SERVER
package test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class Server {
private int port = 5001;
public void work() {
try {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
InetSocketAddress isa = new InetSocketAddress(port);
serverSocketChannel.socket().bind(isa);
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Listing on "+port);
while(selector.select()>0) {
Set keys = selector.selectedKeys();
for(Iterator i = keys.iterator(); i.hasNext();) {
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isAcceptable()) {
ServerSocketChannel keyChannel = (ServerSocketChannel)key.channel();
SocketChannel channel = keyChannel.accept();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ );
} else if (key.isReadable()) {
SocketChannel keyChannel = (SocketChannel) key.channel();
String m = Help.read(keyChannel );
Help.write(m.toUpperCase(), keyChannel );
} catch (IOException e) {
e.printStackTrace();
public static void main(String[] args) {
Server s = new Server();
s.work();
// CLIENT
package test;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Client extends JFrame {
private String host = "localhost";
private int port = 5001;
private SocketChannel socketChannel;
private Selector selector;
public void work() {
try {
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
InetSocketAddress isa = new InetSocketAddress(host, port);
socketChannel.connect(isa);
selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ );
while(true) {
selector.select();
Set keys = selector.selectedKeys();
for(Iterator i = keys.iterator(); i.hasNext();) {
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isConnectable()) {
SocketChannel keyChannel = (SocketChannel) key.channel();
if (keyChannel.isConnectionPending()) {
System.out.println("Connected "+keyChannel.finishConnect());
} else if (key.isReadable()) {
SocketChannel keyChannel = (SocketChannel) key.channel();
String m = Help.read(keyChannel);
display(m);
} catch (IOException e) {
e.printStackTrace();
private void display(final String m) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
area.append(m+"\n");
textFieed.setText("");
private void sendMessage(final String m) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
Help.write(m, socketChannel);
} catch (IOException e) {
e.printStackTrace();
t.start();
public Client() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
textFieed.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()== KeyEvent.VK_ENTER) {
String m = textFieed.getText();
sendMessage(m);
area.setEditable(false);
getContentPane().add(textFieed, "North");
getContentPane().add(new JScrollPane(area));
setBounds(200, 200, 400, 300);
show();
private String messageToSend;
private JTextArea area = new JTextArea();
JTextField textFieed = new JTextField();
public static void main(String[] args) {
Client s = new Client();
s.work();
// HELPER CLASS
package test;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
public class Help {
private static Charset charset = Charset.forName("us-ascii");
private static CharsetEncoder enc = charset.newEncoder();
private static CharsetDecoder dec = charset.newDecoder();
private static void log(String m) {
System.out.println(m);
public static String read(SocketChannel channel) throws IOException {
log("*** start READ");
int n;
ByteBuffer buffer = ByteBuffer.allocate(1024);
while((n = channel.read(buffer)) > 0) {
System.out.println(" adding "+n+" bytes");
log(" BUFFER REMPLI : "+buffer);
buffer.flip();
CharBuffer cb = dec.decode(buffer);
log(" CHARBUFFER : "+cb);
String m = cb.toString();
log(" MESSAGE : "+m);
log("*** end READ");
//buffer.clear();
return m;
public static void write(String m, SocketChannel channel) throws IOException {
log("xxx start WRITE");
CharBuffer cb = CharBuffer.wrap(m);
log(" CHARBUFFER : "+cb);
ByteBuffer buffer = enc.encode(cb);
log(" BUFFER ALLOUE REMPLI : "+buffer);
int n;
while(buffer.hasRemaining()) {
n = channel.write(buffer);
System.out.println(" REMAINING : "+buffer.hasRemaining());
log("xxx end WRITE");Here's the fix for that old problem. Change the work method to do the following
- don't register interest in things that can't happen
- when you connect register based on whether the connection is complete or pending.
- add the OP_READ interest once the connection is complete.
This doesn't fix all the other problems this code will have,
eg.
- what happens if a write is incomplete?
- why does my code loop if I add OP_WRITE interest?
- why does my interestOps or register method block?
For code that answers all those questions see my obese post Taming the NIO Circus
Here's the fixed up Client code
// CLIENT
package test
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Client extends JFrame {
private String host = "localhost";
private int port = 5001;
private SocketChannel socketChannel;
private Selector selector;
public void work() {
try {
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
InetSocketAddress isa = new InetSocketAddress(host, port);
socketChannel.connect(isa);
selector = Selector.open();
int interest = 0;
if(socketChannel.isConnected())interest = SelectionKey.OP_READ;
else if(socketChannel.isConnectionPending())interest = SelectionKey.OP_CONNECT;
socketChannel.register(selector, interest);
while(true)
int nn = selector.select();
System.out.println("nn="+nn);
Set keys = selector.selectedKeys();
for(Iterator i = keys.iterator(); i.hasNext();)
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isConnectable())
SocketChannel keyChannel = (SocketChannel) key.channel();
System.out.println("Connected "+keyChannel.finishConnect());
key.interestOps(SelectionKey.OP_READ);
if (key.isReadable())
SocketChannel keyChannel = (SocketChannel) key.channel();
String m = Help.read(keyChannel);
display(m);
} catch (IOException e) {
e.printStackTrace();
private void display(final String m) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
area.append(m+"\n");
textFieed.setText("");
private void sendMessage(final String m) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
Help.write(m, socketChannel);
} catch (IOException e) {
e.printStackTrace();
t.start();
public Client() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(1);
textFieed.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()== KeyEvent.VK_ENTER) {
String m = textFieed.getText();
sendMessage(m);
area.setEditable(false);
getContentPane().add(textFieed, "North");
getContentPane().add(new JScrollPane(area));
setBounds(200, 200, 400, 300);
show();
private String messageToSend;
private JTextArea area = new JTextArea();
JTextField textFieed = new JTextField();
public static void main(String[] args) {
Client s = new Client();
s.work(); -
Converting from CP1252 (Windows) to ISO 8859-1 doesn't work with java.nio?
Hi
I'm trying to write some code that checks whether an InputStream contains only characters with a given encoding. I'm using java.nio for that. For tests, I downloaded some character set examples from http://www.columbia.edu/kermit/csettables.html
When creating the CharsetDecoder, I want to get all errors:
Charset charset = Charset.forName( encoding );
CharsetDecoder decoder = charset.newDecoder();
decoder.onMalformedInput( CodingErrorAction.REPORT );
decoder.onUnmappableCharacter( CodingErrorAction.REPORT );I then read an InputStream and try to convert it. If that fails, it can't contain the desired encoding:
boolean isWellEncoded = true;
ByteBuffer inBuffer = ByteBuffer.allocate( 1024 );
ReadableByteChannel channel = Channels.newChannel( inputStream );
while ( channel.read( inBuffer ) != -1 )
CharBuffer decoded = null;
try
inBuffer.flip();
decoded = decoder.decode( inBuffer );
catch ( MalformedInputException ex )
isWellEncoded = false;
catch ( UnmappableCharacterException ex )
isWellEncoded = false;
catch ( CharacterCodingException ex )
isWellEncoded = false;
if ( decoded != null )
LOG.debug( decoded.toString() );
if ( !isWellEncoded )
break;
inBuffer.compact();
channel.close();
return isWellEncoded;Now I want to check whether a file containing Windows 1252 characters is ISO-8859-1. From my point of view, the code above should fail when it gets to the Euro symbol (decimal 128), since that's not defined in ISO-8859-1.
But all I get is a ? character instead:
(}) 125 07/13 175 7D RIGHT CURLY BRACKET, RIGHT BRACE
(~) 126 07/14 176 7E TILDE
[?] 128 08/00 200 80 EURO SYMBOL
[?] 130 08/02 202 82 LOW 9 SINGLE QUOTEI also tried to replace the faulty character, using
decoder.onUnmappableCharacter( CodingErrorAction.REPLACE );
decoder.replaceWith("!");but I still get the question marks.
I'm probably doing something fundamentally wrong, but I dont get it :-)
Any help is greatly appreciated!
EricAs a suggestion....create a complete example demonstrating the problem. It shouldn't have channel in it since that wouldn't appear to be the problem (decoding is.) You should create the byte array in the example code - populate it with the byte sequence that you think should work. And your code should then demonstrate that it doesn't. Then post that.
-
Cannot access class java.nio.ByteBuffer
Hi All -
I'm trying to compile a java sample code.
I keep getting the error described in the subject line
cannot access class java.nio.ByteBuffer; file java\nio\ByteBuffer.class not found
I have un-ziped the java sdk 1.4.1 src.zip to a directory under c:\ and added the java\nio directory to the project setting and compile with jdk runtime 1.4.1 and yet get the same error....
I've tried by redirecting to src.zip and didn't work either....
I would appreciate your feedbacks and sorry for the lame question, consider it as a newcomer to Java world.
thanks in advanceYou test it by running the following line....
java java.nio.ByteBuffer
If it says "main not found" then your problem has nothing to do with the java install nor the classpath. The code you are trying to compile is wrong.
If it says class not found then you use this line next....
java -version
If this returns nothing then you are not using the Sun VM (you are using the MS one.) If it returns a version below 1.4 then your PATH statement is wrong (and you should uninstall all sun versions then reinstall.) You can fix the MS VM problem by altering the path so the java path is first.
If it does say 1.4 then you need to uninstall and reinstall because something is messed up. -
The application in question uses JNI for legacy integration and I suspect the legacy code is corrupting the stack causing the above error. However, the error does not occur in Java 1.3, only Java 1.4.
Is there some way to suppress 1.4's use of the native IO API when encoding and decoding byte streams? This would at least provide a workaround in the meantime.
Thanks.This is beginning to make a little sense. The problem is that you got a String and you don't want one. A String wraps an array of chars, which your app needs, right? Specifically they're chars because you need 16-bit char sets.
Presumably the getBytes() method call is used to get an array of bytes for some data transfer operation. java.nio was probably added in 1.4 as it has some very efficient ways of handling buffers as simultaneously of two or more types. It's trying to use the underlying char array as a byte array and there's a straight up bug someplace.
Workaround is strange to contemplate, but I'm pretty sure it will work: use String.getChars() to get an array of chars, and then use java.nio yourself to create your byte array! If you've never been there, it's not very hard. I use nio all the time and it's never been a problem. -
Please, need help.
I'm using java.nio to get large files contents (txt) and need to put these data into MSSQL SERVER 2000 database tables. NIO have capabilityies to get data and transfer directly to the tables? If so, how can i do it?
Thanks a lot
AloisioChuckBing, i'm connected with database trough JDBC yet and i can retrieve data meaning my connection is OK.
I'm able to read data from a txt file (250mb large) trough MappedByteBuffer and write into another with the same size and content. It's working fine.
My question is if i can read the txt file and, instead write the data into another txt file, put these data into a table.
Thanks for your reply -
Socket disconnection event not being reveived java.nio
Hi All
My question is as below
i am using java.nio package + win 2K + jdk1.4.1_02
I have a Server accepting socket connection and is configured
in Nonblocking mode.
-The server receives (selector) events (accept event)
when a client requests for a connection .
-The server is getting read and write events when
client wants to read and write
-The server gets a read event when the client disconnects
My only problem is that the Server is not getting any events
when the client disconnect due to network break down
for EX when the network connection breaks the client disconnects
but the server selector is not getting any events
I am storing the client Socket objects in a Hash Table and
if i execute the .isConnected() method it returns true
even though the the client is down
The only work around is to try to read and write on the socket
i get an IO exception and am able to make out that the
client is disconnected
But this is not desirable for my client
as client modification to give any kind of echo is not possible and
a read /write event will cause problem.
Can any one tell how to detect client side disconnection
with out Read and Write on the client socket
I get an socket Exception when the socket is in Blocking IO and is blocked on read/ write but I am facing this problem in NonBlockingIO
Regards
Avinashint ct = read.selectNow();
if (ct > 0)
Set readyKeys = read.selectedKeys();
Iterator i = readyKeys.iterator();
while (i.hasNext())
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isReadable())
SelectableChannel nextReady = (SelectableChannel) key.channel();
SocketChannel sc = (SocketChannel) key.channel();
int rd = sc.read(buffer);
// Link is dead
if (rd == -1)
key.cancel();
key.attach(null);
nextReady.close();
continue;
// Process your data
catch (Exception e)
e.printStackTrace(); -
How to set proxy information in JAVA.NIO framework MINA or Netty?
Hi all,
THERE IS NO PROXY SUPPORT VIA JAVA.NIO.
How to use MINA or Netty,other framework support proxy?
Thanks in advance!The forum is focus on questionNo. The forum is provided by Sun and latterly Oracle to talk about core Java programming: in this case the Java networking APIs. Questions about 3rd party products should be directed to the people who supply them. And asking questions in places where the relevant expertise isn't concentrated is not a rational strategy.
I think you really need patience as a professional expert.Expert, yes. Professional? Is somebody paying me to post here?
And don't talk to me about patience. You have already completely exhausted any patience I may ever have had by repeating a question five times after I had already answered it. Judging by the below you are now in the process of repeating that process.
The information is shared by anybody.Correct, including me, and including the information that there are better places to ask than here about 3rd party products.
Besides, I think I submitted right question on right place.Then once again you are proving yourself incompetent, as I have already told you that it isn't the right place. -
Problematic frame: # J java.nio.MappedByteBuffer.load()
Helo all...
I'm getting an error when generating a PDF using BIRT Framework in Eclipse. Nothing fancy, just compiling a simple report to pdf.
The problem is, the JVM crashes with the following error:
# A fatal error has been detected by the Java Runtime Environment:
# EXCEPTION_IN_PAGE_ERROR (0xc0000006) at pc=0x0190f0be, pid=5848, tid=2392
# JRE version: 7.0_09-b05
# Java VM: Java HotSpot(TM) Client VM (23.5-b02 mixed mode, sharing windows-x86 )
**# Problematic frame:
# J java.nio.MappedByteBuffer.load()Ljava/nio/MappedByteBuffer;**
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
# An error report file with more information is saved as:
# C:\Users\luis\Desktop\teste\birt\hs_err_pid5848.log
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
I already tried:
1. Run the program in another computers - it worked
2. Reinstall Java on the computer - It did nothing
3. I check the integrity of the disk C: - Nothing
I already spent two days trying to fix this and I don't have any progress. I can't find a solution on the web either.
Thanks in advanceThanks for the help.
But it can not be related to BIRT because it works in every other computer that I tried so far. There is only one computer that not works properly.
If it were BIRT's fault, it wouldn't work in any computer.
Thanks again -
Java NIO locking and NTFS network resources
Hi all - just ran into a really nasty situation and I was wondering if anyone else has hit it and might have some suggestions.
Platform: JRE 1.4_02 on a Win XP machine
The following test code locks a file, then copies it to another location using NIO.
When I run it with source path on my local drives (C), it works fine. If I run it with source path on a network shared resource, it fails with an IOException with description 'Error performing inpage operation'.
If I disable the lock immediately before the copy operation, it works fine.
My conclusion is that there is something about the NIO locking implementation that prevents it from working properly with NTFS volumes on other hosts. Can this be right? I've found the following bug report:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4774175
but this seems like a huge problem that would prevent folks from using NIO in many, many applications. Maybe I'm wrong on something here...
Anyway, here's the test code:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
* Created on May 28, 2004
* (c) 2004 Trumpet, Inc.
* @author kevin
public class test {
private void createFile(File f) throws IOException{
FileOutputStream os = new FileOutputStream(f);
for(int i = 0; i < 10; i++){
os.write(i);
os.close();
public test() {
boolean testWithReleasingLockPriorToCopy = false;
final File f1= new File("w:/temp/test2.lok");
final File f2 = new File("w:/temp/test.lok");
f1.delete();
f2.delete();
try {
createFile(f1);
RandomAccessFile raf1 = new RandomAccessFile(f1, "rw");
RandomAccessFile raf2 = new RandomAccessFile(f1, "rw");
FileChannel ch1 = raf1.getChannel();
FileChannel ch2 = raf2.getChannel();
FileLock flock1 = ch1.lock();
if (!f2.getParentFile().exists() && !f2.getParentFile().mkdirs())
throw new IOException("Unable to create directories for destination file '" + f2 + "'");
if (testWithReleasingLockPriorToCopy)
flock1.release();
ch1.transferTo(0, raf1.length(), ch2);
raf1.close();
raf2.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
public static void main(String[] args) {
test t = new test();
}Does anyone have any pointers here? I need to be able to exclusively lock a file on a network drive (preventing any other applications from opening it), then make a copy of it. I can't use regular stream operations, because the lock prevents them from working properly (it appears that, once you grab a file lock using NIO, the only way your application can use the file is via the NIO operations - using stream operations fails...).
Thanks in advance for any help!
- Kevini've run into the same problem recently, channels working fine for local file locking, but when you turn to the network, they fail to accurately handle locks.
i ended up writing a jni utility to ship with my java application that locks files using native windows calls.
my .c file ends up looking something like this:
JNIEXPORT jint JNICALL Java_Mapper_NativeUtils_LockFile
(JNIEnv *env, jobject obj, jstring filename)
const char* ntvFilename = (*env)->GetStringUTFChars(env, filename, 0);
int retVal = (int)CreateFile
ntvFilename
, GENERIC_WRITE
, FILE_SHARE_READ
, 0
, OPEN_EXISTING
, FILE_FLAG_SEQUENTIAL_SCAN
, 0
//add code to throw java exceptions based on retVal
if (retVal == (int)INVALID_HANDLE_VALUE)
return retVal;
(*env)->ReleaseStringUTFChars(env, filename, ntvFilename);
return retVal;
JNIEXPORT jboolean JNICALL Java_Mapper_NativeUtils_UnlockFile
(JNIEnv *env, jobject obj, jint handle)
CloseHandle((void *)handle);
return 1;
it's a little shy on the error checking side, but it provides support for network file locking that java seems to lack. -
I'm not sure which forum this should go in so applogies if I got it wrong.
I would like to use the java.nio package from JDK1.4 with the J2ME. Is this possible in any way?
cheers
AndrewI'm not sure which forum this should go in so
applogies if I got it wrong.
I would like to use the java.nio package from JDK1.4
with the J2ME. Is this possible in any way?this topic should be post at
CLDC and MIDP
http://forum.java.sun.com/forum.jsp?forum=76
or
K Virtual Machine (KVM)
http://forum.java.sun.com/forum.jsp?forum=50
CLDC 1.0 and MIDP 1.0 doesn't include java.nio
please go to
http://java.sun.com/j2me/docs/
two documents will be useful
CLDC Specification, V1.0
MIDP 1.0 Specification, Final (JSR 37) -
Socket seems too slow...maybe java.nio?
Hello
In our system I have to receive Multicast Packets repeats very quick(1-5 ms). I have to link them one after an other in the order they sent. And if I miss a packet, something will go wrong...
I have to listen to different IPs and I use different threads for different IPs. If I use only one thread (and listens to only one IP) everything seems ok.
But if I starts listening to an other ip too, I miss 2 packets in a row, or only one if I turn off parsing the message (XML).
Here is the code I use in the Threads:
try{
socket = new MulticastSocket(port);
socket.setSoTimeout(1000);
inetAddress = InetAddress.getByName(ip);
NetworkInterface nInterface=
NetworkInterface.getByName(networkInterface);
if(nInterface!=null)socket.setNetworkInterface(nInterface);
socket.joinGroup(inetAddress);
catch(IOException ioe){
logger.error(ioe.getMessage(), ioe);
return;
try{
while (!interrupted()) {
try{
byte[] buffer = new byte[1480];
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
parse(buffer);
}catch(SocketTimeoutException stoe){
// DO NOTHING
}catch(IOException ioe){
logger.error(ioe.getMessage(), ioe);
}finally{
try {
if(socket!=null){
socket.leaveGroup(inetAddress);
} catch (IOException ioe) {}
Every Thread has its own parsing object.
Any tips, what is wrong?
Maybe java.nio could solve the problem somehow. There is a sample server in [Java Home]/sample/nio/server and it suggest that there are quicker methods to receive messages from different IPs (maybe Blocking/Pooled-Thread Server). But I can't understand the API and the Sample while I was reading it (20-30 minutes).
Could it be quicker? Does it worth toying with the idea?
Thanks:
BenceIn our system I have to receive Multicast Packets
repeats very quick(1-5 ms). I have to link them one
after an other in the order they sent. And if I miss
a packet, something will go wrong...There is no guarantee anywhere in the system that you won't miss a datagram. UDP doesn't make such guarantees. If you need all the packets you will have to build ACK or NACK into your protocol.
You can alleviate the problem by running a very large socket receive buffer. But you can't eliminate it. Rethink this.
NIO is not significantly quicker for applications like this, it is more scalable. -
I need to make the server is able to hold about 500 connections and operates on a single thread. The server itself should make all the connections. Where can I find examples of finished implementations?
I have an example, but it does not work
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.*;
public class NioClient implements Runnable {
// The host:port combination to connect to
private InetAddress hostAddress;
private String host;
private int port;
// The selector we'll be monitoring
private Selector selector;
// The buffer into which we'll read data when it's available
private ByteBuffer readBuffer = ByteBuffer.allocate(8192);
// A list of PendingChange instances
private List pendingChanges = new LinkedList();
// Maps a SocketChannel to a list of ByteBuffer instances
private Map pendingData = new HashMap();
// Maps a SocketChannel to a RspHandler
private Map rspHandlers = Collections.synchronizedMap(new HashMap());
public NioClient() {
try {this.selector = this.initSelector();} catch(IOException e){}
public void connect(String host, int port, RspHandler handler) throws IOException {
this.hostAddress = hostAddress;
this.host = host;
this.port = port;
this.send("$Hello |".getBytes(), handler);
public void send(byte[] data, RspHandler handler) throws IOException {
// Start a new connection
SocketChannel socket = this.initiateConnection();
// Register the response handler
this.rspHandlers.put(socket, handler);
// And queue the data we want written
synchronized (this.pendingData) {
List queue = (List) this.pendingData.get(socket);
if (queue == null) {
queue = new ArrayList();
this.pendingData.put(socket, queue);
queue.add(ByteBuffer.wrap(data));
// Finally, wake up our selecting thread so it can make the required changes
this.selector.wakeup();
handler.waitForResponse();
public void run() {
while (true) {
try {
// Process any pending changes
synchronized (this.pendingChanges) {
Iterator changes = this.pendingChanges.iterator();
while (changes.hasNext()) {
ChangeRequest change = (ChangeRequest) changes.next();
switch (change.type) {
case ChangeRequest.CHANGEOPS:
SelectionKey key = change.socket.keyFor(this.selector);
key.interestOps(change.ops);
break;
case ChangeRequest.REGISTER:
change.socket.register(this.selector, change.ops);
break;
this.pendingChanges.clear();
// Wait for an event one of the registered channels
this.selector.select();
// Iterate over the set of keys for which events are available
Iterator selectedKeys = this.selector.selectedKeys().iterator();
while (selectedKeys.hasNext()) {
SelectionKey key = (SelectionKey) selectedKeys.next();
selectedKeys.remove();
if (!key.isValid()) {
continue;
// Check what event is available and deal with it
if (key.isConnectable()) {
this.finishConnection(key);
} else if (key.isReadable()) {
this.read(key);
} else if (key.isWritable()) {
this.write(key);
} catch (Exception e) {
e.printStackTrace();
private void read(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
// Clear out our read buffer so it's ready for new data
this.readBuffer.clear();
// Attempt to read off the channel
int numRead;
try {
numRead = socketChannel.read(this.readBuffer);
} catch (IOException e) {
// The remote forcibly closed the connection, cancel
// the selection key and close the channel.
key.cancel();
socketChannel.close();
return;
System.out.println("READ");
if (numRead == -1) {
// Remote entity shut the socket down cleanly. Do the
// same from our end and cancel the channel.
key.channel().close();
key.cancel();
return;
// Handle the response
this.handleResponse(socketChannel, this.readBuffer.array(), numRead);
private void handleResponse(SocketChannel socketChannel, byte[] data, int numRead) throws IOException {
// Make a correctly sized copy of the data before handing it
// to the client
byte[] rspData = new byte[numRead];
System.arraycopy(data, 0, rspData, 0, numRead);
// Look up the handler for this channel
RspHandler handler = (RspHandler) this.rspHandlers.get(socketChannel);
// And pass the response to it
if (handler.handleResponse(rspData)) {
// The handler has seen enough, close the connection
socketChannel.close();
socketChannel.keyFor(this.selector).cancel();
private void write(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
synchronized (this.pendingData) {
List queue = (List) this.pendingData.get(socketChannel);
// Write until there's not more data ...
while (!queue.isEmpty()) {
ByteBuffer buf = (ByteBuffer) queue.get(0);
socketChannel.write(buf);
if (buf.remaining() > 0) {
// ... or the socket's buffer fills up
break;
queue.remove(0);
if (queue.isEmpty()) {
// We wrote away all data, so we're no longer interested
// in writing on this socket. Switch back to waiting for
// data.
key.interestOps(SelectionKey.OP_READ);
private void finishConnection(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
// Finish the connection. If the connection operation failed
// this will raise an IOException.
try {
socketChannel.finishConnect();
} catch (IOException e) {
// Cancel the channel's registration with our selector
System.out.println(e);
key.cancel();
return;
// Register an interest in writing on this channel
key.interestOps(SelectionKey.OP_WRITE);
private SocketChannel initiateConnection() throws IOException {
// Create a non-blocking socket channel
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
// Kick off connection establishment
socketChannel.connect(new InetSocketAddress(this.host, this.port));
// Queue a channel registration since the caller is not the
// selecting thread. As part of the registration we'll register
// an interest in connection events. These are raised when a channel
// is ready to complete connection establishment.
synchronized(this.pendingChanges) {
this.pendingChanges.add(new ChangeRequest(socketChannel, ChangeRequest.REGISTER, SelectionKey.OP_CONNECT));
return socketChannel;
private Selector initSelector() throws IOException {
// Create a new selector
return SelectorProvider.provider().openSelector();
public class RspHandler {
private byte[] rsp = null;
public synchronized boolean handleResponse(byte[] rsp) {
this.rsp = rsp;
this.notify();
return true;
public synchronized void waitForResponse() {
while(this.rsp == null) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(new String(this.rsp));
} NioClient NioClient = new NioClient();
Thread t = new Thread(NioClient);
t.setDaemon(true);
t.start();
RspHandler handler = new RspHandler();
NioClient.connect("69.28.156.250", 27040, handler);
NioClient.connect("72.165.61.188", 27040, handler);
NioClient.connect("208.111.133.84", 27011, handler);
NioClient.connect("72.165.61.136", 27012, handler);
Edited by: 915967 on 01.08.2012 7:07 -
Javac compiler throws java.nio.BufferOverflowException!!Why?
An exception has occurred in the compiler (1.4.1). Please file a bug at the Java Developer Connection (http://java.sun.com/cgi-bin/bugreport.cgi) after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report. Thank you.
java.nio.BufferOverflowException
at java.nio.charset.CoderResult.throwException(CoderResult.java:259)
at java.lang.StringCoding$CharsetSD.decode(StringCoding.java:186)
at java.lang.StringCoding.decode(StringCoding.java:222)
at java.lang.StringCoding.decode(StringCoding.java:228)
at java.lang.String.<init>(String.java:383)
at java.lang.String.<init>(String.java:404)
at java.io.UnixFileSystem.list(Native Method)
at java.io.File.list(File.java:914)
at com.sun.tools.javac.v8.code.ClassReader.list(ClassReader.java:1224)
at com.sun.tools.javac.v8.code.ClassReader.listAll(ClassReader.java:1320)
at com.sun.tools.javac.v8.code.ClassReader.fillIn(ClassReader.java:1340)
at com.sun.tools.javac.v8.code.ClassReader.complete(ClassReader.java:1049)
at com.sun.tools.javac.v8.code.Symbol.complete(Symbol.java:332)
at com.sun.tools.javac.v8.comp.Enter.visitTopLevel(Enter.java:467)
at com.sun.tools.javac.v8.tree.Tree$TopLevel.accept(Tree.java:390)
at com.sun.tools.javac.v8.comp.Enter.classEnter(Enter.java:442)
at com.sun.tools.javac.v8.comp.Enter.classEnter(Enter.java:456)
at com.sun.tools.javac.v8.comp.Enter.complete(Enter.java:588)
at com.sun.tools.javac.v8.comp.Enter.main(Enter.java:574)
at com.sun.tools.javac.v8.JavaCompiler.compile(JavaCompiler.java:334)
at com.sun.tools.javac.v8.Main.compile(Main.java:520)
at com.sun.tools.javac.Main.compile(Main.java:36)
at com.sun.tools.javac.Main.main(Main.java:27)See this bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4949631
Maybe you are looking for
-
Oracle RDB driver Version 3.01.00.01 Connectivity Issue
I have a SQL Server 2000 extended stored procedure connecting to an Oracle RDB data server via an ODBC connection (using Oracle RDB driver Version 3.01.00.01). The SQL Server 2000 runs on a Windows 2000 Server. This extended stored procedure opens
-
Good 2-way video but audio clear only 1-way
The problem is with audio during Mac-to-PC AV chats. I'm using an iSight on a 15" PowerBook (10.3.9) with cable ISP (tested 356kbps up / 3mbps down). The PC is newer Dell running Windows XP with Quickcam Fusion and similar cable ISP performance. The
-
Date Format on OWB 11.1.0.7 to use for input parameter for a mapping
All, What is the Date Format on OWB 11.1.0.7 to use for passing in an input parameter for a mapping to execute? I have tried '01-01-2010','01-JAN-2010','01.01.2010', 01/01/2010 and I get the following error: Error RPE-01003: An infrastructure conditi
-
OpenCL acceleration is not working for Firepro S10000 APU/GPUs
I have a machine with two separate AMD firepro S10000 APU/GPU video cards. They are not installed in a crossfire set. They are high-end devices, two APU/GPU per card, for a total of 4 cores. One core is used to drive a single video display on each de
-
Color Correction Quality Loss Computational Problem.
I have an entire film finished editing, all color graded etc... And I recently decided I want to go more conservative with the color palet. If I simply overlay a color correction over the entire film sequence. Would I lose more quality than I woul