Thread: Sockets

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Sockets

    After visiting sites like MadWizard, ProgrammersHeaven, etc., I'm still having a lot of trouble with sockets programming. Almost all tutorials seem contradictory, so I need to ask for help here...

    Can anyone show me a very, very simple code, annotated, that'll show me how to send a message from one program to another, using sockets, by opening a connection ? Thanks for your help.

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    There is a very good tutorial at:
    http://www.ecst.csuchico.edu/~beej/guide/net/
    Try to read this one.
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The socket API is very small, containing just a handful of core functions. Windows sockets mostly mirror Berkley sockets, but also add an even more complicated layer on top of taht, obscuring things just a bit more.
    At first, just focus on the core functions and the underlying framework. Some of the data types are identical in size but different in their interpretation of the (same) data (data - such as port numbers and bind addresses - have to be stored in these data structures in network byte order for the code to work properly (but not the the sent/recieved data - the TCPIP stack will take care of that)).
    Also, several uniouns are used to (supposedly) make your life easier when you get around to messing with subnets, etc.
    And as far as sending and recieving data goes, you have to be keep reading or writing till the buffers are filled or emptied to ensure proper transmission. Sometimes the connection is suddenly closed for some reason, so you have to be prepared for that too. Also, be sure to read through your header docs- there's a whole lot of useful info in there as well.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I find the Beej tutorial not so good to begin Sockets, as it goes through a lot of things that assume you know what is being said. For example, you're not given an easy code showing simple socket functions and how they work, but a lot at once and it's all almost thrown at you. I still can't write, after reading the tutorial many times, a simple program allowing me to send a message over a network...

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Actually Korhedron raises something I am still to get round to as of yet. There seems to be no C++ standard sockets though there are programs out there written in C++.I'll get round to socket programming but right now I'm moving to file I/O and STL. Would just like to know if there is a standard C++ socket API? FYI I have done some light C sockets but as I normally do C programming in UNIX (and rarely Linux) I see I may have trouble converting these little progs to windows code.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
     bool connect(SOCKET socket, unsigned long address, unsigned short port)
    {
     sockaddr_in remote = {0};
     remote.sin_family = AF_INET;
     remote.sin_port = htons(port);
     remote.sin_addr.S_un.S_addr = htonl(address);
     return SOCKET_ERROR != ::connect(socket, (sockaddr*)&remote, sizeof(remote));
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    If your doing windows-programming you need to init you winsock before using it.

    Code:
    bool initWinsocket()
    {
      WORD wVersionRequested;
      WSADATA wsaData;
      int err;
     
      wVersionRequested = MAKEWORD( 2, 2 );
      err = WSAStartup( wVersionRequested, &wsaData );
      if ( err != 0 ) {
          return -1;
      }
     
      if ( LOBYTE( wsaData.wVersion ) != 2 || 
           HIBYTE( wsaData.Version ) != 2 ) {
          WSACleanup( );
          return -1; 
      }
       return 0;
      /* The WinSock DLL is acceptable. Proceed. */
    }
    Source :msdn

  8. #8
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    >>If your doing windows-programming you need to init you winsock before using it.

    Yep, beej covers that near the start of the book. When I was learning sockets I read a book all about networking (can't remember the name... was a white book with black text, something like Networking or Internetworking was one of the words... author started with a C I think). It was good because it covered everything about how networks work, then showed some code. Then I read beej and everything was dandy.

    Although beej does attempt to cover things like how IP addresses work (I think he does), his book still seems to require that you have a good understanding of how networking works down to the socket layer.

    I'd love to see a C++ standard sockets library, but I doubt it will happen for a long time.

  9. #9
    Registered User trainee's Avatar
    Join Date
    Jan 2004
    Posts
    32
    Originally posted by WDT
    Actually Korhedron raises something I am still to get round to as of yet. There seems to be no C++ standard sockets though there are programs out there written in C++.I'll get round to socket programming but right now I'm moving to file I/O and STL. Would just like to know if there is a standard C++ socket API? FYI I have done some light C sockets but as I normally do C programming in UNIX (and rarely Linux) I see I may have trouble converting these little progs to windows code.
    No there is currently no socket API in the STL. I heard somewhere they were thinking about adding it though.

    trainee

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    This thread is allready a little old but since i came accross this problem many times also and it took me about 30min to find these 2 sites i think its worth noting them.

    http://www.manualy.sk/sock-faq/unix-socket-faq.html

    http://www.developerweb.net/sock-faq/ (the old faq)
    http://www.developerweb.net/forum/ (same site, now in a forum)

    I found them extensive enough to cover most of the unclear things anybody i know came up with until now.
    For example SO_REUSEADDR

    Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  2. Cross platform sockets
    By zacs7 in forum Networking/Device Communication
    Replies: 5
    Last Post: 06-27-2007, 05:16 AM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM