?? rhl54.htm
字號:
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl54.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl55.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl55.htm">
<LINK REL="Previous" HREF="rhl53.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl53.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl54.htm</H2>
<P ALIGN=LEFT>
<A HREF="rhl53.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl53.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl55.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl55.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E415" >Ports and Sockets</A>
<LI>
<A HREF="#E68E416" >Socket Programming</A>
<UL>
<LI>
<A HREF="#E69E535" >socket()</A>
<LI>
<A HREF="#E69E536" >The bind() System Call</A>
<LI>
<A HREF="#E69E537" >The listen() System Call</A>
<LI>
<A HREF="#E69E538" >The accept() System Call</A>
<LI>
<A HREF="#E69E539" >The connect() System Call</A>
<LI>
<A HREF="#E69E540" >Connectionless Socket Programming</A></UL>
<LI>
<A HREF="#E68E417" >Record and File Locking</A>
<LI>
<A HREF="#E68E418" >Interprocess Communications</A>
<LI>
<A HREF="#E68E419" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E54"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>54</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E54"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Network Programming</B></FONT></CENTER></H2>
<BR>
<P>This chapter will look at the basic concepts you need for network programming:
<BR>
<UL>
<LI>Ports and sockets
<BR>
<BR>
<LI>Record and file locking
<BR>
<BR>
<LI>Interprocess communications
<BR>
<BR>
</UL>
<P>It is impossible to tell you how to program applications for a network in just a few pages. Indeed, the best reference to network programming available takes almost 800 pages in the first volume alone! If you really want to do network programming, you
need a lot of experience with compilers, TCP/IP, network operating systems, and a great deal of patience.
<BR>
<P>For information on details of TCP/IP, check the book Teach Yourself TCP/IP in 14 Days by Tim Parker (Sams).
<BR>
<BR>
<A NAME="E68E415"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Ports and Sockets</B></FONT></CENTER></H3>
<BR>
<P>Network programming relies on the use of sockets to accept and transmit information. Although there is a lot of mystique about sockets, the concept is actually very simple to understand.
<BR>
<P>Most applications that use the two primary network protocols, Transmission Control Protocol (TCP) or User Datagram Protocol (UDP) have a port number that identifies the application. A port number is used for each different application the machine is
handling, so it can keep track of them by numbers instead of names. The port number makes it easier for the operating system to know how many applications are using the system and which services are available.
<BR>
<P>In theory, port numbers can be assigned on individual machines by the system administrator, but some conventions have been adopted to allow better communications. This convention enables the port number to identify the type of service that one system is
requesting from another. For this reason, most systems maintain a file of port numbers and their corresponding services.
<BR>
<P>Port numbers are assigned starting from the number 1. Normally, port numbers above 255 are reserved for the private use of the local machine, but numbers between 1 and 255 are used for processes requested by remote applications or for networking
services.
<BR>
<P>Each network communications circuit that goes into and out of the host computer's TCP application layer is uniquely identified by a combination of two numbers, together called the socket. The socket is composed of the IP address of the machine and the
port number used by the TCP software.
<BR>
<P>Because there are at least two machines involved in network communications, there will be a socket on both the sending and receiving machine. The IP address of each machine is unique, and the port numbers are unique to each machine, so socket numbers
will also be unique across the network. This enables an application to talk to another application across the network based entirely on the socket number.
<BR>
<P>The sending and receiving machines maintain a port table that lists all active port numbers. The two machines involved have reversed entries for each session between the two, a process called binding. In other words, if one machine has the source port
number 23 and the destination port number set at 25, then the other machine will have its source port number set at 25 and the destination port number set at 23.
<BR>
<BR>
<A NAME="E68E416"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Socket Programming</B></FONT></CENTER></H3>
<BR>
<P>Linux supports BSD style socket programming. Both connection-oriented and connectionless types of sockets are supported. In connection-oriented communication, the server and client establish a connection before any data is exchanged. In connectionless
communication, data is exchanged as part of a message. In either case, the server always starts up first, binds itself to a socket, and listens to messages. How the server attempts to listen depends on the type of connection for which you have programmed
it.
<BR>
<P>You need to know about only a few system calls:
<BR>
<UL>
<LI>socket()
<BR>
<BR>
<LI>bind()
<BR>
<BR>
<LI>accept()
<BR>
<BR>
<LI>listen()
<BR>
<BR>
<LI>connect()
<BR>
<BR>
<LI>sendto()
<BR>
<BR>
<LI>recvfrom()
<BR>
<BR>
</UL>
<P>We will cover these in the following examples.
<BR>
<BR>
<A NAME="E69E535"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>socket()</B></FONT></CENTER></H4>
<BR>
<P>The socket() system call creates a socket for the client or the server. The socket function is defined as follows:
<BR>
<PRE>
<FONT COLOR="#000080">#include<sys/types.h>
#include<sys/socket.h>
int socket(int family, int type, int protocol)</FONT></PRE>
<P>For Linux, you will have family = AF_UNIX. The type is either SOCK_STREAM for reliable, though slower communications or SOCK_DGRAM for faster, but less reliable communications. The protocol should be IPPROTO_TCP for SOCK_STREAM and IPPROTO_UDP for
SOCK_DGRAM.
<BR>
<P>The return value from this function is -1 if there was an error; otherwise, it's a socket descriptor. You will use this socket descriptor to refer to this socket in all subsequent calls in your program.
<BR>
<P>Sockets are created without a name. Clients use the name of the socket in order to read or write to it. This is where the bind function comes in.
<BR>
<BR>
<A NAME="E69E536"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>bind()</B><B> System Call</B></FONT></CENTER></H4>
<BR>
<P>The bind() system call assigns a name to an unnamed socket.
<BR>
<PRE>
<FONT COLOR="#000080">#include<sys/types.h>
#include<sys/socket.h>
int bind(int sockfd, struct sockaddr *saddr, int addrlen)</FONT></PRE>
<P>The first item is a socket descriptor. The second is a structure with the name to use, and the third item is the size of the structure.
<BR>
<P>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, then it sets itself up to listen and accept connections. Let's look at the function available for such an endeavor.
<BR>
<BR>
<A NAME="E69E537"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>listen()</B><B> System Call</B></FONT></CENTER></H4>
<BR>
<P>The listen() system call is used by the server. It is defined as follows:
<BR>
<PRE>
<FONT COLOR="#000080">#include<sys/types.h>
#include<sys/socket.h>
int listen(int sockfd, int backlog);</FONT></PRE>
<P>The sockfd is the descriptor of the socket. The backlog is the number of waiting connections at one time before rejecting any. Use the standard value of 5 for backlog. A returned value of less than 1 indicates an error.
<BR>
<P>If this call is successful, you can accept connections.
<BR>
<BR>
<A NAME="E69E538"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>accept()</B><B> System Call</B></FONT></CENTER></H4>
<BR>
<P>The accept() system call is used by a server to accept any incoming messages from clients' connect() calls. Be aware that this function will not return if no connections are received.
<BR>
<PRE>
<FONT COLOR="#000080">#include<sys/types.h>
#include<sys/socket.h>
int accept(int sockfd, struct sockaddr *peeraddr, int addrlen)</FONT></PRE>
<P>The parameters are the same as that for the bind call, with the exception that the peeraddr points to information about the client that is making a connection request. Based on the incoming message, the fields in the structure pointed at by peeraddr are
filled out.
<BR>
<P>So how does a client connect to a server. Let's look at the connect() call.
<BR>
<BR>
<A NAME="E69E539"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>The </B><B>connect()</B><B> System Call</B></FONT></CENTER></H4>
<BR>
<P>The connect() system call is used by clients to connect to a server in a connection-oriented system. This connect() call should be made after the bind() call.
<BR>
<PRE>
<FONT COLOR="#000080">#include<sys/types.h>
#include<sys/socket.h>
int connect(int sockfd, struct sockaddr *servsaddr, int addrlen)</FONT></PRE>
<P>The parameters are the same as that for the bind call, with the exception that the servsaddr points to information about the server that the client is connecting to. The accept call creates a new socket for the server to work with the request. This way
the server can fork() off a new process and wait for more connections. On the server side of things, you would have code that looks like that shown in Listing 54.1.
<BR>
<P>
<FONT COLOR="#000080"><B>Listing 54.1. Server side for socket-oriented protocol.</B></FONT>
<BR>
<PRE>
<FONT COLOR="#000080">#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <linux/net.h>
#define MY_PORT 6545
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) < 0)
{
myabort("Unable to create socket");
}
bzero((char *)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_family = htons(MY_PORT);
/*
* The htonl(for a long integer) and htons(for short integer) convert
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -