您的位置:首页 > 理论基础

Unix Network Programming1

2016-03-12 07:11 183 查看
(Macro,港澳台作巨集),是一种批量处理的称谓。计算机科学里的宏是一种抽象(Abstraction),它根据一系列预定义的规则替换一定的文本模式。解释器编译器在遇到宏时会自动进行这一模式替换。对于编译语言,宏展开在编译时发生,进行宏展开的工具常被称为宏展开器。宏这一术语也常常被用于许多类似的环境中,它们是源自宏展开的概念,这包括键盘宏宏语言。绝大多数情况下,“宏”这个词的使用暗示着将小命令或动作转化为一系列指令

The basics

Program. A program is an executable file residing on a disk in a directory. A program is read into memory and is executed by the kernel as a result of an exec()function. The exec() has six variants, but we only consider the simplest one (exec()) in this course.Process. An executing instance of a program is called a process. Sometimes, task is used instead of process with the same meaning. UNIX guarantees that every process has a unique identifier called the process ID. The process ID is always a non-negative integer.File descriptors. File descriptors are normally small non-negative integers that the kernel uses to identify the files being accessed by a particular process. Whenever it opens an existing file or creates a new file, the kernel returns a file descriptor that is used to read or write the file. As we will see in this course, sockets are based on a very similar mechanism (socket descriptors).Generic Socket Address StructureA socket address structure is always passed by reference as an argument to any socket functions. But any socket function that takes one of these pointers as an argument must deal with socket address structures from any of the supported protocol families.A problem arises in declaring the type of pointer that is passed. With ANSI C, the solution is to use void * (the generic pointer type). But the socket functions predate the definition of ANSI C and the solution chosen was to define a generic socket address as follows:
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family; /* address family: AD_xxx value */
char sa_data[14];
};

TCP Socket API

The sequence of function calls for the client and a server participating in a TCP connection is presented in Figure 3.



As shown in the figure, the steps for establishing a TCP socket on the client side are the following:Create a socket using the socket() function;

Connect the socket to the address of the server using the connect() function;

Send and receive data by means of the read() and write() functions.

The steps involved in establishing a TCP socket on the server side are as follows:Create a socket with the socket() function;

Bind the socket to an address using the bind() function;

Listen for connections with the listen() function;

Accept a connection with the accept() function system call. This call typically blocks until a client connects with the server.

Send and receive data by means of send() and receive().
http://www.cs.dartmouth.edu/~campbell/cs50/socketprogramming.html
To check error:
To check for errors from socket,inet_pton,connect,read and inputs can short our program by defining a wrapper function to perform the actual system call,tests the return value,and terminates on an error.
The convention we use is to capitalize the name of the function,as in
sockfd=Socke(AF_INET,SOCK_STREAM,0);wrapper function:
int Socket(int family,int tyoe,int protocol)
{
int n;
if((n=socket(family,type,protocol))<0)
err_sys("socket error");
return (n);
}
When an error occurs in a UNIX function(such as one of the socket functions),the global variable errno is set to a positive value indicating the type of error and the function normally returns 01.
But storing errno in a global variable does not work with multipke threads that share all global variables.

UDP Socket API

There are some fundamental differences between TCP and UDP sockets. UDP is a connection-less, unreliable, datagram protocol (TCP is instead connection-oriented, reliable and stream based). There are some instances when it makes to use UDP instead of TCP. Some popular applications built around UDP are DNS, NFS, SNMP and for example, some Skype services and streaming media.Figure 4 shows the the interaction between a UDP client and server. First of all, the client does not establish a connection with the server. Instead, the client just sends a datagram to the server using the sendto function which requires the address of the destination as a parameter. Similarly, the server does not accept a connection from a client. Instead, the server just calls the recvfrom function, which waits until data arrives from some client. recvfrom returns the IP address of the client, along with the datagram, so the server can send a response to the client.As shown in the Figure, the steps of establishing a UDP socket communication on the client side are as follows:Create a socket using the socket() function;

Send and receive data by means of the recvfrom() and sendto() functions.

The steps of establishing a UDP socket communication on the server side are as follows:Create a socket with the socket() function;

Bind the socket to an address using the bind() function;

Send and receive data by means of recvfrom() and sendto().

I/O multiplexing means what it says - allowing the programmer to examine and block on multiple I/O streams (or other "synchronizing" events), being notified whenever any one of the streams is active so that it can process data on that stream.

In the Unix world, it's called select() or poll() (when using the CeeLanguage API for Unix). In the MicrosoftWindowsApi world, it's called WaitForMultipleObjects?(). Other languages/environments have similar features:

The advantage of IoMultiplexing is that it allows blocking on multiple resources simultaneously, without needing to use polling (which wastes CPU cycles) or multithreading (which can be difficult to deal with, especially if threads are introduced into an otherwise sequential app only for the purpose of pending on multiple descriptors).

With the understanding, of course, that the CPU cycles are gonna get burned somewhere anyway, and even if your task/process is not threaded, the system as a whole will have other threads/tasks running.

Agreed; with the caveat that there is a lot of difference between CPU cycles spent polling (even if it's a test-then-sleep loop) and CPU cycles spent doing productive work. OTOH, on many destkop systems most CPU cycles are spent in the IdleTask.



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Network 计算机 科学