`
阿尔萨斯
  • 浏览: 4170245 次
社区版块
存档分类
最新评论

网络程序设计 Sockets

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
关于Socket编程,在《Linux从入门到精通》里有简单的介绍,更详细的可以参考
《UNIX网络编程 卷1:联网的API:套接字与XTI 第2版》清华影印版,其中还讲了
线程(Thread)编程。极好的参考书,可惜没人把它翻译过来。
胡淑瑜翻译了一篇“网络编程”,我把它收集进来了。
如有更新,请参考胡先生的个人主页
________________________________________________________________________________|                                   版权声明                                   ||                                                                              ||  1、本文可以转载、修改及引用,但请保留本声明和其后所付英文原文。             ||  2、任何情况下,作者和译者姓名不可删除。                                     ||  3、任何情况下,本文不可进入商业领域。                                       ||                                                                              ||                                                  胡淑瑜                      ||                                        (husuyu@linux.cqi.com.cn)             ||                                                                              ||                                                 1998年11月                   ||______________________________________________________________________________|第59章目录   网络程序设计      端口(Ports)和套接字(Sockets)      套接字程序设计         socket()系统调用(System Call)         bind()系统调用         listen()系统调用         accept()系统调用         setsockopt和getsockopt系统调用         connect()系统调用      程序清单59.1服务器端(Server Side)面向套接字(socket-oriented)协议                 程序清单59.2客户端函数(The lient Side function)         无连接(Connectionless)套接字程序设计      程序清单59.3服务端      注意      记录(Record)和文件锁定(Locking)      进程间通信      小结-------------------------------------------------------------------------------                                   --第59章--                                                             网络程序设计                                      作者 Kamran Husain,Tim Parker译者 胡淑瑜本章内容   端口和套接字   套接字程序设计   记录和文件锁定   进程间通信   阅读本章需你具有如下网络程序设计的基本概念   端口和套接字   记录和文件锁定   进程间通信      本文不可能在几页之内就能与你说清网络程序设计的全部内容.事实上,一本第一卷就有800页的专门描述网络程序设计的参考书是最有用的.如果你真想进行网络编程的话,你需要具有编译器,TCP/IP和网络操作系统的大量经验.另外,你还需有极大的耐心.      欲知TCP/IP详细内容,请参见Tim Parker所著之> (Sams Publish-ing).                                  端口和套接字                                    网络程序设计全靠套接字接受和发送信息.尽管套接字这个词好象显得有些神秘,但其实这个概念极易理解.   大多数网络应用程序使用两个协议:传输控制协议(TCP)和用户数据包协议(UDP).他们都使用一个端口号以识别应用程序.端口号为主机上所运行之程序所用,这样就可以通过号码象名字一样来跟踪每个应用程序.端口号让操作系统更容易的知道有多少个应用程序在使用系统,以及哪些服务有效.   理论上,端口号可由每台主机上的管理员自由的分配.但为了更好的通信通常采用一些约定的协议.这些协议使能通过端口号识别一个系统向另一个系统所请求的服务的类型.基于如此理由,大多数系统维护一个包含端口号及它们所提供哪些服务的文件.   端口号被从1开始分配.通常端口号超出255的部分被本地主机保留为私有用途.1到255之间的号码被用于远程应用程序所请求的进程和网络服务.每个网络通信循环地进出主计算机的TCP应用层.它被两个所连接的号码唯一地识别.这两个号码合起来叫做套接字.组成套接字的这两个号码就是机器的IP地址和TCP软件所使用的端口号.   因为网络通讯至少包括两台机器,所以在发送和接收的机器上都存在一个套接字.由于每台机器的IP地址是唯一的,端口号在每台机器中也是唯一的,所以套接字在网络中应该是唯一的.这样的设置能使网络中的两个应用程序完全的基于套接字互相对话.   发送和接收的机器维护一个端口表,它列出了所有激活的端口号.两台机器都包括一个进程叫做绑定,这是每个任务的入口,不过在两台机器上恰恰相反.换句话说,如果一台机器的源端口号是23而目的端口号被设置成25,那么另一台机器的源端口号设置成25目的端口号设置成23.                                  套接字程序设计                     Linux支持伯克利(BSD)风格的套接字编程.它同时支持面向连接和不连接类型的套接字.在面向连接的通讯中服务器和客户机在交换数据之前先要建立一个连接.再不连接通讯中数据被作为信息的一部分被交换.无论那一种方式,服务器总是最先启动,把自己绑定(Banding)在一个套接字上,然后侦听信息.服务器究竟怎样试图去侦听就得依靠你编程所设定的连接的类型了.   你需要了解一些系统调用         socket()            bind()            listen()            accept()            setsockopt()和getsockopt()            connect()            sendto()            recvfrom()         我们将在以下的例子中使用这些系统调用.                                     socket()系统调用                  socket()系统调用为客户机或服务器创建一个套接字,套接字函数在如下定义:   #include<sys>      #include<sys>      int socket(int family, int type, int protocol)           在Linux中family=AF_UNIX.type可以是SOCK_STREAM它是可靠的虽然通讯速度较慢,也可以是SOCK_DGRAM它通讯速度较快但不可靠.如果type=SOCK_STREAM那么protocol=IPPROTO_TCP.如果type=SOCK_DGRAM那么protocol=IPPROTO_UDP.   如果出错,函数将返回-1.否则返回一个套接字描述符你可以在程序后面的调用中通过套接字描述符使用这个套接字.   套接字创建时没有指定名字.客户机用套接字的名字读写它.这就是如下绑定函数所要做之事.                                  bind()系统调用                  bind()系统调用为没有名字的套接字分配一个名字.绑定函数是这样定义的:   #include<sys>      #include<sys>      int bind(int sockfd, struct sockaddr *saddr, int addrlen)      第一个参数是一个套接字描述符.第二个参数是名字所用的一个结构.第三个参数是结构的大小.   现在你已经为你的客户机或服务器限定了一个地址,你可以connect()服务器或是在服务器上侦听了.如果你的程序是一个服务器,那么它把自己设置为侦听然后等待连接.让我们来看看哪些函数能让你试图这样做.                                  listen()系统调用                  listen()系统调用被服务器所使用.下面有它的定义:      #include<sys>      #include<sys>      int listen(int sockfd, int backlog);      sockfd是套接字描述符.backlog是在一时间内尚未被决定是否拒绝的连接的号码.一般使用标准值5.如发生错误则返回值小于1.   如果这个调用成功,你就已经可以接受连接了.                                     accept()系统调用                  accept()调用被服务器用于接受任何从客户机connect()调用所引入的信息.必须明白的是,如果没有接受到连接这个函数将不返回任何值.它是象这样定义的:   #include<sys>      #include<sys>      int accept(int sockfd, struct sockaddr *peeraddr, int addrlen)      除peeraddr指向发出连接请求的客户机的信息外,其它参数和bind()系统调用的相同.在信息引入的基础上,peeraddr所指向的结构的域被填上相应的值.                      setsockopt()和getsockopt()系统调用       Linux所提供的socket库含有一个错误(bug).此错误表现为你不能为一个套接字重新启用同一个端口号,即使在你正常关闭该套接字以后.例如,比方说,你编写一个服务器在一个套接字上等待的程序.服务器打开套接字并在其上侦听是没有问题的.无论如何,总有一些原因(不管是正常还是非正常的结束程序)使你的程序需要重新启动.然而重启动后你就不能把它绑定在原来那个端口上了.从bind()系统调用返回的错误代码总是报告说你试图连接的端口已经被别的进程所绑定.   问题就是Linux内核在一个绑定套接字的进程结束后从不把端口标记为未用.在大多数UNIX系统中,端口可以被一个进程重复使用,甚至可以被其它进程使用.   在Linux中绕开这个问题的办法是,但套接字已经打开但尚未有连接的时候用setsockopt()系统调用在其上设定选项(options).setsockopt()调用设置选项而getsockopt()从给定的套接字取得选项.   这里是这些调用的语法:      #include<sys>      #include<sys>      int getsockopt(int sockfd, int level, int name   , char *value, int *optlen)      int setsockopt(int sockfd, int level, int name   , char *value, int *optlen)      sockfd必须是一个已打开的套接字.level是函数所使用的协议标准(protocol level)(TCP/IP协议使用IPPROTO_TCP,套接字标准的选项实用SOL_SOCKET),选项的名称(name)在套接字说明书中(man page)有详细说明.*value指向为getsockopt()函数所获取的值或setsockopt()函数所设置的值的地址.optlen指针指向一个整数,该整数包含参数以字节计算的长度.其值被getsockopt()设置且其值必须被程序员设定当使用一个经由setsockopt().   选项的所有细节可以在使用手册中setsockopt的第二节(setsockopt(2))找到.   现在我们再回到Linux的错误上来.当你打开一个套接字时必须同时用下面的代码段来调用setsockopt()函数:   #ifdef LINUX      opt = 1; len = sizeof(opt);      setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&amp;opt   ,&amp;len);           #endif      只有当你想让程序不光是在Linux系统下使用时,#ifdef和#endif描述才是必须的.有些UNIX系统可能不支持或不需要SO_REUSEADDR标志.                                  connect()系统调用                  connect()调用被在面向连接的系统中客户机连接服务器时使用.connect()调用必须被用在bind()调用之后.它是这样定义的:   #include<sys>      #include<sys>      int connect(int sockfd, struct sockaddr *servs   addr, int addrlen)         除servsaddr外所有的参数都和bind调用相同,servsaddr指向客户机所连接的服务器的信息.当接到请求后,accept调用为服务器创建一个新的套接字.然后服务器就可以fork()一个新进程然后再去等待其它连接.在服务器端你可以象程序清单59.1所显示的那样编写代码                                  程序清单59.1                                           面向套接字协议的服务器端           #include    <sys>#include    <sys>#include <linux>#include <linux>#define MY_PORT 6545main(int argc, char *argv[]){int sockfd, newfd;int cpid; /* child id */struct sockaddr_in servaddr;struct sockaddr_in clientInfo;if ((sockfd = socket(AF_INET, SOCK_STREAM, 0) #include    <sys>#include <linux>#include <linux>#define MY_PORT 6545#define MY_HOST_ADDR "204.25.13.1"int getServerSocketId(){    int fd, len;    struct sockaddr_in   unix_addr;                /* create a Unix domain stream socket */    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) #include    <sys>#include <linux>#include <linux>#define MY_PORT 6545#define MAXM   4096char mesg[MAXM];main(int argc, char *argv[]){int sockfd, newfd;int cpid; /* child id */struct sockaddr_in servaddr;struct sockaddr_in clientInfo;if ((sockfd = socket(AF_INET, SOCK_STREAM, 0) #include <sys>int sendto((int sockfd, const void *message__,  /* the pointer to message */ int  length, /* of message */ unsigned  int type, /* of routing, leave 0 */ const struct sockaddr * client, /* where to send it */ int length ); /* of sockaddr */   注意:如果你使用的是BSD系统,请使用sendto()系统调用,不要使用sendmsg(),因为sendto()性能更好.   如出错则返回-1,不过仅能检查出本地错误.      recvfrom()系统调用是这样定义的:   #include <sys>#include <sys>int recvfrom(int sockfd, const void *message__,  /* the pointer to message */ int  length, /* of message */ unsigned  int flags, /* of routing, leave 0 */ const struct sockaddr * client, /* where to send it */ int length ); /* of sockaddr */   如果一个信息大得缓冲区都放不下,那么附加信息将被砍掉.该调用可以立即返回,也可以永久的等待.这取决于你把flags设置成什么类型.你甚至可以设置超时(timeout)值.在说明书(man pages)中可以找到recvfrom的更多信息.   在此你已学会了如何利用Linux的性能上的优点设计网络应用程序的基本知识.我们不打算再进一步的描述更复杂的网络编程了.获得更多细节信息的一个极好的起点是参考W. Richard Stevens 的&gt;(Prentice Hall, 1990).此书乃众多大学所使用的经典教材,内容极为详尽.                                  记录和文件锁定                                     当两个进程共享一个文件时,这之中存在一件非常危险的事情.如果一个进程改变了文件目录那么必然影响到另一个进程.基于此理由,大多数操作系统采用一个互斥原则(mutuallyexclusive principle):当一个进程拥有一个文件时,其它进程就不能再碰这个文件.这叫做文件锁定.   这个技术非常容易实现.通常所发生的事是,所谓"锁定文件"就是创建一个和源文件名同名的文件再加上.lock扩展名.这就告诉其它进程这个文件不能再碰了.Linux假脱机打印系统以及UUCP就是这样实现文件锁定的.这可能是一种粗暴的方法,但编程上非常简单.   不幸的是,你有几个进程要同时迅速的处理同一条信息时,这项技术对你并不实用.因为等待文件打开和关闭所产生的延时将变得很长.同样一个进程如不能正确的释放文件,其它进程将会挂在那里一直等待下去以获得存取权限.   由于这个原因,通常使用记录锁定.用记录锁定,一个大文件的一小部分被锁定以防止两个进程同时改变它.如果有必要的话,记录锁定可以让多个进程同时存取相同文件的不同部分记录.当然实现记录锁定编程要比实现文件锁定更复杂.   通常,要实现记录锁定你需要用到文件偏移量或是到文件起始处的字符数.在大多数程序中,一个范围内的字符被锁定.程序记录锁定范围的起始处和长度,并保存在其它进程能查询到的地方.不管是编写文件锁定还是记录锁定都需要对操作系统有很好的理解.但是并不难.特别是可以从Internet,网络程序设计指导书和BBS上很容易地获得成千的程序.你可以察看这些程序的源代码.                                  进程间通信                                     网络程序设计中通常包括两个或更多的进程将互相对话(interprocess communications).因此进程通信的方法在网络程序设计中是极为重要的.网络程序设计在一些重要的方面不同于一般程序设计通常所使用的方法.一个传统的程序可以通过全局变量或函数调用和不同的模块(甚至同一机器上的其它应用程序)对话.但是在网络上却不行.   网络程序设计的一个重要的目标是保证进程间不互相干涉.否则系统可能被挂起或自锁.因此,进程间必须使用简洁有效的方法进行通信.在此方面,UNIX具有非常显著的健壮性.因为UNIX的许多基本性能如管道,队列等都非常适合网络.   和单个的应用程序代码相比,写进程间通信的代码十分复杂.如果你想写这类程序,可以学习网络程序设计指导书和BBS站点上的例子程序.以了解这些任务是何以完成的.                                    小结                                       很少有人想写网络应用程序,因此进程的细节最好留给那些想写的人.实践和查阅大量的例子程序是开始写网络代码的最好的方法.但是要掌握这门技术却要花许多年时间.-------------------------------英文原文--------------------------------      - 59 -        Network Programming            Ports and Sockets            Socket Programming                The socket() System Call                The bind() System Call                The listen() System Call                The accept() System Call                The setsockopt() and getsockopt() System Calls                The connect() System Call            Listing 59.1. The server side for a socket-oriented protocol.            Listing 59.2. The client side function.                Connectionless Socket Programming            Listing 59.3. The server side.            NOTE            Record and File Locking            Interprocess Communications            Summary- 59 -Network Programmingby Kamran Husain and Tim ParkerIN THIS CHAPTER    Ports and Sockets    Socket Programming    Record and File Locking    Interprocess CommunicationsThis chapter looks at the basic concepts you need for network programming:    Ports and sockets    Record and file locking    Interprocess communicationsIt is impossible to tell you how to program applications for a network in just afew pages. Indeed, the best available reference to network programming takesalmost 800 pages in the first volume alone! If you really want to do networkprogramming, you need a lot of experience with compilers, TCP/IP, and networkoperating systems--and you need a great deal of patience.For details on TCP/IP, check the book Teach Yourself TCP/IP in 14 Days, by TimParker (Sams Publishing).Ports and SocketsNetwork programming relies on the use of sockets to accept and transmitinformation. Although there is a lot of mystique about sockets, the concept isactually simple to understand.Most applications that use the two primary network protocols, TransmissionControl Protocol (TCP) and User Datagram Protocol (UDP) have a port number thatidentifies the application. A port number is used for each different applicationthe machine is handling, so it can keep track of those applications by numbersrather than names. The port number makes it easier for the operating system toknow how many applications are using the system and which services areavailable.In theory, port numbers can be assigned on individual machines by the systemadministrator, but some conventions have been adopted to allow bettercommunications. These conventions enable the port number to identify the type ofservice that one system is requesting from another. For this reason, mostsystems maintain a file of port numbers and their corresponding services.Port numbers are assigned starting from the number 1. Normally, port numbersabove 255 are reserved for the private use of the local machine, but numbersbetween 1 and 255 are used for processes requested by remote applications or fornetworking services.Each network communications circuit into and out of the host computer's TCPapplication layer is uniquely identified by a combination of two numbers,together called the socket. The socket is composed of the IP address of themachine and the port number used by the TCP software.Because at least two machines are involved in network communications, there willbe a socket on both the sending and the receiving machine. Because the IPaddress of each machine is unique and the port numbers are unique to eachmachine, socket numbers are also unique across the network. This setup enablesan application to talk to another application across the network based entirelyon the socket number.The sending and receiving machines maintain a port table that lists all activeport numbers. The two machines involved have reversed entries for each sessionbetween the two, a process called binding. In other words, if one machine hasthe source port number 23 and the destination port number set at 25, the othermachine has its source port number set at 25 and the destination port number setat 23.Socket ProgrammingLinux supports BSD-style socket programming. Both connection-oriented andconnectionless types of sockets are supported. In connection-orientedcommunication, the server and client establish a connection before any data isexchanged. In connectionless communication, data is exchanged as part of amessage. In either case, the server always starts first, binds itself to asocket, and listens to messages. How the server attempts to listen depends onthe type of connection for which you have programmed it.You need to know about a few system calls:    socket()    bind()    listen()    accept()    setsockopt() and getsockopt()    connect()    sendto()       recvfrom()We will cover these system calls in the following examples.The socket() System CallThe socket() system call creates a socket for the client or the server. Thesocket function is defined as shown here:#include<sys>#include<sys>int socket(int family, int type, int protocol)For Linux, you will have family = AF_UNIX. The type is either SOCK_STREAM forreliable, though slower, communications or SOCK_DGRAM for faster, but lessreliable, communications. The protocol should be IPPROTO_TCP for SOCK_STREAM andIPPROTO_UDP for SOCK_DGRAM.The return value from this function is -1 if there was an error; otherwise, it'sa socket descriptor. You will use this socket descriptor to refer to this socketin all subsequent calls in your program.Sockets are created without a name. Clients use the name of the socket to reador write to it. This is where the bind function comes in.The bind() System CallThe bind() system call assigns a name to an unnamed socket. The bind function isdefined like this:#include<sys>#include<sys>int bind(int sockfd, struct sockaddr *saddr, int addrlen)The first item is a socket descriptor. The second is a structure with the nameto use, and the third item is the size of the structure.Now that you have bound an address for your server or client, you can connect()to it or listen on it. If your program is a server, it sets itself up to listenand accept connections. Let's look at the function available for such anendeavor.The listen() System CallThe listen() system call is used by the server. It is defined in the followingway:#include<sys>#include<sys>int listen(int sockfd, int backlog);The sockfd is the descriptor of the socket. The backlog is the number ofconnections that are pending at one time before any are rejected. Use thestandard value of 5 for backlog. A returned value of less than 1 indicates anerror.If this call is successful, you can accept connections.The accept() System CallThe accept() system call is used by a server to accept any incoming messagesfrom clients' connect() calls. Be aware that this function does not return if noconnections are received. It is defined like this:#include<sys>#include<sys>int accept(int sockfd, struct sockaddr *peeraddr, int addrlen)The parameters are the same as those for the bind call, with the exception thatthe peeraddr points to information about the client that is making a connectionrequest. Based on the incoming message, the fields in the structure pointed atby peeraddr are filled out.The setsockopt() and getsockopt() System CallsThe socket libraries provided with Linux include a bug. The symptom of this bugis that you cannot reuse a port number for a socket even if you closed thesocket properly. For example, say you write your own server that waits on asocket. This server opens the socket and listens on it with no problems.However, for some reason (a crash or normal termination), when the program isrestarted, you are not able to bind to the same port. The error codes from thebind() call will always return an error indicating that the port you areattempting to connect to is already bound to another process.The problem is that the Linux kernel never marks the port as unused when theprocess bound to a socket terminates. In most other UNIX systems, the port canbe used again by another invocation of the same or even another process.The way to get around this problem in Linux is to use the setsockopt() systemcall to set the options on a socket when it is opened and before a connection ismade on it. The setsockopt() sets options and the getsockopt()call gets optionsfor a given socket.Here is the syntax for these calls:#include<sys>#include<sys>int getsockopt(int sockfd, int level, int name, char *value, int *optlen)int setsockopt(int sockfd, int level, int name, char *value, int *optlen)The sockfd must be an open socket. The level is the protocol level to use forthe function (IPPROTO_TCP for TCP/IP and SOL_SOCKET for socket level options),and the name of the option is as defined in the socket's man page. The *valuepointer points to a location where a value is stored for getsockopt() or when avalue is read for setsockopt(). The optlen parameter is a pointer to an integercontaining the length of the parameters in bytes; the value is set bygetsockopt() and must be set by the programmer when making a call viasetsockopt().The full man page with details of all the options is found in the man pagesetsockopt(2).Now back to the bug in Linux. When you open a socket, you must also call thesetsockopt() function with the following segment of code:#ifdef LINUXopt = 1; len = sizeof(opt);setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&amp;opt,&amp;len);#endifThe #ifdef and #endif statements are necessary only if you want to port the codeover to systems other than Linux. Some UNIX systems might not support or requirethe SO_REUSEADDR flag.The connect() System CallThe connect() system call is used by clients to connect to a server in aconnection-oriented system. This connect() call should be made after the bind()call. It is defined like this:#include<sys>#include<sys>int connect(int sockfd, struct sockaddr *servsaddr, int addrlen)The parameters are the same as those for the bind call, with the exception thatthe servsaddr points to information about the server that the client isconnecting to. The accept call creates a new socket for the server to work withthe request. This way, the server can fork() off a new process and wait for moreconnections. On the server side of things, you would have code that looks likethat shown in Listing 59.1.Listing 59.1. The server side for a socket-oriented protocol.#include    <sys>#include    <sys>#include <linux>#include <linux>#define MY_PORT 6545main(int argc, char *argv[]){int sockfd, newfd;int cpid; /* child id */struct sockaddr_in servaddr;struct sockaddr_in clientInfo;if ((sockfd = socket(AF_INET, SOCK_STREAM, 0) #include    <sys>#include <linux>#include <linux>#define MY_PORT 6545#define MY_HOST_ADDR "204.25.13.1"int getServerSocketId(){    int fd, len;    struct sockaddr_in   unix_addr;                /* create a Unix domain stream socket */    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) #include    <sys>#include <linux>#include <linux>#define MY_PORT 6545#define MAXM   4096char mesg[MAXM];main(int argc, char *argv[]){int sockfd, newfd;int cpid; /* child id */struct sockaddr_in servaddr;struct sockaddr_in clientInfo;if ((sockfd = socket(AF_INET, SOCK_STREAM, 0) #include <sys>int sendto((int sockfd, const void *message__,  /* the pointer to message */ int  length, /* of message */ unsigned  int type, /* of routing, leave 0 */ const struct sockaddr * client, /* where to send it */ int length ); /* of sockaddr */       NOTE: If you are a BSD user, use the sendto() call, not the sendmsg() call.    The sendto() call is more efficient.       Any errors are indicated by a return value of -1. Only local errors aredetected.The recvfrom() system call is defined as shown here:#include <sys>#include <sys>int recvfrom(int sockfd, const void *message__,  /* the pointer to message */ int  length, /* of message */ unsigned  int flags, /* of routing, leave 0 */ const struct sockaddr * client, /* where to send it */ int length ); /* of sockaddr */If a message is too long to fit in the supplied buffer, the extra bytes arediscarded. The call might return immediately or wait forever, depending on thetype of the flag being set. You can even set timeout values. Check the man pagesfor recvfrom for more information.There you have it--the very basics of how to program applications to takeadvantage of the networking capabilities under Linux. We have not even scratchedthe surface of all the intricacies of programming for networks. A good startingpoint for more detailed information would be UNIX Network Programming, by W.Richard Stevens (Prentice Hall, 1990). This book, a classic, is used in mostuniversities and is by far the most detailed book to date.Record and File LockingWhen two processes want to share a file, the danger exists that one processmight affect the contents of the file, and thereby affect the other process. Forthis reason, most operating systems use a mutually exclusive principle: when oneprocess has a file open, no other process can touch it. This is called filelocking.This technique is simple to implement. What usually happens is that a "lockfile" is created with the same name as the original file but with the extension.lock, which tells other processes that the file is unavailable. This is howmany Linux spoolers, such as the print system and UUCP, implement file locking.It is a brute-force method, perhaps, but effective and easy to program.Unfortunately, this technique is not good when you must have several processesaccess the same information quickly, because the delays waiting for file openingand closing can grow to be appreciable. Also, if one process doesn't release thefile properly, other processes can hang there, waiting for access.For this reason, record locking is sometimes implemented. With record locking, asingle part of a larger file is locked to prevent two processes from changingits contents at the same time. Record locking enables many processes to accessthe same file at the same time, each updating different records within the file,if necessary. The programming necessary to implement record locking is morecomplex than that for file locking, of course.Normally, to implement record locking, you use a file offset, or the number ofcharacters from the beginning of the file. In most cases, a range of charactersis locked; the program has to note the start of the locking region and thelength of it, and then store that information where other processes can examineit.Writing either file-locking or record-locking code requires a good understandingof the operating system but is otherwise not difficult, especially becausethousands of programs are readily available from the Internet, in networkingprogramming books, and on BBSs to examine for sample code.Interprocess CommunicationsNetwork programming always involves two or more processes talking to each other(interprocess communications), so the way in which processes communicate isvitally important to network programmers. Network programming differs from theusual method of programming in a few important aspects. A traditional programcan talk to different modules (or even other applications on the same machine)through global variables and function calls. That doesn't work across networks.A key goal of network programming is to ensure that processes don't interferewith each other. Otherwise, systems can get bogged down or can lock up.Therefore, processes must have a clean and efficient method of communicating.UNIX is particularly strong in this regard, because many of the basic UNIXcapabilities, such as pipes and queues, are used effectively across networks.Writing code for interprocess communications is quite difficult compared tosingle application coding. If you want to write this type of routine, you shouldstudy sample programs from a network programming book or a BBS site to see howthis task is accomplished.SummaryFew people need to write network applications, so the details of the process arebest left to those who want them. Experience and lots of examples are the bestway to begin writing network code, and mastering the skills can take many years.</sys></sys></sys></linux></linux></sys></linux></linux></sys></linux></linux></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></linux></linux></sys></linux></linux></sys></linux></linux></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys></sys>



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics