?? faq.html
字號:
<HTML><HEAD><TITLE>Common Questions</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="More References"HREF="reference.html"><LINKREL="NEXT"TITLE="Disclaimer and Call for Help"HREF="conclusion.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="reference.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="conclusion.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="sect1"><H1CLASS="sect1"><ANAME="faq">8. Common Questions</A></H1><DIVCLASS="qandaset"><DL><DT>Q: <AHREF="faq.html#AEN1346">Where can I get those header files?</A></DT><DT>Q: <AHREF="faq.html#AEN1352">What do I do when <TTCLASS="function">bind()</TT> reports"Address already in use"?</A></DT><DT>Q: <AHREF="faq.html#AEN1364">How do I get a list of open sockets on thesystem?</A></DT><DT>Q: <AHREF="faq.html#AEN1376">How can I view the routing table?</A></DT><DT>Q: <AHREF="faq.html#AEN1384">How can I run the client and server programs if I onlyhave one computer? Don't I need a network to write networkprogram?</A></DT><DT>Q: <AHREF="faq.html#AEN1398">How can I tell if the remote side has closedconnection?</A></DT><DT>Q: <AHREF="faq.html#AEN1405">How do I implement a "ping" utility? What is ICMP?Where can I find out more about raw sockets and<TTCLASS="constant">SOCK_RAW</TT>?</A></DT><DT>Q: <AHREF="faq.html#AEN1412">How do I build for Windows?</A></DT><DT>Q: <AHREF="faq.html#AEN1419">How do I build for Solaris/SunOS? I keep getting linkererrors when I try to compile!</A></DT><DT>Q: <AHREF="faq.html#AEN1425">Why does <TTCLASS="function">select()</TT> keep falling outon a signal?</A></DT><DT>Q: <AHREF="faq.html#AEN1444">How can I implement a timeout on a call to<TTCLASS="function">recv()</TT>?</A></DT><DT>Q: <AHREF="faq.html#AEN1461">How do I encrypt or compress the data before sending itthrough the socket?</A></DT><DT>Q: <AHREF="faq.html#AEN1491">What is this "<TTCLASS="constant">PF_INET</TT>" I keepseeing? Is it related to<TTCLASS="constant">AF_INET</TT>?</A></DT><DT>Q: <AHREF="faq.html#AEN1500">How can I write a server that accepts shell commandsfrom a client and executes them?</A></DT><DT>Q: <AHREF="faq.html#AEN1545">I'm sending a slew of data, but when I<TTCLASS="function">recv()</TT>, it only receives 536 bytes or 1460 bytes ata time. But if I run it on my local machine, it receives all the dataat the same time. What's going on?</A></DT><DT>Q: <AHREF="faq.html#AEN1558">I'm on a Windows box and I don't have the<TTCLASS="function">fork()</TT> system call or any kind of <TTCLASS="type">structsigaction</TT>. What to do?</A></DT><DT>Q: <AHREF="faq.html#AEN1573">How do I send data securely with TCP/IP usingencryption?</A></DT><DT>Q: <AHREF="faq.html#AEN1579">I'm behind a firewall--how do I let people outside thefirewall know my IP address so they can connect to mymachine?</A></DT></DL><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1346"></A><B>Q: </B>Where can I get those header files?</P></DIV><DIVCLASS="answer"><P><B>A: </B>If you don't have them on your system already, youprobably don't need them. Check the manual for your particularplatform. If you're building for Windows, you only need to<TTCLASS="computeroutput">#include<winsock.h></TT>.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1352"></A><B>Q: </B>What do I do when <TTCLASS="function">bind()</TT> reports"Address already in use"?</P></DIV><DIVCLASS="answer"><P><B>A: </B>You have to use <TTCLASS="function">setsockopt()</TT> with the<TTCLASS="constant">SO_REUSEADDR</TT> option on the listening socket. Checkout the <AHREF="syscalls.html#bind">section on<TTCLASS="function">bind()</TT></A> and the <AHREF="advanced.html#select">section on <TTCLASS="function">select()</TT></A> for anexample.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1364"></A><B>Q: </B>How do I get a list of open sockets on thesystem?</P></DIV><DIVCLASS="answer"><P><B>A: </B>Use the <BCLASS="command">netstat</B>. Check the<BCLASS="command">man</B> page for full details, but you should get somegood output just typing:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="screen"> <TTCLASS="prompt">$</TT> <BCLASS="command">netstat</B></PRE></TD></TR></TABLE><P>The only trick is determining which socket is associated withwhich program. <TTCLASS="computeroutput">:-)</TT></P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1376"></A><B>Q: </B>How can I view the routing table?</P></DIV><DIVCLASS="answer"><P><B>A: </B>Run the <BCLASS="command">route</B> command (in<TTCLASS="filename">/sbin</TT> on most Linuxes) or the command<BCLASS="command">netstat -r</B>.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1384"></A><B>Q: </B>How can I run the client and server programs if I onlyhave one computer? Don't I need a network to write networkprogram?</P></DIV><DIVCLASS="answer"><P><B>A: </B>Fortunately for you, virtually all machines implement aloopback network "device" that sits in the kernel and pretends to be anetwork card. (This is the interface listed as"<TTCLASS="computeroutput">lo</TT>" in the routing table.)</P><P>Pretend you're logged into a machine named"<TTCLASS="computeroutput">goat</TT>". Run the client in one windowand the server in another. Or start the server in the background("<BCLASS="command">server &</B>") and run the client in the samewindow. The upshot of the loopback device is that you can either<BCLASS="command">client goat</B> or <BCLASS="command">client localhost</B>(since "<TTCLASS="computeroutput">localhost</TT>" is likely defined inyour <TTCLASS="filename">/etc/hosts</TT> file) and you'll have the clienttalking to the server without a network!</P><P>In short, no changes are necessary to any of the code to make itrun on a single non-networked machine! Huzzah!</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1398"></A><B>Q: </B>How can I tell if the remote side has closedconnection?</P></DIV><DIVCLASS="answer"><P><B>A: </B>You can tell because <TTCLASS="function">recv()</TT> willreturn <TTCLASS="constant">0</TT>.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1405"></A><B>Q: </B>How do I implement a "ping" utility? What is ICMP?Where can I find out more about raw sockets and<TTCLASS="constant">SOCK_RAW</TT>?</P></DIV><DIVCLASS="answer"><P><B>A: </B>All your raw sockets questions will be answered in W.Richard Stevens' UNIX Network Programming books. See the <AHREF="reference.html#books">books</A> section of this guide.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1412"></A><B>Q: </B>How do I build for Windows?</P></DIV><DIVCLASS="answer"><P><B>A: </B>First, delete Windows and install Linux or BSD.<TTCLASS="computeroutput">};-)</TT>. No, actually, just see the <AHREF="intro.html#windows">section on building forWindows</A> in the introduction.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1419"></A><B>Q: </B>How do I build for Solaris/SunOS? I keep getting linkererrors when I try to compile!</P></DIV><DIVCLASS="answer"><P><B>A: </B>The linker errors happen because Sun boxes don'tautomatically compile in the socket libraries. See the <AHREF="intro.html#solaris">section on building for Solaris/SunOS</A> in theintroduction for an example of how to do this.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1425"></A><B>Q: </B>Why does <TTCLASS="function">select()</TT> keep falling outon a signal?</P></DIV><DIVCLASS="answer"><P><B>A: </B>Signals tend to cause blocked system calls to return<TTCLASS="constant">-1</TT> with <TTCLASS="parameter"><I>errno</I></TT> set to<TTCLASS="constant">EINTR</TT>. When you set up a signal handler with<TTCLASS="function">sigaction()</TT>, you can set the flag<TTCLASS="constant">SA_RESTART</TT>, which is supposed to restart the systemcall after it was interrupted.</P><P>Naturally, this doesn't always work.</P><P>My favorite solution to this involves a<TTCLASS="computeroutput">goto</TT> statement. You know thisirritates your professors to no end, so go for it!</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="programlisting"> select_restart: if ((err = select(fdmax+1, &readfds, NULL, NULL, NULL)) == -1) { if (errno == EINTR) { // some signal just interrupted us, so restart goto select_restart; } // handle the real error here: perror("select"); } </PRE></TD></TR></TABLE><P>Sure, you don't <EM>need</EM> to use<TTCLASS="computeroutput">goto</TT> in this case; you can use otherstructures to control it. But I think the<TTCLASS="computeroutput">goto</TT> statement is actuallycleaner.</P></DIV></DIV><DIVCLASS="qandaentry"><DIVCLASS="question"><P><ANAME="AEN1444"></A
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -