[Networking] General TCP Questions menu

User Tag List

Results 1 to 2 of 2
  1. #1
    xalcon's Avatar Contributor ふたなり
    Authenticator enabled
    Reputation
    198
    Join Date
    Oct 2008
    Posts
    291
    Thanks G/R
    20/58
    Trade Feedback
    0 (0%)
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    [Networking] General TCP Questions

    Hi folks,
    in the past I've developed a few applications using TCP Sockets and I always had a situation in mind, which might cause problems.
    TCP is a stream based protocol. So, when I want to use packets, I have to create some kind of generic packet handler.
    Lets say I my generic packet structure looks like this (Pseudocode)
    Code:
    struct GenericPacket
    {
        uint16 opcode;
        uint16 packetSize;
        byte payload[packetSize];
    };
    I never had any problems with this, even with bigger packets (size > 1500 bytes) but recently I tried to create a websocket server in c# using the autobahn test suite.
    In the suite is a test were packets are send in chops
    Case 1.1.8: Send text message with payload of length 65536. Sent out data in chops of 997 octets.
    In a worst case scenario, I would receive a packet with size X while only getting a few bytes (because of lag, packet loss, etc), so I have to wait until I receive the remaining bytes.

    Now my questions: Is this the general case in network programming? Do I have take into account, that packets might come in chunked? How do I handle those scenarios properly? Or is this a problem which doesn't realy exist?

    In my websocket server I've read all data from the socket into a byte buffer, so I can peek into the data more easily. But I'd never seen such stuff in any open source projects on the web. Every project I've looked into never used any kind of data buffer to make sure they get always the whole packet.
    "Threads should always commit suicide - they should never be murdered" - DirectX SDK

    [Networking] General TCP Questions
  2. #2
    Cromon's Avatar Legendary


    Reputation
    840
    Join Date
    Mar 2008
    Posts
    714
    Thanks G/R
    0/7
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello xalcon

    Yes, thats the general case. There is no guarantee on how much data you receive on a call to recv. Various factors influence that amount, for example this one: Maximum transmission unit - Wikipedia, the free encyclopedia

    So in a generic read function would look somewhat like this:
    Code:
    template<typename T>
    T read() {
        T ret;
        byte* tPtr = reinterpret_cast<byte*>(&ret);
        int bytesRead = 0;
        while(bytesRead < sizeof(T)) {
            int numRead = recv(mSocket, tPtr + bytesRead, sizeof(T) - bytesRead, 0);
            if(numRead <= 0) errorHandler();
            bytesRead += numRead;
        }
    
        return ret;
    }
    
    void readBytes(std::vector<byte>& data) {
        int bytesRead = 0;
        while(bytesRead < data.size()) {
            int numRead = recv(mSocket, data.data() + bytesRead, data.size() - bytesRead);
            if(numRead <= 0) { errorHandler(); }
            bytesRead += numRead;
        }
    }
    
    GenericPacket readPacket() {
        GenericPacket ret = { 0 };
        ret.opcode = read<uint16>();
        ret.packetSize = read<uint16>();
        ret.payload.resize(ret.packetSize);
        readBytes(ret.payload);
        return ret;
    }
    Greetings
    Cromon

Similar Threads

  1. Some general WoW questions
    By SectorSeven in forum World of Warcraft General
    Replies: 8
    Last Post: 07-16-2008, 04:58 PM
  2. General Spec Question - Leveling Warr.
    By Shinyshoes in forum World of Warcraft General
    Replies: 4
    Last Post: 02-29-2008, 08:36 PM
  3. General Model Question
    By Aghula in forum WoW ME Questions and Requests
    Replies: 4
    Last Post: 11-26-2007, 09:19 PM
  4. More general hacking questions
    By aqua200546 in forum Community Chat
    Replies: 2
    Last Post: 05-23-2007, 08:06 PM
  5. General Hacking Questions
    By ALB822 in forum World of Warcraft General
    Replies: 10
    Last Post: 03-31-2007, 03:50 AM
All times are GMT -5. The time now is 12:01 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search