jjenkov / java-nio-server Goto Github PK
View Code? Open in Web Editor NEWA Java NIO Server using non-blocking IO all the way through.
License: Apache License 2.0
A Java NIO Server using non-blocking IO all the way through.
License: Apache License 2.0
thank you for your code.A question about the method getMessage() in MessageBuffer Class.how can I get the message from the mediumMessageBuffer or largeMessageBuffer when it overflow 4k or 128k.The method getMessage() seems just can get message from smallMessageBuffer?
Hellio Jjenkov,
I was using your server, I like the way you have coded it but there is an issue I found out when I bombarded the server with multiple clients. It can receive the total of 512 messages.
Furthermore, I found out that if I change the size of:
public static int KB = Constant.maxLimit; public static int MB = Constant.maxLimit * KB; byte[] smallMessageBuffer = new byte[Constant.maxLimit * 4 * KB]; QueueIntFlip smallMessageBufferFreeBlocks = new QueueIntFlip(Constant.maxLimit);
The maxLimit is:
public class Constant { public static int maxLimit = 2048; }
In Server Class
Queue socketQueue = new ArrayBlockingQueue(Constant.maxLimit);
In SocketProcessor
private ByteBuffer readByteBuffer = ByteBuffer.allocate(Constant.maxLimit * Constant.maxLimit); private ByteBuffer writeByteBuffer = ByteBuffer.allocate(Constant.maxLimit * Constant.maxLimit); private double nextSocketId = 16 * Constant.maxLimit;
The client code I am using is:
client = SocketChannel.open(); isa = new InetSocketAddress("127.0.0.1", 9998); client.connect(isa); client.configureBlocking(false); sendMessage(); //here i send PING. client.close(); try { Thread.sleep(500); } catch (InterruptedException ex) { Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex); }
Please reply to the issue and help me accordingly.
java-nio-server/src/main/java/com/jenkov/nioserver/Message.java
Lines 92 to 97 in 35fdd0f
In your HttpUtil.parseHttpRequest()
method:
the variable bodyEndIndex
should be bodyStartIndex + httpHeaders.contentLength + 1
rather than bodyStartIndex + httpHeaders.contentLength
In your Message.writePartialMessageToMessage
method:
the variable startIndexOfPartialMessage
should just be endIndex + 1
rather than message.offset + endIndex
.
the variable lengthOfPartialMessage
should be (message.offset + message.length) - endIndex + 1
rather than (message.offset + message.length) - endIndex
I got a NPE when invoking the HttpMessageReader.read()
.The code snippet is shown below:
Message message = this.messageBuffer.getMessage; //the return message might be null
message.metadata = new HttpHeaders(); //the NPE occurs here
To solve the problem,I add some code in your QueueIntFlip.take()
method:
if(!flipped){
//add this if
if(readPos == capacity) {
readPos = 0;
}
if(readPos < writePos){
return elements[readPos++];
} else {
return -1;
}
}
Hi, jjenkov, I really appreciate you blog because I've learned how to use Java NIO.And I have written a Http server based on your code.It works well when there are not much connections.But when I test it with high concurrent connection(10K connection), almost all the connections are time out(Socket time out). I just don't know how to optimize it now. Could you give me some advice? Thanks anyway!
You only get Message from MessageBuffer, but you never return to MessageBuffer. Byte array will be full. No message could be gotten eventually. So this server will be eventually unavailable to receive or send messages.
I want to make a Tcp Server that can push message to client, and I'm worry about how to save and mark every SocketChannel of client.
does your code have this function ? thanks.
I did some testing using 'ab' tool.
So,
./ab -n 100000 -c 10000 -s 120 http://localhost:9999/
makes the application stop with stack trace:
Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64822] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64823] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64824] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64825] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64826] Socket accepted: java.nio.channels.SocketChannel[connected local=/0:0:0:0:0:0:0:1:9999 remote=/0:0:0:0:0:0:0:1:64827] Exception in thread "Thread-0" java.lang.IllegalStateException: Queue full at java.util.AbstractQueue.add(AbstractQueue.java:98) at java.util.concurrent.ArrayBlockingQueue.add(ArrayBlockingQueue.java:312) at com.jenkov.nioserver.SocketAccepter.run(SocketAccepter.java:44) at java.lang.Thread.run(Thread.java:748)
it seems that if two messages comes simultaneously, then problems there may occure:
HttpMessageReader:read:int endIndex = HttpUtil.parseHttpRequest(...
I think "for loop" need there.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.