1. 程式人生 > >Tcp/Ip I/O函式

Tcp/Ip I/O函式

  • 用於建立檔案描述符的函式,包括pipe、dup/dup2函式
  • 用於讀寫資料的函式,包括readv/writev、sendfile、mmap/munmap、splice和tee函式。

  • 用於控制I/O行為和屬性的函式,包括fcntl函式。

1.Pipe函式

pipe函式用於建立一個管道

#include<unistd.h>
int pipe(int fd[2]);  //成功返回0 失敗返回-1並設定 errno   fd[0]負責寫入 fd[1]從管道讀取資料  
             //如果想實現雙i昂的資料傳輸 應該使用兩個管道 預設read write都是堵塞的

2.socketpair函式

#include<sys/types.h>
#include<sys/socket.h>
int socketpair(int domain,int type,int protocol,int fd[2]);
//成功返回0 失敗返回-1並設定 errno
  • socketpair前三個引數的含義與socket系統呼叫的三個引數相同     但domain只能使用UNIX本地域協議族AF_UNIX
  • socketpair建立的這對檔案描述符都是既可讀又可寫的

3.dup函式和dup2函式  (複製檔案述符file_descriptor

#include<unistd.h>
int dup(int file_descriptor); //dup函式建立一個新的檔案描述符,該新檔案描述符和原有檔案描述符file_descriptor
指向相同的檔案、管道或者網路連線。
int dup2(int file_descriptor_one,int file_descriptor_two); //dup2和dup類似,不過它將返回第一個不小於file_descriptor_two的整數值。
dup和dup2系統呼叫失敗時返回-1並設定errno。

 

  • 把標準輸入重定向到一個檔案,或者把標準輸出重定
  • 向到一個網路連線(比如CGI程式設計)