Git Product home page Git Product logo

java-nio-server's People

Contributors

jjenkov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

java-nio-server's Issues

a question about message buffer

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?

Message Buffer Issue.

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.

maybe a little problem of Message class.

why use this.offset as start point instead of this.offset+this.length

public void writePartialMessageToMessage(Message message, int endIndex){
int startIndexOfPartialMessage = message.offset + endIndex;
int lengthOfPartialMessage = (message.offset + message.length) - endIndex;
System.arraycopy(message.sharedArray, startIndexOfPartialMessage, this.sharedArray, this.offset, lengthOfPartialMessage);
}

Some bug in your code

  • 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;
            }
        }

client socket time out

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!

MessageBuffer will be full

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.

can this server push message to client ?

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.

The application dies under high load

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)

two messages simultaneously

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.