標籤:style blog color 使用 os io strong 檔案
1、read
-----------------------------------------------------------------------
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t nbyte);
-----------------------------------------------------------------------
read()函數是負責從fd中讀取內容。當讀成功時,read()返回實際所讀的位元組數,如果返回的值是0,表示已經讀到檔案的結束了,小於0表示出現了錯誤。如果錯誤為EINTR說明讀是由中斷引起的,如果是ECONNREST表示網路連接出了問題。我們寫一個自己的讀函數:
int my_read(int fd,void *buffer,int length) { int bytes_left; int bytes_read; char *ptr; bytes_left=length; while(bytes_left>0) { bytes_read = read(fd,ptr,bytes_read); if (bytes_read < 0) { if(errno==EINTR) bytes_read=0; else return(-1); } else if(bytes_read==0) break; bytes_left-=bytes_read; ptr+=bytes_read; } return(length-bytes_left); }2、write
-----------------------------------------------------------------------
#include <unistd.h>
ssize_t write(int fd, const void*buf,size_t nbytes);
-----------------------------------------------------------------------
write()函數將buf中的nbytes位元組內容寫入檔案描述符fd,成功時返回寫的位元組數,失敗時返回-1並設定errno變數。在網路程式中,當我們向通訊端檔案描述符寫時有兩可能:
1)write的傳回值大於0,表示寫了部分或者是全部的資料,這樣我們用一個while迴圈來不停的寫入,但是迴圈過程中的buf參數和nbyte參數得由我們來更新。也就是說,網路寫函數是不負責將全部資料寫完之後在返回的。
2)返回的值小於0,此時出現了錯誤,我們要根據錯誤類型來處理:
如果錯誤為EINTR表示在寫的時候出現了中斷錯誤。
如果為EPIPE表示網路連接出現了問題(對方已經關閉了串連)。
為了處理以上的情況,我們自己編寫一個寫函數來處理這幾種情況。
int my_write(int fd,void *buffer,int length) { int bytes_left; int written_bytes; char *ptr; ptr=buffer; bytes_left=length; while(bytes_left>0) { /* 開始寫*/ written_bytes=write(fd,ptr,bytes_left); if(written_bytes<=0) /* 出錯了*/ { if(errno==EINTR) /* 中斷錯誤我們繼續寫*/ written_bytes=0; else /* 其他錯誤沒有辦法,只好撤退了*/ return(-1); } bytes_left-=written_bytes; ptr+=written_bytes; /* 從剩下的地方繼續寫 */ } return(0); } 3、recv & send
recv和send函數提供了和read和write差不多的功能,不過它們提供了第四個參數來控制讀寫操作。
-----------------------------------------------------------------------
#include <sys/types.h>
#include <sys/socket.h>
int recv(int sockfd,void *buf,int len,int flags);
int send(int sockfd,void *buf,int len,int flags);
-----------------------------------------------------------------------
前面的三個參數和一樣,第四個參數可以是0或者是以下的組合:
|----------------------------------------------------------------------|
| MSG_DONTROUTE | 不尋找表 |
| MSG_OOB | 接受或者發送帶外資料 |
| MSG_PEEK | 查看資料,並不從系統緩衝區移走資料 |
| MSG_WAITALL | 等待所有資料 |
|----------------------------------------------------------------------|
MSG_DONTROUTE:是send函數使用的標誌。這個標誌告訴IP,目的主機在本網上面,沒有必要尋找表。這個標誌一般用網路診斷和路由程式裡面。
MSG_OOB:表示可以接收和發送帶外的資料。關於帶外資料我們以後會解釋的。
MSG_PEEK:是recv函數的使用標誌。表示只是從系統緩衝區中讀取內容,而不清除系統緩衝區的內容,這樣下次讀的時候仍然是一樣的內容。一般在有多個進程讀寫資料時可以使用這個標誌。
MSG_WAITALL:是recv函數的使用標誌。表示等到所有的資訊到達時才返回。使用這個標誌的時候recv會一直阻塞,直到指定的條件滿足或者是發生了錯誤。
1)當讀到了指定的位元組時,函數正常返回。傳回值等於len;
2)當讀到了檔案的結尾時,函數正常返回。傳回值小於len;
3)當操作發生錯誤時返回-1,且設定錯誤為相應的錯誤號碼(errno)。
MSG_NOSIGNAL:is a flag used by send() in some implementations of the Berkeley sockets API.
This flag requests that the implementation does not to send a SIGPIPE signal on errors on stream oriented sockets when the other end breaks the connection. The EPIPE error is still returned as normal.
Though it is in some Berkely sockets APIs (notably Linux) it does not exist in what some refer to as the reference implementation, FreeBSD, which instead uses a socket option SO_NOSIGPIPE?.
對於伺服器端,我們可以使用這個標誌。目的是不讓其發送SIG_PIPE訊號,導致程式退出。
如果flags為0,則和read、write一樣的操作。還有其它的幾個選項,不過我們實際上用的很少。可以查看 Linux Programmer‘s Manual得到詳細解釋。