Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Extreme lag...  (Read 1529 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Member




...


« Posted 2008-12-05 21:49:02 »

When I use my server, if I get more than 2 clients connected onto it, I get massive lag. this is my socket.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * This file is part of the "Renoria" Game.
 * Copyright (C) 2008
 * IDGames.
 */


package renoria.net;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import renoria.Console;
import renoria.GameProperties;
import renoria.tools.HexTool;
import renoria.tools.data.input.ByteArrayByteStream;
import renoria.tools.data.input.GenericSeekableLittleEndianAccessor;
import renoria.tools.data.input.SeekableLittleEndianAccessor;

/**
 *
 * @author David
 */

public class PacketHandler implements GameProperties, Runnable {
    private Socket sock;
    private Thread runThread;
    private DataOutputStream dos;
    private DataInputStream dis;
    private PacketProcessor processor;

    public PacketHandler() throws IOException, Exception {
        runThread = new Thread(this, "Packet Processor");
        //runThread.setPriority(Thread.MAX_PRIORITY);
       runThread.start();
        processor = new PacketProcessor();
        sock = new Socket(ip, port);
        sock.setTcpNoDelay(true);
        sock.setKeepAlive(true);
        dos = new DataOutputStream(sock.getOutputStream());
        dis = new DataInputStream(sock.getInputStream());
        System.out.println("Packet processor started.");
    }
   
    @Override public void run() {
        while(true) {
            try {
                if (sock.isInputShutdown() || sock.isOutputShutdown()
                        || sock.isClosed() || !sock.isConnected()) {
                    Console.getWindow().showError("Connection Lost", "The connection with the server was lost.");
                    sock.close();
                    return;
                }
                byte[] data = new byte[1024];
                byte[] rdata;
                byte b;
                int c = 0;
                while ((b = dis.readByte()) != -1) {
                    data[c] = b;
                    c++;
                }
                rdata = new byte[c];
                for (int i = 0; i < c; i++) {
                    rdata[i] = data[i];
                }
                SeekableLittleEndianAccessor slea = new GenericSeekableLittleEndianAccessor(new ByteArrayByteStream(rdata));
      short packetId = slea.readShort();
                if (processor.getHandler(packetId) != null) {
                    processor.getHandler(packetId).handlePacket(slea);
                } else {
                    System.out.println("Got unhandled packet: " + HexTool.toString(rdata));
                }
            }catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (Exception e) {
               
            }
        }
    }
   
    public Socket getConnection() {
        return sock;
    }
   
    public void close() throws IOException {
        sock.close();
    }
   
    public void write(RenoriaPacket packet) throws IOException {
        if (packet.getOnSend() != null) {
            packet.getOnSend().run();
        }
        byte[] b = packet.getBytes();
        try {
            for (int i = 0; i < b.length; i++) {
                dos.writeByte(b[i]);
            }
            dos.writeByte(-1);
        } catch (IOException ioe) {
           
        }
        System.out.println("Sent packet: " + HexTool.toString(packet.getBytes()));
    }
   
    public void write(byte[] b) throws IOException {
        try {
            for (int i = 0; i < b.length; i++) {
                dos.writeByte(b[i]);
            }
            dos.writeByte(-1);
        } catch (IOException ioe) {
           
        }
    }
}
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2008-12-05 22:00:19 »

You call
   sock.setTcpNoDelay(true);
and then call
   writeByte(...)
in a loop. If you write N bytes, it will result in N tcp packets. (slow!)


Switch to
   write(byte[], off, len)
and it might solve all your problems.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Renoria

Junior Member




...


« Reply #2 - Posted 2008-12-05 22:23:12 »

okay I will try that Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Renoria

Junior Member




...


« Reply #3 - Posted 2008-12-05 22:25:00 »

so do I replace it with

1  
dos.write(b, 0, b.length);

??
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2008-12-05 22:53:53 »

Read the Javadocs.

Use Google.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Renoria

Junior Member




...


« Reply #5 - Posted 2008-12-05 23:52:53 »

yep it worked. thanks. Grin
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!