LInux下Socket编程.pdf





《LInux下Socket编程.pdf》由会员分享,可在线阅读,更多相关《LInux下Socket编程.pdf(24页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Rajinder Yadav Copyright 2007 www.devmentor.org Client/Server Programming with TCP/IP Sockets Author:Rajinder Yadav Date:Sept 9,2007 Revision:Mar 11,2008 Web:http:/devmentor.org Email:rajinderdevmentor.org Table of Content Networks.2 Diagram 1 Communication Link.2 IPv4 Internet Protocol.2 IP Address
2、.2 IP Classes.3 Subnets.3 IP and the Hardware.4 IP and DNS.4 loopback.5 IP from hostname.5 hostname from IP.5 hostent structure.5 TCP Transmission Control Protocol.6 Diagram 3 Network Hop Topology.6 TCP Window.6 Byte Ordering.7 Little-Endian:the LSB(Least Significant Byte)is at the lowest address.7
3、Big-Endian:The MSB(Most Significant Byte)is at the lowest address.7 Diagram 4 Address Byte Order.7 htons()and htonl().7 ntohs()and ntohl().7 Sockets.7 TCP Ports.8 Table 1 Well Known Ports.8 Opening and closing a socket.8 Initializing Winsock.9 Socket Address Structure.9 TCP Server.10 Naming the sock
4、et.11 TCP Client.13 TCP State Diagram.14 Connection Termination.14 Running The Programs.16 Network Utilities.17 ping.17 netstat.17 Server Socket Code.18 Client Socket Code.22 Rajinder Yadav Copyright 2007 www.devmentor.org Introduction In this article we will look at how to program using sockets by
5、implementing an echo server along with a client that we will use to send and receive string messages.I will start off by giving a quick introduction to TCP/IP fundamentals and then explain how sockets fit into the picture.I am going to assume you already understand basic network concepts.When you ge
6、t done reading this article you should be armed with sufficient information to be able to investigate other concepts in more detail on your own.Sample code can be downloaded for this article from my website for Windows and Linux.I have also included all the Winsock source code in print at the end of
7、 the article for others.Networks Most network applications can be divided into two pieces:a client and a server.A client is the side that initiates the communication process,whereas the server responds to incoming client requests.Diagram 1 Communication Link There are numerous network protocols,such
8、 as Netbios,RPC(Remote Procedure Call),DCOM,Pipes,IPC(Inter-process Communication)that can be used for the Communication Link.We will only look at TCP/IP here.In particular we will look at sockets IPv4 since this is widely implemented by many socket vendors.IPv4 Internet Protocol The current version
9、 for IP supported by many modern networks is version 4,the next generation of IP is version 6(IPv6)which is not widely supported as of this writting.IP is both a network addressing protocol and a network transport protocol.IP is a connection-less protocol that provides unreliable service for data co
10、mmunication.Most of the properties of data communication such as transmission reliability,flow control and error checking are provided by TCP which we will look at shortly.Most people know IP in its basic form as a quad dotted-numerical string,such as“192.168.1.1”.Each integer value separated by a d
11、ot can have a value from 0 to 255(8 bits).Thus IPv4 is a 32 bit unsigned integer values.IP Address Diagram 2a IP Address Rajinder Yadav Copyright 2007 www.devmentor.org IP Classes The IP address is made of a network address and a host address.There can be many sub-networks connect together,so a netw
12、ork address help routers to redirect data packet to the proper destination network,from there the data packet is sent to the final destination host PC.The 4 IP classes are:Class Leftmost bit Start Address End Address A 0 xxx 0.0.0.0 127.255.255.255 B 10 xx 128.0.0.0 191.255.255.255 C 110 x 192.0.0.0
13、 223.255.255.255 D 1110 224.0.0.0 223.255.255.255 E 1111 240.0.0.0 255.255.255.255 Class A network.local.local.local (small network,large hosts)Class B work.local.local (medium network,medium hosts)Class C work.local (large network,small hosts)Class D work(multicast to many hosts )Class E reserved (
14、*)A special type of IP address is the limited broadcast address 255.255.255.255(*)IP Mapping:Class A,B,C 1-to-1,Class D is 1-to-many Subnets An IP address can have the host address subdivided into a subnet part and a host part using a subnet mask.The subnet mask is also a 32bit value,the bits for th
15、e network and subnet will be set to 1,this way from the mask and IP class we can determine the network address,the subnet address and the host number.Diagram 2b IP Address with Subnet There IP address can be classified as unicast,broadcast and multicast.A unicast address has a 1-to-1 relationship.A
16、broadcast can apply to:a)all hosts on a network,b)all hosts on a subnet,and c)all hosts on all subnets.For multicast,a host needs to belong to the multicast group in order to receive a packet.The main thing to note is that a broadcast or multicast packet is never forwarded by a router/gateway outsid
17、e the network.Rajinder Yadav Copyright 2007 www.devmentor.org IP and the Hardware IP is used to identify a PC on the network.This is done for us at the hardware level by the NIC(Network Interface Card)like a PCs Ethernet card or a Router.A machine NIC or Ethernet uses ARP(Address Resolution Protocol
18、)to convert an IP address into a network address that can be understood by routers and gateways.Likewise the hardware layer use RARP(Reverse Address Resolution Protocol)to convert a hardware network address at the MAC(Media Access Control)level into an IP address.As data packets move around the netw
19、ork,the hardware layer(router)is checking if a packet is meant for it to process by checking the MAC address.If its not the data packet is transmitted down the line.If the data packet is meant for a host in the network,then the IP address is checked using a lookup table to determine which host PC to
20、 send the data packet off to in the network.We really dont need to be concerned with underlying details as all this is handled for us.IP and DNS Most people dont use IP directly,its not an easy way to remember addresses,so to help humans the DNS(Domain Name System)maps a hostname strings like“”into
21、an IP address.If youre developing any type of network application its better to use the DSN name format to communicate with another computer.DSN mapping can be easily changed(in the router table)thus allowing one to redirect network traffic to another destination.Host File On your local PC the DNS m
22、apping entries are found in the host file.On Windows NT,2K,XP the file“hosts”is located at:%WINDOWS%system32driversetc The host files(shown in blue)contains a space separated IP address and hostname.All this info ends up in the router table,where network traffic control occurs.127.0.0.1 localhost 19
23、2.168.1.106 192.168.1.105 192.168.1.100 Rajinder Yadav Copyright 2007 www.devmentor.org loopback Note:the loopback address of“127.0.0.1”also known as“localhost”is the IP address used when communicating with other process running on the same PC.This is how we will test our client and server applicati
24、on,they will run on the same“local”host PC.IP from hostname The gethostbyname function retrieves host information corresponding to a host name from a host database.struct hostent*FAR gethostbyname(const char*name);hostname from IP The gethostbyaddr function retrieves the host information correspondi
25、ng to a network address.struct HOSTENT*FAR gethostbyaddr(const char*addr,int len,int type);hostent structure msdn The hostent structure is used by functions to store information about a given host,such as host name,IP address.An application should never attempt to modify this structure or to free an
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- LInux Socket 编程

限制150内