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.

Messages - kalimatas

Pages: [1]
Java / Re: Delays while sending packets with NIO
« on: November 06, 2014, 03:05:47 pm »
I duplicated the question on StackOverflow, but also posted it here in hope anyone had the same issue  :)

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 / Re: SFML book source code port
« on: November 05, 2014, 09:17:32 am »
Thanks for the replies!

I'll definitely try to ask for help on some Java specific resources, but I thought it would be more productive to communicate with people with SFML background  :)

Later I'll open a new thread on my networking problem where I'll describe it in more details.

Thanks again!

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!

General discussions / Re: SFML Game Development -- A book on SFML
« on: December 29, 2013, 10:36:47 am »
What does "like" mean, did you write the code yourself? It looks as if the airplanes have position (0,0).

Can you download the code from GitHub and compile it 1:1, without applying any changes?

I do not know why, but I haven't tried to compile original source from Github. Now as I did compile it and it worked fine, it turned out to be a problem in my code and I was able to find it.

The problem was in Aircraft::drawCurrent method. My method looked like this:

void Aircraft::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const {

I forgot to pass states as the second parameter to braw. It should be:

void Aircraft::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const {
        target.draw(mSprite, states);

Thanks for helping!

General discussions / Re: SFML Game Development -- A book on SFML
« on: December 28, 2013, 09:54:08 pm »
Hi everyone!

I've just finished reading the 3rd chapter and got a problem - my ships are always positioned in the left top corner of the view, and, moreover, I can see them only after the view is scrolled up completely to its top border (better see on the screenshot).


I rechecked my code several times - everything is like in the book. I would appreciate any help.

I'm using SFML 2.1 on Mac OS X 10.9.


Pages: [1]