socket常见错误代码解析

发布时间 : 星期一 文章socket常见错误代码解析更新完毕开始阅读

socket常见错误代码解析

Socket error 0 - Directly send error

Socket error 10004 - Interrupted function call Socket error 10013 - Permission denied Socket error 10014 - Bad address Socket error 10022 - Invalid argument Socket error 10024 - Too many open files

Socket error 10035 - Resource temporarily unavailable Socket error 10036 - Operation now in progress Socket error 10037 - Operation already in progress Socket error 10038 - Socket operation on non-socket Socket error 10039 - Destination address required Socket error 10040 - Message too long

Socket error 10041 - Protocol wrong type for socket Socket error 10042 - Bad protocol option Socket error 10043 - Protocol not supported Socket error 10044 - Socket type not supported Socket error 10045 - Operation not supported

Socket error 10046 - Protocol family not supported

Socket error 10047 - Address family not supported by protocol family Socket error 10048 - Address already in use

Socket error 10049 - Cannot assign requested address Socket error 10050 - Network is down

Socket error 10051 - Network is unreachable

Socket error 10052 - Network dropped connection on reset Socket error 10053 - Software caused connection abort Socket error 10054 - Connection reset by peer Socket error 10055 - No buffer space available Socket error 10056 - Socket is already connected Socket error 10057 - Socket is not connected

Socket error 10058 - Cannot send after socket shutdown Socket error 10060 - Connection timed out Socket error 10061 - Connection refused Socket error 10064 - Host is down Socket error 10065 - No route to host Socket error 10067 - Too many processes

Socket error 10091 - Network subsystem is unavailable Socket error 10092 - WINSOCK.DLL version out of range Socket error 10093 - Successful WSAStartup not yet performed Socket error 10094 - Graceful shutdown in progress Socket error 11001 - Host not found

Socket error 11002 - Non-authoritative host not found Socket error 11003 - This is a non-recoverable error

Socket error 11004 - Valid name, no data record of requested type

WSAEADDRINUSE (10048) Address already in use

WSAECONNABORTED (10053) Software caused connection abort WSAECONNREFUSED (10061) Connection refused WSAECONNRESET (10054) Connection reset by peer

WSAEDESTADDRREQ (10039) Destination address required WSAEHOSTUNREACH (10065) No route to host WSAEMFILE (10024) Too many open files WSAENETDOWN (10050) Network is down

WSAENETRESET (10052) Network dropped connection WSAENOBUFS (10055) No buffer space available

WSAENETUNREACH (10051) Network is unreachable WSAETIMEDOUT (10060) Connection timed out WSAHOST_NOT_FOUND (11001) Host not found

WSASYSNOTREADY (10091) Network sub-system is unavailable WSANOTINITIALISED (10093) WSAStartup() not performed WSANO_DATA (11004) Valid name, no data of that type WSANO_RECOVERY (11003) Non-recoverable query error WSATRY_AGAIN (11002) Non-authoritative host found

WSAVERNOTSUPPORTED (10092) Wrong WinSock DLL version

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++ 常見SOCKET錯誤返回碼

WSAEINTR (10004)被中斷的系統呼叫

當 以阻攔式進行的WinSock函式被WSACancelBlockingCall()中斷的時候,這個阻攔式函式會得到WSAEINTR這個錯 誤訊息。讀者要注意的是,當你的程序有用WSACancelBlockingCall去中斷阻攔式函式的時候,你必須為這個阻攔式函式寫處理 WSAEINTR錯誤訊息的程序代碼,否則你的程序可能會出現些無預期的錯誤。

原則上,所有能以阻攔式進行的函式都可能會發生這個錯誤。

WSAEBADF (10009)錯誤的檔案代碼

柏 克萊socket接口中,檔案描述子和socket描述子是相通的,開啟socket可以想像成開啟一個檔案。 WSAEBADF在柏克萊socket界面的意義是指錯誤的socket描述子,這個錯誤的socket描述子可能是未經開啟的socket或是以關閉的 socket。

在WinSock中有個相通的錯誤代碼WSAENOTSOCK,用來指定錯誤的socket描述子。詳細說明請參考WSAENOTSOCK部分。

WSAEACCES (10013)無此權限

對 於柏克萊socket接口函式,這個錯誤發生的原因通常是開啟一個不具有開啟權限的檔案或socket。例如在UNIX上,一般的使用者不能開 啟SOCK_RAW的socket,通常是超級使用者(super user)才有此權限。如果一般的使用者企圖開啟SOCK_RAW的socket,將會得到這個錯誤訊息。

對於WinSock API函式接口,發生此錯誤的函式有二:send()和sendto()。當利用send()或sendto()傳送數據的時候,將目的地址設成廣播地址 ( broadcast address ),但是並未呼叫setsockopt()設定SO_BROADCAST選項時便會發生WSAEACCES錯誤。

WSAEFAULT (10014)錯誤的內存地址

系 統偵測出使用錯誤的內存地址。呼叫WinSock函式時,常常會有參數是以指標的方式傳入,這個指標可能是指向一個結構,如 sockaddr_in結構,或是指向一個數據緩衝區。當程序不小心傳入一個錯誤內存未指的指針時,就會發生WSAEFAULT這個錯誤。此外,當指針所 指向的內存區塊太小的時候,也會發生這個錯誤。

WSAEINVAL (10022)參數錯誤

如果傳入不正確的參數給WinSock函式的話,會導致WSAEINVAL這個錯誤。如果參數是一個結構的指針,此結構內容填入不正確的值,也會導致這個錯誤。例如,shutdown()函式的how()參數只能是0、1或2,若是設定成其它數值,就會出現這個錯誤。

在網絡程序的設計中,函式和函式之間的呼叫通常有一定的順序,如過不照這個順序進行,也會出現這個錯誤。底下是針對每個相關函式的詳細說明。

函式

錯誤發生的時機

accept()

呼叫accept()之前沒有成功呼叫listen()

bind()

已經成功呼叫bind()函式而指定stocket的名稱了

getstockname()

沒有呼叫bind()函式指定socket名稱

listen()

已經處於連接狀態或是socket沒有呼叫bind()函式指定socket名稱

recv()和recvfrom()

對於datagram socket,socket沒有呼叫bind()函式指定IP地址、port和協議等;對於stream

socket,連接尚未建立

send()和sendto()

對於datagram socket,socket沒有呼叫bind()函式指定IP地址、port和協議等;對於stream socket,連接尚未建立

原則上,所有函式其參數所能設定的值有一定的範圍,或是有一些特定的值,這些函式都有可能發生這個錯誤。

WSAEMFILE (10024)太多開啟的檔案

柏 克萊socket接口的解釋是開啟過多的檔案,超過檔案開啟數的限制。在柏克萊socket將檔案和socket的開啟以檔案描述子(file descriptor)描述。 WinSock對於socket是有別於檔案的,WSAEMFILE在WinSock的意義是開啟太多的socket。

這個程序通常發生在同一部機器中執行過多的網絡程序,以致開啟過多的socket。另一種可以避免的錯誤是應用程序沒有正常的關閉不用的socket,以致socket資源耗盡。

WSAEWOULDBLOCK (10035)函式作用阻攔中

當 函式作用是以非阻攔式進行,而此刻的函式作用再返回時其函式作用尚未完成的時候,就會出現這個錯誤。舉例來說,假設send()以非阻攔式傳送 一段數據至遠程主機,假設函式返回時得到WSAEWOULDBLOCK這個錯誤,這樣表示這一段數據不能在這一次的send()函式呼叫中傳送完。

對於connect()函式,這個錯誤表示尚未成功地連接上遠程主機,連接要求正在進行中。若你的程序中有利用setsockopt()函式以SO_LINGER設定時限,在呼叫closesocket()時有可能出現這個錯誤。

對於異步的網絡數據函式( WSAAsyncGetXXXXByYYYY())而言,得到WSAEWOULDBLOCK錯誤,表示要查詢的網絡數據目前無法取得。這通常是個暫時性的錯誤,應用程序可能稍後再呼叫相同的函式就會成功。

WSAEINPROGRESS (10036)有阻攔函式正在執行中

對於每個process或thread,WinSock只允許”一”個阻攔式函式的執行。如果已有阻攔式函式正在執行,而又呼叫到WinSock函式,函式會傳回WSAEINPROGRESS的錯誤。

這 個錯誤也會發生在需要長執行時間的函式,如connect()。有一種情況是這樣:當執行”非阻攔式”的connect()函式兩次,由於是非 阻攔式的connect(),第一次的呼叫傳回WSAEWOULDBLOCK錯誤碼。此時要求連接動作尚未完成,第二次的connect()呼叫就會得到 WSAEINPROGRESS的錯誤。

联系合同范文客服:xxxxx#qq.com(#替换为@)