Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

question about structs and TCP/IP in linux

Options
  • 02-04-2005 5:02pm
    #1
    Registered Users Posts: 2,320 ✭✭✭


    hey guys,

    i have a struct declared in my program that i want to pass from a client program through TCP/IP to a server program and vice versa. The struct is derclared here (dont mind the data types, its what we were told to use...)
    struct details {
            int op;
            char name[21];
            char host[41];
            char port[11];
    }
    

    the write and read statements are as followsw (domain is declared as struct details domain; )
    write(s, domain, sizeof(domain));
    
    read(s, domain, sizeof(domain));
    

    These dont seem to be working (it works if i pass single strings tho). My question is

    Is it possible to pass structs through tcp/ip or does it HAVE to be one long string?

    all the best,

    darren


Comments

  • Closed Accounts Posts: 423 ✭✭Dizz


    Is it possible to pass structs through tcp/ip or does it HAVE to be one long string?

    Structs - Not directly...
    One long string - not exactly!

    Ever try to print a struct? ;)
    What you actually need to do is look to marshalling - marshalling takes an in-memory object and places it in a buffer suitable for transmission "over the wire" - flattens it so to speak. Simply put - you need to place (marshall) your structs data members in a char buffer first and send that composite buffer across the wire using your write(...) function. Alternatively, you could access each struct member and send each using sequential calls to write(...) but this method is not advised and does not lead to maintainable and flexible software. Most middlewares that I have encountered use the first method.

    HTH

    Dizz


  • Registered Users Posts: 2,320 ✭✭✭Q_Ball


    Yeah i was using the dot method to get access at individual members. The second idea occured to me alright but i think it best to have as little read-write statements as possible. The other method i like, but maybe insert a character between each field so that i can split the string? I did this in java for my project and it worked well.


Advertisement