Sending a struct containing a string over network

  • Sending a struct containing a string over network pleasedontlaugh

    I'm trying to send structs as neatly sorted packets using iPhone Game Kit..

    I have a struct which looks like:

    typedef struct {
       int coolStuff;
       char playerID[100];
    } MyStruct;

    Then I'm using strncpy() to copy the NSString I want into MyStruct.playerID. However, I would like to be able to send a playerID (string) of any size and possibly multiple strings per struct. Any suggestions?

    There is a chance I'm making this more difficult than it actually is..

  • Google's Protocol Buffers is a project designed to help with this kind of thing. Makes it somewhat easy to move structs around between memory, network, disk, et al.

  • The fundamental thing you need to do is just have code on one side that knows how to serialize data, and code on the other that knows how to deserialize.

    So, for variable length strings, just send the length first:

       n = strlen(playerid) + 1; // for NULL terminator

    and then on the receiving side get the length, and read that many bytes.:

       int n;
       char *s;
       s = malloc(n)

    Of course, what you should really do is gather up your communication into packets, and then send blocks of data at once with headers, have type info in a debug mode for safety, etc. but that's the gist of it.

    Google protocol buffers look like they are a way of turning data into something serializable, so in that version of this example send and recv would be an XML string, which is great for general servers, but you may want more explicit control.

c++ networking objective-c c
Related questions and answers
  • I have a very simple effect file shown below. I am using this to draw 2D lines, however it is not behaving how I expected and I can't seem to get my head round why. If I draw a line that goes from 0,0 to 100, 100 for example, I would expect it to draw a line from one corner of the screen to a little way in. I would expect it to treat these numbers as screen coordinates. Instead, the line is huge! A line of about 2 long fills the whole screen. Why is this? How can I modify my shader to 'think' in screen coordinates? // a struct for the vertex shader return value struct VSOut { float4 Col

  • every 3 or 4 X's down in the wall... I hope this is enough explination, Thanks for any Help, and LOOK AT PREVIOUS POST ABOUT COLLISION DETECTION FOR MORE SPECIFIC FUNCTION PROBLEMS otherwise, heres... #include <sstream> // gives access to rand function #include <cstdlib> //gives access to time functions #include <ctime> // mySTOPWATCH i think i'm gonna cry.... =') #include <...; save; public: Map(){}; Map(string* lines, int i, string name= "map"){name = name; contents.resize(56

  • I want to be able to (only) define game states using Lua script, but I'm not sure how I should do it. Here's what I have in mind currently: For each state, I will create a .lua file that contains... exiting the state). So if I want to have a MainMenuState, I will have a file called "MainMenuState.lua" which will contain something like this: MainMenuState = {} MainMenuState["onEnter"] = function.... StateManager will have a function that registers a state under a unique name: void registerState(string stateName, string fileName); State registration will be done in script by placing the registration

  • them to be attached to the node in most cases, even if I allow setting global lights to the scene. @Nicol: Yes that's what I'm trying to figure out. You can see the code doesn't rely on any hardware... full of questions about this.. Thanks in advance. EDIT: I'm gonna add some code about it Yes probably I'm mixing up things, but it's complicated because there are a lot of data: struct...well... I'm building the animation system of my game engine (the skeletal and skinned animation stuff), and I came to a point where I added so much functionality in the frame and node structures

  • . I fixed those by just removing those entirely but this one cannot be removed it needs to exist. Don't know if this has anything to do with it, but I'm using visual studio 11 dev preview, and using...; nodeChildren.getCount(); i++) { string type = nodeChildren[i]->getAttribute("sid"); if (type == "rotationX") { string data = nodeChildren[i]->getCharData(); stringstream stm(data...") { string data = nodeChildren[i]->getCharData(); stringstream stm(data); stm >> matWorld.m[1][0]; stm >> matWorld.m[1][1]; stm >> matWorld.m[1

  • if a Vertex looks like this: struct vertex { vec3 point; vertex [] connected; }; Can I tell if this makes a solid shape with no holes? (e.g a sphere or a cube, NOT a sphere with a side missing for example) (if i can see the back of a face from outside of the shape then it is not solid)

  • i was able to compile and link V8 against my game and code interpretation works fine. However I want to divide my code and the game loop should exist in one thread and the scripting engine should run in a second thread alongside my game loop. I am using SDL and therefore tried this code to spawn a new thread SDL_CreateThread(ScriptingEngine::SpawnMain, NULL); where the following code is my...;v8::Value> result = script->Run(); context.Dispose(); return EXIT_SUCCESS; } } However I get an error like Access violation reading location 0x00000000. in

  • ; scd.Windowed = TRUE; scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // create a device class and swap chain class using the information in the scd struct D3D10CreateDeviceAndSwapChain(NULL...(); }; #endif If I call Init() from game.cpp, the code works fine, if I call cube->init(), just as this function is entered (not when the other one is left), the device pointer suddenly has an address...Basically when placed in the same file this works fine, but if placed in separate files (and I have tested this) just after Init() is called, the pointer to ID3D10* device's value is suddenly

  • I am setting an HLSL effect variable in the following way in a number of places. extern ID3D10EffectVectorVariable* pColour; pColour = pEffect->GetVariableByName("Colour")->AsVector(); pColour->SetFloatVector(temporaryLines[i].colour); In one of the places it is set in a loop, each line in the vector temporaryLines has a D3DXCOLOR variable associated with it. The most annoying thing about this problem is that it actually works on rare occasions, but most of the time it doesn't. Are there any known issues with this kind of code? Here it works: void GameObject::Draw(D3DMATRIX

Data information