《LInux下Socket编程.pdf》由会员分享,可在线阅读,更多相关《LInux下Socket编程.pdf(24页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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
26、y of its components.Furthermore,only one copy of the hostent structure is allocated per thread,and an application should therefore copy any information that it needs before issuing any other Windows Sockets API calls.typedef struct hostent char FAR*h_name;char FAR FAR*h_aliases;short h_addrtype;shor
27、t h_length;char FAR FAR*h_addr_list;hostent;Rajinder Yadav Copyright 2007 www.devmentor.org TCP Transmission Control Protocol Although TCP can be implemented to work over any transport protocol,its usually synonymous with IP.TCP is a connection-oriented stream protocol(like a telephone call).TCP com
28、munication happens using a handshake process,where each data that is sent is acknowledge by the recipient within the time of TCPs timer value.TCP provides many services such as data reliability,error checking,and flow control.If a data packet is corrupt or lost(not acknowledged),TCP will retransmitt
29、ed the data from the client side automatically.Because the route a packet takes can be many,one packet may arrive before the one sent earlier.As data packets arrive,it is the job of TCP to assemble the packets into the proper order.This is shown below with a factious network topology layout,where th
30、e data packet takes(n)number of hops to get from the source to the destination.On a bigger network like the Internet,there are many routes a data packet can take to arrive at its final destination.Diagram 3 Network Hop Topology TCP Window Any duplicate data packet is silently dropped with no acknowl
31、edgement.TCP controls the flow of transmission by using a“window”that can grow or shrink based on how responsive the(next-hop)node is.If a lot of packets are getting dropped because the receivers buffer is full,TCP will slow down the rate of transmission by decreasing the size of the“window”.Usually
32、 TCP will negotiate with the receiver for such things as the maximum transmission unit.If the sizes are different,such that the receiver node accepts a smaller sized packet,the out-bound packet will be fragmented by TCP.Again,the data packet“segments”can arrive at different times by taking different
33、 routes.So its the job of TCP at the destination end to reassemble the original data packet as the segments arrive.The data is placed into a larger buffer by TCP to be read by the application.Data is streamed out from the client side and streamed in at the server side.This is why TCP is called a str
34、eam bases protocol,its work just like a file I/O read and write operation,which is what provides synchronous data access.Rajinder Yadav Copyright 2007 www.devmentor.org Byte Ordering There are two types of memory byte ordering in use today that are very much machine dependent.They are known as littl
35、e-endian and big-endian,because of this we have to be very careful how we interpret numerical data.If we do not take into account the endiannes,the numerical data we read will be corrupt.Little-Endian:the LSB(Least Significant Byte)is at the lowest address.Big-Endian:The MSB(Most Significant Byte)is
36、 at the lowest address.Diagram 4 Address Byte Order Generally when working with numeric data,one needs to convert from machine(host)byte order to network byte order when sending data(write-op),and then from network byte order to machine byte order when retrieving data(read-op).The APIs to make the c
37、onversion are:htons()and htonl()/host to network uint16_t htons(uint16_t host16bitvalue);uint32_t htonl(uint32_t host32bitvalue);ntohs()and ntohl()/network to host uint16_t ntohs(uint16_t net16bitvalue);unit32_t ntohl(unit32_t net32bitvalue);Note:network byte order is in Big-Endian,CPU based on the
38、x86 architecture use Little-Endian byte order.Sockets We are now ready to talk about what a socket is.A socket is made up of 3 identifying properties:Protocol Family,IP Address,Port Number For TCP/IP Sockets:The protocol family is AF_INET(Address Family Internet)The IP Address identifies a host/serv
39、ice machine on the network Port defines the Service on the machine were communicating to/from Rajinder Yadav Copyright 2007 www.devmentor.org TCP Ports The port numbers from 0 to 255 are well-known ports,and the use of these port numbers in your application is highly discouraged.Many well-known serv
40、ices you use have assigned port numbers in this range.Service Name Port Number ftp 21 telenet 23 www-http 80 irc 194 Table 1 Well Known Ports In recent years the range for assigned ports managed by IANA(Internet Assigned Numbers Authority)has been expanded to the range of 0 1023.To get the most rece
41、nt listing of assigned port numbers,you can view the latest RFC 1700 at:http:/www.faqs.org/rfcs/rfc1700.html In order for 2 machines to be able to communicate they need to be using the same type of sockets,both have to be TCP or UDP.On Windows,the socket definition is defined in the header file or.O
42、ur program will be using Winsock 2.2 so we will need to include and link with WS2_32.lib Opening and closing a socket To create a TCP/IP socket,we use the socket()API.SOCKET hSock=socket(AF_INET,SOCK_STREAM,IPPROTO_IP);If the socket API fails,it will return a value of INVALID_SOCKET,otherwise it wil
43、l return a descriptor value that we will need to use when referring to this socket.Once we are done with the socket we must remember to call the closesocket()API.closesocket(hSock);Note:On Unix/Linux the return type for socket()is an int,while the API to close the socket is close(socket_discriptor);
44、Before we can begin to use any socket API in Windows we need to initialize the socket library,this is done by making a call to WSAStartup().This step is not required on Unix/Linux.Rajinder Yadav Copyright 2007 www.devmentor.org Initializing Winsock/Initialize WinSock2.2 DLL/low-word=major,hi-word=mi
45、nor WSADATA wsaData=0;WORD wVer=MAKEWORD(2,2);int nRet=WSAStartup(wVer,&wsaData);Before we exit our program we need to release the Winsock DLL with the following call./Release WinSock DLL WSACleanup();With the basics out of the way,the process of preparing the client and server application is simila
46、r,but differ slightly in their steps.We will talk about how to write the server code first.Socket Address Structure For IPv4 the socket structure is defined as:struct sockaddr u_short sa_family;/*address family*/char sa_data14;/*up to 14 bytes of direct address*/;This is the generic structure that m
47、ost socket APIs accept,but the structure you will work with is sockaddr_in(socket address internet).struct sockaddr_in short sin_family;u_short sin_port;struct in_addr sin_addr;char sin_zero8;Note:sockaddr_in and the more generic sockaddr struct are the same size.Padding is used to maintain the size
48、 by sin_zero8.You will need to typecast between the two in your program.Rajinder Yadav Copyright 2007 www.devmentor.org struct in_addr found inside sockaddr_in is a union defined as:struct in_addr union struct u_char s_b1,s_b2,s_b3,s_b4;S_un_b;struct u_short s_w1,s_w2;S_un_w;u_long S_addr;S_un;#defi
49、ne s_addr S_un.S_addr#define s_host S_un.S_un_b.s_b2#define s_net S_un.S_un_b.s_b1#define s_imp S_un.S_un_w.s_w2#define s_impno S_un.S_un_b.s_b4#define s_lh S_un.S_un_b.s_b3;This structure holds the IP address which can be accessed in many ways.You can use the inet_addr()API to convert a IP dotted-n
50、umerical string into a 32bit IP address and assign it to the s_addr member.The use of this API is shown when we discuss the client code.To do an opposite conversion from network to IP string,use the inet_ntoa()API.TCP Server The steps to get a server up and running are shown below(read from top to b