没有合适的资源?快使用搜索试试~ 我知道了~
Beej's 网络编程
需积分: 0 2 下载量 3 浏览量
2010-05-21
22:05:52
上传
评论
收藏 515KB PDF 举报
温馨提示
试读
49页
不错网络编程入门书籍 Beej's 网络编程Beej's 网络编程Beej's 网络编程
资源详情
资源评论
资源推荐
Beej’s Guide to Network Programming
Using Internet Sockets
Brian "Beej" Hall
beej@piratehaven.org
Copyright © 1995-2001 by Brian "Beej" Hall
Revision History
Revision Version 1.0.0 August, 1995 Revised by: beej
Initial version.
Revision Version 1.5.5 January 13, 1999 Revised by: beej
Latest HTML version.
Revision Version 2.0.0 March 6, 2001 Revised by: beej
Converted to DocBook XML, corrections, additions.
Revision Version 2.3.1 October 8, 2001 Revised by: beej
Fixed typos, syntax error in client.c, added some stuff to the Q&A section.
Table of Contents
1. Intro............................................................................................................................................................................3
1.1. Audience.........................................................................................................................................................3
1.2. Platform and Compiler...................................................................................................................................3
1.3. Official Homepage .........................................................................................................................................3
1.4. Note for Solaris/SunOS Programmers ...........................................................................................................3
1.5. Note for Windows Programmers....................................................................................................................4
1.6. Email Policy...................................................................................................................................................5
1.7. Mirroring........................................................................................................................................................5
1.8. Note for Translators........................................................................................................................................5
1.9. Copyright and Distribution.............................................................................................................................5
2. What is a socket?.......................................................................................................................................................6
2.1. Two Types of Internet Sockets .......................................................................................................................6
2.2. Low level Nonsense and Network Theory .....................................................................................................7
3. structs and Data Handling ....................................................................................................................................9
3.1. Convert the Natives!.....................................................................................................................................10
3.2. IP Addresses and How to Deal With Them..................................................................................................11
4. System Calls or Bust...............................................................................................................................................12
4.1. socket()–Get the File Descriptor!.............................................................................................................12
4.2. bind()–What port am I on?........................................................................................................................13
4.3. connect()–Hey, you!.................................................................................................................................15
4.4. listen()–Will somebody please call me?.................................................................................................16
4.5. accept()–"Thank you for calling port 3490."...........................................................................................16
4.6. send() and recv()–Talk to me, baby!......................................................................................................18
4.7. sendto() and recvfrom()–Talk to me, DGRAM-style ..........................................................................19
4.8. close() and shutdown()–Get outta my face!..........................................................................................19
4.9. getpeername()–Who are you? .................................................................................................................20
4.10. gethostname()–Who am I?....................................................................................................................20
4.11. DNS–You say "whitehouse.gov", I say "198.137.240.92".........................................................................21
5. Client-Server Background .....................................................................................................................................22
5.1. A Simple Stream Server...............................................................................................................................23
5.2. A Simple Stream Client................................................................................................................................25
5.3. Datagram Sockets.........................................................................................................................................26
6. Slightly Advanced Techniques...............................................................................................................................29
6.1. Blocking .......................................................................................................................................................29
6.2. select()–Synchronous I/O Multiplexing..................................................................................................30
6.3. Handling Partial send()s............................................................................................................................35
6.4. Son of Data Encapsulation...........................................................................................................................36
7. More References......................................................................................................................................................38
7.1. man Pages....................................................................................................................................................38
7.2. Books............................................................................................................................................................39
7.3. Web References............................................................................................................................................40
7.4. RFCs.............................................................................................................................................................40
8. Common Questions.................................................................................................................................................40
9. Disclaimer and Call for Help .................................................................................................................................46
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 structs 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... :-)
1.2. Platform and Compiler
The code contained within this document was compiled on a Linux PC using Gnu’s gcc compiler. It should, however,
build on just about any platform that uses gcc. Naturally, this doesn’t apply if you’re programming for Windows–see
the section on Windows programming, below.
1.3. Official Homepage
This official location of this document is at California State University, Chico, at
http://www.ecst.csuchico.edu/~beej/guide/net/
1
.
1.4. Note for Solaris/SunOS Programmers
When compiling for Solaris or SunOS, you need to specify some extra command-line switches for linking in the
proper libraries. In order to do this, simply add "-lnsl -lsocket -lresolv" to the end of the compile command,
like so:
$ cc -o server server.c -lnsl -lsocket -lresolv
If you still get errors, you could try further adding a "-lxnet" to the end of that command line. I don’t know what
that does, exactly, but some people seem to need it.
Another place that you might find problems is in the call to setsockopt(). The prototype differs from that on my
Linux box, so instead of:
int yes=1;
3
Beej’s Guide to Network Programming
enter this:
char yes=’1’;
As I don’t have a Sun box, I haven’t tested any of the above information–it’s just what people have told me through
email.
1.5. Note for Windows Programmers
I have a particular dislike for Windows, and encourage you to try Linux, BSD, or Unix instead. That being said, you
can still use this stuff under Windows.
First, ignore pretty much all of the system header files I mention in here. All you need to include is:
#include <winsock.h>
Wait! You also have to make a call to WSAStartup() before doing anything else with the sockets library. The code
to do that looks something like this:
#include <winsock.h>
{
WSADATA wsaData; // if this doesn’t work
//WSAData wsaData; // then try this instead
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
You also have to tell your compiler to link in the Winsock library, usually called wsock32.lib or winsock32.lib
or somesuch. Under VC++, this can be done through the Project menu, under Settings.... Click the Link tab,
and look for the box titled "Object/library modules". Add "wsock32.lib" to that list.
Or so I hear.
Finally, you need to call WSACleanup() when you’re all through with the sockets library. See your online help for
details.
Once you do that, the rest of the examples in this tutorial should generally apply, with a few exceptions. For one
thing, you can’t use close() to close a socket–you need to use closesocket(), instead. Also, select() only
works with socket descriptors, not file descriptors (like 0 for stdin).
There is also a socket class that you can use, CSocket. Check your compilers help pages for more information.
To get more information about Winsock, read the Winsock FAQ
2
and go from there.
Finally, I hear that Windows has no fork() system call which is, unfortunately, used in some of my examples.
Maybe you have to link in a POSIX library or something to get it to work, or you can use CreateProcess()
instead. fork() takes no arguments, and CreateProcess() takes about 48 billion arguments. If you’re not up to
4
Beej’s Guide to Network Programming
that, the CreateThread() is a little easier to digest...unfortunately a discussion about multithreading is beyond the
scope of this document. I can only talk about so much, you know!
1.6. Email Policy
I’m generally available to help out with email questions so feel free to write in, but I can’t guarantee a response. I lead
a pretty busy life and there are times when I just can’t answer a question you have. When that’s the case, I usually
just delete the message. It’s nothing personal; I just won’t ever have the time to give the detailed answer you require.
As a rule, the more complex the question, the less likely I am to respond. If you can narrow down your question
before mailing it and be sure to include any pertinent information (like platform, compiler, error messages you’re
getting, and anything else you think might help me troubleshoot), you’re much more likely to get a response. For
more pointers, read ESR’s document, How To Ask Questions The Smart Way
3
.
If you don’t get a response, hack on it some more, try to find the answer, and if it’s still elusive, then write me again
with the information you’ve found and hopefully it will be enough for me to help out.
Now that I’ve badgered you about how to write and not write me, I’d just like to let you know that I fully appreciate
all the praise the guide has received over the years. It’s a real morale boost, and it gladdens me to hear that it is being
used for good! :-) Thank you!
1.7. Mirroring
You are more than welcome to mirror this site, whether publically or privately. If you publically mirror the site and
want me to link to it from the main page, drop me a line at <beej@piratehaven.org>.
1.8. Note for Translators
If you want to translate the guide into another language, write me at <beej@piratehaven.org> and I’ll link to
your translation from the main page.
Feel free to add your name and email address to the translation.
Sorry, but due to space constraints, I cannot host the translations myself.
1.9. Copyright and Distribution
Beej’s Guide to Network Programming is Copyright © 1995-2001 Brian "Beej" Hall.
This guide may be freely reprinted in any medium provided that its content is not altered, it is presented in its
entirety, and this copyright notice remains intact.
Educators are especially encouraged to recommend or supply copies of this guide to their students.
5
剩余48页未读,继续阅读
lijunuuxxx
- 粉丝: 3
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于springboot的医院门诊管理系统源码.zip
- 基于python的垃圾分类系统源码(高分优质项目).zip
- 场景视点偏移改善激光雷达点云分割项目源码(下载即用).zip
- 纸板缺陷检测数据集VOC+YOLO格式1055张1类别.7z
- 基于CarNet实现裂缝检测python源码+文档说明+数据+图片(课程设计)
- 课程设计-基于耐火材料裂缝剥落检测python源码+课件
- 基于OpenCV的视频道路车道检测python源码+文档说明+实验演示+图片+使用方法(高分毕业设计)
- 基于OpenCV的案例:图像边缘、角点和轮廓检测,图像分割,图像增强;图片拼接;运动目标检测,颜色直方图比较,三帧帧差法,抠图
- SmartPlug-html大一笔记
- SmartPlug-proteusdemo
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0