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