Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - kalimatas

Pages: [1]
Java / Delays while sending packets with NIO
« on: November 06, 2014, 02:53:54 pm »

A few days ago I wrote about my problem with Java NIO delays while sending packets. So, as I was advised, I'm opening a separate thread for this issue. I made a minimal working example, where the problem is reproduced.

Here I'll show only parts of the code, full version is available here.

So, the program has two entities: Server and Client. If you start an application in a server mode, then a Server is created, starts to listen for new connections, and a new Client is automatically created and tries to connect to the Server. In client mode only a Client is created and connects to the Server.

The application also creates a new basic GUI window and starts an event loop, where everything happens.

The Client sends packets to the Server. It handles them by just logging the fact of accepting. There are two types of packets the Client can send: periodical packet (with an incremental ID) and an event packet (application reacts to pressing SPACE or M buttons).

Client sends packets:

public void update(Time dt) throws IOException {
        if (!isConnected) return;

        if (tickClock.getElapsedTime().compareTo(Time.getSeconds(1.f / 20.f)) > 0) {
            Packet intervalUpdatePacket = new Packet();

            PacketReaderWriter.send(socketChannel, intervalUpdatePacket);


    public void handleEvent(Event event) throws IOException {
        if (isConnected && (event.type == Event.Type.KEY_PRESSED)) {
            KeyEvent keyEvent = event.asKeyEvent();

            if (keyEvent.key == Keyboard.Key.SPACE) {
                LOGGER.info("press SPACE");
                Packet spacePacket = new Packet();
                PacketReaderWriter.send(socketChannel, spacePacket);

            if (keyEvent.key == Keyboard.Key.M) {
                LOGGER.info("press M");
                Packet mPacket = new Packet();
                PacketReaderWriter.send(socketChannel, mPacket);

Server accepts packets:

private void handleIncomingPackets() throws IOException {

        Set<SelectionKey> readKeys = readSelector.selectedKeys();
        Iterator<SelectionKey> it = readKeys.iterator();

        while (it.hasNext()) {
            SelectionKey key = it.next();

            SocketChannel channel = (SocketChannel) key.channel();

            Packet packet = null;
            try {
                packet = PacketReaderWriter.receive(channel);
            } catch (NothingToReadException e) {

            if (packet != null) {
                // Interpret packet and react to it
                handleIncomingPacket(packet, channel);

    private void handleIncomingPacket(Packet packet, SocketChannel channel) {
        PacketType packetType = (PacketType) packet.get();

        switch (packetType) {
            case INTERVAL_UPDATE:
                int intervalId = (int) packet.get();
                //LOGGER.info("handling interval packet: " + intervalId);
            case SPACE_BUTTON:
                LOGGER.info("handling SPACE button");
            case M_BUTTON:
                LOGGER.info("handling M button");

And here is the problem: I have quite big delays between pressing a button (and sending a corresponding packet from the Client) and accepting this packet on the Server. If I start a new instance of the application in a client mode (just add a new Client in short), the delays become even bigger.

I don't see any reason why these periodical packets create so much network load that other packets just cannot get through, but maybe I'm just missing something. Here I have to say that I'm not a Java expert, so don't blame me too much for not seeing something obvious  :)

Does anyone have any ideas?

Java / SFML book source code port
« on: November 03, 2014, 10:04:52 pm »

Hello guys!

For the last seven months I've been working on porting C++ source code of the SFML book to Java. And now I’m almost done! You can find the code on github.

I'm almost done, because new Mac OS X release and Java NIO package definitely don't want me to finish, and here I really need your help. I have several problems now (most of them in Network chapter), which I don't know (or I just missing something obvious) how to solve, because of the lack of Java knowledge.

One the problems is - bloom effect (which is done by means of shaders) doesn't work on Mac OS X 10.10, but it worked on 10.9.

The other big problem is - network packets from the last chapter come with big delays. This part is the most difficult, because JSFML doesn't implement SFML network library, but relays on native Java implementation.

The list of other issues you can find here.

So I'm asking you to help me to finish my work. I've been struggling with these problems for the last two weeks and couldn't resolve out.

Thank you in advance! I'll really appreciate any help!

Pages: [1]