⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intro.html

📁 Beej的socket教材
💻 HTML
字号:
<HTML><HEAD><TITLE>Intro</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.70"><LINKREL="HOME"TITLE="Beej's Guide to Network Programming"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Beej's Guide to Network Programming"HREF="index.html"><LINKREL="NEXT"TITLE="What is a socket?"HREF="theory.html"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=utf-8"></HEAD><BODYCLASS="sect1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Beej's Guide to Network Programming</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="index.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="theory.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="sect1"><H1CLASS="sect1"><ANAME="intro">1. Intro</A></H1><P>Hey!  Socket programming got you down?  Is this stuff just alittle too difficult to figure out from the <BCLASS="command">man</B>pages?  You want to do cool Internet programming, but you don't havetime to wade through a gob of <TTCLASS="type">struct</TT>s trying to figure outif you have to call <TTCLASS="function">bind()</TT> before you<TTCLASS="function">connect()</TT>, etc., etc.</P><P>Well, guess what!  I've already done this nasty business, and I'mdying to share the information with everyone!  You've come to the rightplace.  This document should give the average competent C programmer theedge s/he needs to get a grip on this networking noise.</P><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="AEN46">1.1. Audience</A></H2><P>This document has been written as a tutorial, not a reference.  Itis probably at its best when read by individuals who are just startingout with socket programming and are looking for a foothold.  It iscertainly not the <EM>complete</EM> guide to socketsprogramming, by any means.</P><P>Hopefully, though, it'll be just enough for those man pages tostart making sense... <TTCLASS="computeroutput">:-)</TT></P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="AEN52">1.2. Platform and Compiler</A></H2><P>The code contained within this document was compiled on a Linux PCusing Gnu's <BCLASS="command">gcc</B> compiler.  It should, however, buildon just about any platform that uses <BCLASS="command">gcc</B>.  Naturally,this doesn't apply if you're programming for Windows--see the <AHREF="intro.html#windows">section on Windows programming</A>, below.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="AEN58">1.3. Official Homepage</A></H2><P>This official location of this document is at California StateUniversity, Chico, at <TTCLASS="computeroutput"><AHREF="http://www.ecst.csuchico.edu/~beej/guide/net/"TARGET="_top">http://www.ecst.csuchico.edu/~beej/guide/net/</A></TT>.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="solaris">1.4. Note for Solaris/SunOS Programmers</A></H2><P>When compiling for Solaris or SunOS, you need to specify someextra command-line switches for linking in the proper libraries.  Inorder to do this, simply add "<TTCLASS="computeroutput">-lnsl -lsocket-lresolv</TT>" to the end of the compile command, likeso:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="screen">&#13;    <TTCLASS="prompt">$</TT> <BCLASS="command">cc -o server server.c -lnsl -lsocket -lresolv</B></PRE></TD></TR></TABLE><P>If you still get errors, you could try further adding a"<TTCLASS="computeroutput">-lxnet</TT>" to the end of that commandline.  I don't know what that does, exactly, but some people seem toneed it.</P><P>Another place that you might find problems is in the call to<TTCLASS="function">setsockopt()</TT>.  The prototype differs from that onmy Linux box, so instead of:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;	int yes=1;</PRE></TD></TR></TABLE><P>enter this:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;	char yes='1';</PRE></TD></TR></TABLE><P>As I don't have a Sun box, I haven't tested any of the aboveinformation--it's just what people have told me through email.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="windows">1.5. Note for Windows Programmers</A></H2><P>I have a particular dislike for Windows, and encourage you to tryLinux, BSD, or Unix instead.  That being said, you can still use thisstuff under Windows.</P><P>First, ignore pretty much all of the system header files I mentionin here.  All you need to include is:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;winsock.h&#62; </PRE></TD></TR></TABLE><P>Wait!  You also have to make a call to<TTCLASS="function">WSAStartup()</TT> before doing anything else with thesockets library.  The code to do that looks something like this:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting">&#13;    #include &#60;winsock.h&#62;    {        WSADATA wsaData;   // if this doesn't work        //WSAData wsaData; // then try this instead        if (WSAStartup(MAKEWORD(1, 1), &#38;wsaData) != 0) {            fprintf(stderr, "WSAStartup failed.\n");            exit(1);        } </PRE></TD></TR></TABLE><P>You also have to tell your compiler to link in the Winsocklibrary, usually called <TTCLASS="filename">wsock32.lib</TT> or<TTCLASS="filename">winsock32.lib</TT> or somesuch.  Under VC++, this can bedone through the <TTCLASS="computeroutput">Project</TT> menu, under<TTCLASS="computeroutput">Settings...</TT>.  Click the<TTCLASS="computeroutput">Link</TT> tab, and look for the box titled"Object/library modules".  Add "wsock32.lib" to that list.</P><P>Or so I hear.</P><P>Finally, you need to call <TTCLASS="function">WSACleanup()</TT> whenyou're all through with the sockets library.  See your online help fordetails.</P><P>Once you do that, the rest of the examples in this tutorial shouldgenerally apply, with a few exceptions.  For one thing, you can't use<TTCLASS="function">close()</TT> to close a socket--you need to use<TTCLASS="function">closesocket()</TT>, instead.  Also,<TTCLASS="function">select()</TT> only works with socket descriptors, notfile descriptors (like <TTCLASS="constant">0</TT> for<TTCLASS="computeroutput">stdin</TT>).</P><P>There is also a socket class that you can use,<TTCLASS="type">CSocket</TT>.  Check your compilers help pages for moreinformation.</P><P>To get more information about Winsock, read the <AHREF="http://tangentsoft.net/wskfaq/"TARGET="_top">Winsock FAQ</A> and go from there.</P><P>Finally, I hear that Windows has no <TTCLASS="function">fork()</TT>system call which is, unfortunately, used in some of my examples.  Maybeyou have to link in a POSIX library or something to get it to work, oryou can use <TTCLASS="function">CreateProcess()</TT> instead.<TTCLASS="function">fork()</TT> takes no arguments, and<TTCLASS="function">CreateProcess()</TT> takes about 48 billion arguments.If you're not up to that, the <TTCLASS="function">CreateThread()</TT> is alittle easier to digest...unfortunately a discussion aboutmultithreading is beyond the scope of this document.  I can only talkabout so much, you know!</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="emailpolicy">1.6. Email Policy</A></H2><P>I'm generally available to help out with email questions so feelfree to write in, but I can't guarantee a response.  I lead a prettybusy life and there are times when I just can't answer a question youhave.  When that's the case, I usually just delete the message.  It'snothing personal; I just won't ever have the time to give the detailedanswer you require.</P><P>As a rule, the more complex the question, the less likely I am torespond.  If you can narrow down your question before mailing it and besure to include any pertinent information (like platform, compiler,error messages you're getting, and anything else you think might help metroubleshoot), you're much more likely to get a response.  For morepointers, read ESR's document, <AHREF="http://www.tuxedo.org/~esr/faqs/smart-questions.html"TARGET="_top">How To AskQuestions The Smart Way</A>.</P><P>If you don't get a response, hack on it some more, try to find theanswer, and if it's still elusive, then write me again with theinformation you've found and hopefully it will be enough for me to helpout.</P><P>Now that I've badgered you about how to write and not write me,I'd just like to let you know that I <EM>fully</EM>appreciate all the praise the guide has received over the years.  It's areal morale boost, and it gladdens me to hear that it is being used forgood! <TTCLASS="computeroutput">:-)</TT>  Thank you!</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="AEN120">1.7. Mirroring</A></H2><P>You are more than welcome to mirror this site, whether publicallyor privately.  If you publically mirror the site and want me to link toit from the main page, drop me a line at<TTCLASS="email">&#60;<AHREF="mailto:beej@piratehaven.org">beej@piratehaven.org</A>&#62;</TT>.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="AEN124">1.8. Note for Translators</A></H2><P>If you want to translate the guide into another language, write meat <TTCLASS="email">&#60;<AHREF="mailto:beej@piratehaven.org">beej@piratehaven.org</A>&#62;</TT> and I'll link to your translation from themain page.</P><P>Feel free to add your name and email address to thetranslation.</P><P>Sorry, but due to space constraints, I cannot host thetranslations myself.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="AEN130">1.9. Copyright and Distribution</A></H2><P>Beej's Guide to Network Programming is Copyright 漏 1995-2001Brian "Beej" Hall.</P><P>This guide may be freely reprinted in any medium provided that itscontent is not altered, it is presented in its entirety, and thiscopyright notice remains intact.</P><P>Educators are especially encouraged to recommend or supply copiesof this guide to their students.</P><P>This guide may be freely translated into any language, providedthe translation is accurate, and the guide is reprinted in itsentirety.  The translation may also include the name and contactinformation for the translator.</P><P>The C source code presented in this document is hereby granted tothe public domain.</P><P>Contact <TTCLASS="email">&#60;<AHREF="mailto:beej@piratehaven.org">beej@piratehaven.org</A>&#62;</TT> for more information.</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="index.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="theory.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Beej's Guide to Network Programming</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top">&nbsp;</TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">What is a socket?</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -