Unix Sockets 简明教程

Unix Socket - Helper Functions

本章描述了所有辅助函数,这些函数在执行套接字编程时使用。其他辅助函数在章节中描述 - Ports and Services 和网络 Byte Orders

This chapter describes all the helper functions, which are used while doing socket programming. Other helper functions are described in the chapters −Ports and Services, and Network Byte Orders.

The write Function

write 函数尝试将 buf 指向的缓冲区中的 nbyte 字节写入与打开的文件描述符 fildes 关联的文件。

The write function attempts to write nbyte bytes from the buffer pointed by buf to the file associated with the open file descriptor, fildes.

您还可以使用 send() 函数向另一个进程发送数据。

You can also use send() function to send data to another process.

#include <unistd.h>

int write(int fildes, const void *buf, int nbyte);

成功完成后,write() 返回实际写入与 fildes 关联的文件的字节数。此数字永远不会大于 nbyte。否则,返回 -1。

Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned.

Parameters

  1. fildes − It is a socket descriptor returned by the socket function.

  2. buf − It is a pointer to the data you want to send.

  3. nbyte − It is the number of bytes to be written. If nbyte is 0, write() will return 0 and have no other results if the file is a regular file; otherwise, the results are unspecified.

The read Function

read 函数尝试从与缓冲区 fildes 关联的文件读取 nbyte 字节,并将其读入 buf 指向的缓冲区。

The read function attempts to read nbyte bytes from the file associated with the buffer, fildes, into the buffer pointed to by buf.

您还可以使用 recv() 函数读取另一个进程的数据。

You can also use recv() function to read data to another process.

#include <unistd.h>

int read(int fildes, const void *buf, int nbyte);

成功完成后,write() 返回实际写入与 fildes 关联的文件的字节数。此数字永远不会大于 nbyte。否则,返回 -1。

Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned.

Parameters

  1. fildes − It is a socket descriptor returned by the socket function.

  2. buf − It is the buffer to read the information into.

  3. nbyte − It is the number of bytes to read.

The fork Function

fork 函数创建一个新进程。称为子进程的新进程将是调用进程(父进程)的确切副本。子进程从父进程继承许多属性。

The fork function creates a new process. The new process called the child process will be an exact copy of the calling process (parent process). The child process inherits many attributes from the parent process.

#include <sys/types.h>
#include <unistd.h>

int fork(void);

成功完成后,fork() 将 0 返回给子进程,并将子进程的进程 ID 返回给父进程。否则,返回 -1 给父进程,不会创建子进程,并设置 errno 指示错误。

Upon successful completion, fork() returns 0 to the child process and the process ID of the child process to the parent process. Otherwise -1 is returned to the parent process, no child process is created and errno is set to indicate the error.

Parameters

  1. void − It means no parameter is required.

The bzero Function

bzero 函数在字符串 s 中放置 nbyte 个空字节。此函数用于使用空值设置所有套接字结构。

The bzero function places nbyte null bytes in the string s. This function is used to set all the socket structures with null values.

void bzero(void *s, int nbyte);

此函数不会返回任何内容。

This function does not return anything.

Parameters

  1. s − It specifies the string which has to be filled with null bytes. This will be a point to socket structure variable.

  2. nbyte − It specifies the number of bytes to be filled with null values. This will be the size of the socket structure.

The bcmp Function

bcmp 函数将字节字符串 s1 与字节字符串 s2 进行比较。假设这两个字符串都是 nbyte 字节长。

The bcmp function compares byte string s1 against byte string s2. Both strings are assumed to be nbyte bytes long.

int bcmp(const void *s1, const void *s2, int nbyte);

如果这两个字符串相同,则此函数返回 0,否则返回 1。如果 nbyte 为 0,bcmp() 函数始终返回 0。

This function returns 0 if both strings are identical, 1 otherwise. The bcmp() function always returns 0 when nbyte is 0.

Parameters

  1. s1 − It specifies the first string to be compared.

  2. s2 − It specifies the second string to be compared.

  3. nbyte − It specifies the number of bytes to be compared.

The bcopy Function

bcopy 函数将 nbyte 字节从字符串 s1 复制到字符串 s2。重叠字符串得到了正确的处理。

The bcopy function copies nbyte bytes from string s1 to the string s2. Overlapping strings are handled correctly.

void bcopy(const void *s1, void *s2, int nbyte);

此函数不会返回任何内容。

This function does not return anything.

Parameters

  1. s1 − It specifies the source string.

  2. s2v − It specifies the destination string.

  3. nbyte − It specifies the number of bytes to be copied.

The memset Function

memset 函数还用于像 bzero 一样设置结构变量。请看下面给出的语法。

The memset function is also used to set structure variables in the same way as bzero. Take a look at its syntax, given below.

void *memset(void *s, int c, int nbyte);

此函数返回一个指向 void 的指针;实际上是一个指向已设置内存的指针,并且需要相应地对其进行转换。

This function returns a pointer to void; in fact, a pointer to the set memory and you need to caste it accordingly.

Parameters

  1. s − It specifies the source to be set.

  2. c − It specifies the character to set on nbyte places.

  3. nbyte − It specifies the number of bytes to be set.