socket programming

Discussion in 'C Programming' started by PH, Nov 16, 2003.

  1. PH

    PH Guest

    Hi,
    I need a bit of explanation (or pointers to online references) on socket
    programming in linux. How do I send a data structure over the internet
    and how mtu affects the data flow? does the computer wait til the local
    buffer reaches a treshold before it sends? if so, how do i extract each
    structure with its corresponding header and body? I can find only htons
    and htonl for byte ordering conversion. What do I do with a struct
    that's, say, 42 or 75 bytes?

    I'm new to socket programming. I hope u guys can help. Many many thanks
    in advance :)
    PH, Nov 16, 2003
    #1
    1. Advertising

  2. PH

    Sidney Cadot Guest

    PH wrote:

    > Hi,
    > I need a bit of explanation (or pointers to online references) on socket
    > programming in linux.


    You're better off asking this in linux-specific newsgroups, or some
    newsgroup related to networking. This newsgroup is about standard C, and
    the C standard doesn't say anything about sockets (it's a platform-
    specific library extention, originating with BSD Unix I think).

    Best regards,

    Sidney
    Sidney Cadot, Nov 16, 2003
    #2
    1. Advertising

  3. On Sun, 16 Nov 2003 09:20:50 +0800, PH <> wrote:

    > Hi,
    > I need a bit of explanation (or pointers to online references) on socket
    > programming in linux. How do I send a data structure over the internet
    > and how mtu affects the data flow? does the computer wait til the local
    > buffer reaches a treshold before it sends?


    As already said, the socket part is not Standard C and offtopic;
    although I would add for generic sockets, as opposed to any Linux
    specifics, I believe comp.unix.programmer is also good.

    > if so, how do i extract each
    > structure with its corresponding header and body? I can find only htons
    > and htonl for byte ordering conversion. What do I do with a struct
    > that's, say, 42 or 75 bytes?
    >

    However, all BSD-type sockets I know of transport memory contents
    unchanged, so you have the issue of whether the representation of data
    in memory on system is the same as that on another -- and, if in C,
    *that* is ontopic; the same issue arises for fwrite'ing and fread'ing
    binary files which is standard; and the answer is it's not required --
    many details of the representation depend on the 'implementation', a
    term of art that basically means the combination of CPU, compiler,
    library, and system/OS you use, with some rare exceptions that don't
    matter here; and in fact *do* vary between implementations.

    If both/all ends are the same architecture e.g. Linux/GNU/x86 *and
    using the same compiler/options*, you can probably get away with just
    sending/recving an identically declared struct and it will work; for
    any other case you probably need to worry about either defining and
    converting to and from a common representation, or defining pairwise
    conversions between all (current? expected? possible?) platforms; and
    even for a homogenous environment it is better to do this, because
    they may well become heterogenous in the future.

    An obvious common (platform-independent) form is text; because of its
    simplicity and ease of debugging this is used in many Internet
    application formats (SMTP/mail, NNTP/news, HTTP/HTML) though by no
    means all (cf SNMP). Although typically somewhat less efficient, text
    is simpler to define and often to implement correctly, and in modern
    applications the inefficiency is rarely a problem. This is supported
    in C by s[n]printf and sscanf, and more specific routines like strtol
    and even wcstombs. Remember that C requires strings to be terminated
    by a null character, but it is usual to not actually send that null,
    and the recv'er usually needs to (allow for and) add it.

    Another possibility is a binary format like XDR (provided on many?
    most? Unices as part of the "Sun" RPC = Remote Procedure Call package)
    or (more complicated) ASN.1; and you can always define your own,
    possibly using {n,h}to{h,n}{s,l} for network-endian integers in the
    same way as the lower IP levels.

    > I'm new to socket programming. I hope u guys can help. Many many thanks
    > in advance :)


    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Nov 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,769
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    949
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    532
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    648
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    703
Loading...

Share This Page