{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

socketprogramming - Beej's Guide to Network Programming...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Beej's Guide to Network Programming Using Internet Sockets Brian “Beej Jorgensen” Hall [email protected] Version 2.4.5 August 5, 2007 Copyright © 2007 Brian “Beej Jorgensen” Hall
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ii Contents 1. Intro ................................................................................................................................................. 1 1.1. Audience 1 1.2. Platform and Compiler 1 1.3. Official Homepage 1 1.4. Note for Solaris/SunOS Programmers 1 1.5. Note for Windows Programmers 1 1.6. Email Policy 3 1.7. Mirroring 3 1.8. Note for Translators 3 1.9. Copyright and Distribution 3 2. What is a socket? ........................................................................................................................... 4 2.1. Two Types of Internet Sockets 4 2.2. Low level Nonsense and Network Theory 5 3. struct s and Data Handling ......................................................................................................... 8 3.1. Convert the Natives! 9 3.2. IP Addresses and How to Deal With Them 9 4. System Calls or Bust ................................................................................................................... 12 4.1. socket() —Get the File Descriptor! 12 4.2. bind() —What port am I on? 12 4.3. connect() —Hey, you! 14 4.4. listen() —Will somebody please call me? 15 4.5. accept() —“Thank you for calling port 3490.” 15 4.6. send() and recv() —Talk to me, baby! 17 4.7. sendto() and recvfrom() —Talk to me, DGRAM-style 17 4.8. close() and shutdown() —Get outta my face! 18 4.9. getpeername() —Who are you? 19 4.10. gethostname() —Who am I? 19 4.11. DNS—You say “whitehouse.gov”, I say “63.161.169.137” 19 5. Client-Server Background .......................................................................................................... 22 5.1. A Simple Stream Server 22 5.2. A Simple Stream Client 24 5.3. Datagram Sockets 25 6. Slightly Advanced Techniques .................................................................................................... 28 6.1. Blocking 28 6.2. select() —Synchronous I/O Multiplexing 28 6.3. Handling Partial send() s 33 6.4. Serialization—How to Pack Data 34 6.5. Son of Data Encapsulation 42 6.6. Broadcast Packets—Hello, World! 44
Image of page 2
Contents iii 7. Common Questions ...................................................................................................................... 48 8. Man Pages ..................................................................................................................................... 54 8.1. accept() 55 8.2. bind() 57 8.3. connect() 58 8.4. close() 59 8.5. gethostname() 60 8.6. gethostbyname() , gethostbyaddr() 61 8.7. getpeername() 63 8.8. errno 64 8.9. fcntl() 65 8.10. htons() , htonl() , ntohs() , ntohl() 66 8.11. inet_ntoa() , inet_aton() 68 8.12. listen() 70 8.13. perror() , strerror() 71 8.14. poll() 72 8.15. recv() , recvfrom() 74 8.16. select() 76 8.17. setsockopt() , getsockopt() 78 8.18. send() , sendto() 80 8.19. shutdown() 82 8.20. socket() 83 8.21. struct sockaddr_in , struct in_addr 85 9. More References ........................................................................................................................... 86 9.1. Books 86 9.2. Web References 86 9.3. RFCs 87 Index 88
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
1 1. Intro Hey! Socket programming got you down? Is this stuff just a little too difficult to figure out from the man pages? You want to do cool Internet programming, but you don't have time to wade through a gob of struct s trying to figure out if you have to call bind() before you connect() , etc., etc. Well, guess what! I've already done this nasty business, and I'm dying to share the information with everyone! You've come to the right place. This document should give the average competent C programmer the edge s/he needs to get a grip on this networking noise. 1.1. Audience This document has been written as a tutorial, not a reference. It is probably at its best when read by individuals who are just starting out with socket programming and are looking for a foothold. It is certainly not the complete guide to sockets programming, by any means. Hopefully, though, it'll be just enough for those man pages to start making sense...
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern