connect 是 errno 爲111 115 101 22 錯誤分析

22:參數錯誤,比如ip地址不合法,沒有目標端口等

101:網絡不可達,比如不能ping通

111:鏈接被拒絕,比如目標關閉鏈接等

115:當鏈接設置爲非阻塞時,目標沒有及時應答,返回此錯誤,socket可以繼續使用

 

附錄:Linux的錯誤碼錶(errno table)

_ 124 EMEDIUMTYPE_ Wrong medium type
_ 123 ENOMEDIUM__ No medium found
_ 122 EDQUOT___  Disk quota exceeded
_ 121 EREMOTEIO__ Remote I/O error
_ 120 EISNAM___  Is a named type file
_ 119 ENAVAIL___ No XENIX semaphores available
_ 118 ENOTNAM___ Not a XENIX named type file
_ 117 EUCLEAN___ Structure needs cleaning
_ 116 ESTALE___  Stale NFS file handle
_ 115 EINPROGRESS  +Operation now in progress
_ 114 EALREADY__  Operation already in progress
_ 113 EHOSTUNREACH  No route to host
_ 112 EHOSTDOWN__ Host is down
_ 111 ECONNREFUSED  Connection refused
_ 110 ETIMEDOUT_  +Connection timed out
_ 109 ETOOMANYREFS  Too many references: cannot splice
_ 108 ESHUTDOWN__ Cannot send after transport endpoint shutdown
_ 107 ENOTCONN__  Transport endpoint is not connected
_ 106 EISCONN___ Transport endpoint is already connected
_ 105 ENOBUFS___ No buffer space available
_ 104 ECONNRESET_  Connection reset by peer
_ 103 ECONNABORTED  Software caused connection abort
_ 102 ENETRESET__ Network dropped connection on reset
_ 101 ENETUNREACH_ Network is unreachable
_ 100 ENETDOWN__  Network is down
_  99 EADDRNOTAVAIL Cannot assign requested address
_  98 EADDRINUSE_  Address already in use
_  97 EAFNOSUPPORT  Address family not supported by protocol
_  96 EPFNOSUPPORT  Protocol family not supported
_  95 EOPNOTSUPP_  Operation not supported
_  94 ESOCKTNOSUPPORT Socket type not supported
_  93 EPROTONOSUPPORT Protocol not supported
_  92 ENOPROTOOPT_ Protocol not available
_  91 EPROTOTYPE_  Protocol wrong type for socket
_  90 EMSGSIZE__ +Message too long
_  89 EDESTADDRREQ  Destination address required
_  88 ENOTSOCK__  Socket operation on non-socket
_  87 EUSERS___  Too many users
_  86 ESTRPIPE__  Streams pipe error
_  85 ERESTART__  Interrupted system call should be restarted
_  84 EILSEQ___  Invalid or incomplete multibyte or wide character
_  83 ELIBEXEC__  Cannot exec a shared library directly
_  82 ELIBMAX___ Attempting to link in too many shared libraries
_  81 ELIBSCN___ .lib section in a.out corrupted
_  80 ELIBBAD___ Accessing a corrupted shared library
_  79 ELIBACC___ Can not access a needed shared library
_  78 EREMCHG___ Remote address changed
_  77 EBADFD___  File descriptor in bad state
_  76 ENOTUNIQ__  Name not unique on network
_  75 EOVERFLOW__ Value too large for defined data type
_  74 EBADMSG__  +Bad message
_  73 EDOTDOT___ RFS specific error
_  72 EMULTIHOP__ Multihop attempted
_  71 EPROTO___  Protocol error
_  70 ECOMM____ Communication error on send
_  69 ESRMNT___  Srmount error
_  68 EADV____  Advertise error
_  67 ENOLINK___ Link has been severed
_  66 EREMOTE___ Object is remote
_  65 ENOPKG___  Package not installed
_  64 ENONET___  Machine is not on the network
_  63 ENOSR____ Out of streams resources
_  62 ETIME____ Timer expired
_  61 ENODATA___ No data available
_  60 ENOSTR___  Device not a stream
_  59 EBFONT___  Bad font file format
_  57 EBADSLT___ Invalid slot
_  56 EBADRQC___ Invalid request code
_  55 ENOANO___  No anode
_  54 EXFULL___  Exchange full
_  53 EBADR____ Invalid request descriptor
_  52 EBADE____ Invalid exchange
_  51 EL2HLT___  Level 2 halted
_  50 ENOCSI___  No CSI structure available
_  49 EUNATCH___ Protocol driver not attached
_  48 ELNRNG___  Link number out of range
_  47 EL3RST___  Level 3 reset
_  46 EL3HLT___  Level 3 halted
_  45 EL2NSYNC__  Level 2 not synchronized
_  44 ECHRNG___  Channel number out of range
_  43 EIDRM____ Identifier removed
_  42 ENOMSG___  No message of desired type
_  40 ELOOP____ Too many levels of symbolic links
_  39 ENOTEMPTY_  +Directory not empty
_  38 ENOSYS___ +Function not implemented
_  37 ENOLCK___ +No locks available
_  36 ENAMETOOLONG +File name too long
_  35 EDEADLK__  +Resource deadlock avoided
_  34 ERANGE___ +Numerical result out of range
_  33 EDOM____ +Numerical argument out of domain
_  32 EPIPE___  +Broken pipe
_  31 EMLINK___ +Too many links
_  30 EROFS___  +Read-only file system
_  29 ESPIPE___ +Illegal seek
_  28 ENOSPC___ +No space left on device
_  27 EFBIG___  +File too large
_  26 ETXTBSY___ Text file busy
_  25 ENOTTY___ +Inappropriate ioctl for device
_  24 EMFILE___ +Too many open files
_  23 ENFILE___ +Too many open files in system
_  22 EINVAL___ +Invalid argument
_  21 EISDIR___ +Is a directory
_  20 ENOTDIR__  +Not a directory
_  19 ENODEV___ +No such device
_  18 EXDEV___  +Invalid cross-device link
_  17 EEXIST___ +File exists
_  16 EBUSY___  +Device or resource busy
_  15 ENOTBLK___ Block device required
_  14 EFAULT___ +Bad address
_  13 EACCES___ +Permission denied
_  12 ENOMEM___ +Cannot allocate memory
_  11 EAGAIN___ +Resource temporarily unavailable
_  10 ECHILD___ +No child processes
__ 9 EBADF___  +Bad file descriptor
__ 8 ENOEXEC__  +Exec format error
__ 7 E2BIG___  +Argument list too long
__ 6 ENXIO___  +No such device or address
__ 5 EIO____  +Input/output error
__ 4 EINTR___  +Interrupted system call
__ 3 ESRCH___  +No such process
__ 2 ENOENT___ +No such file or directory
__ 1 EPERM___  +Operation not permitted
#_  0 --_____  Success

 

附錄參考 http://fanqiang.chinaunix.net/a4/b7/20010913/0900001283.html

摘要:

討論關於利用select()檢測對方Socket關閉的問題: 
 
仍然是本地Socket有東東可讀,因爲對方Socket關閉時,會發一個關閉連接 
通知報文,會馬上被select()檢測到的。關於TCP的連接(三次握手)和關 
閉(二次握手)機制,敬請參考有關TCP/IP的書籍。 
 
不知是什麼原因,UNIX好象沒有提供通知進程關於Socket或Pipe對方關閉的 
信號,也可能是cpu所知有限。總之,當對方關閉,一執行recv()或read(), 
馬上回返回-1,此時全局變量errno的值是115,相應的sys_errlist[errno] 
爲"Connect refused"(請參考/usr/include/sys/errno.h)。所以,在上 
篇的for(;;)...select()程序塊中,當有東西可讀時,一定要檢查recv()或 
read()的返回值,返回-1時要作出關斷本地Socket的處理,否則select()會 
一直認爲有東西讀,其結果曾幾令cpu傷心欲斷針腳。不信你可以試試:不檢 
查recv()返回結果,且將收到的東東(實際沒收到)寫至標準輸出... 
在有名管道的編程中也有類似問題出現。具體處理詳見拙作:發佈一個有用 
的Socket客戶方原碼。 
 
至於主動寫Socket時對方突然關閉的處理則可以簡單地捕捉信號SIGPIPE並作 
出相應關斷本地Socket等等的處理。SIGPIPE的解釋是:寫入無讀者方的管道。 
在此不作贅述,請詳man signal。 
 
以上是cpu在作tcp/ip數據傳輸實驗積累的經驗,若有錯漏,請狂炮擊之。 
 
唉,昨天在hacker區被一幫孫子轟得差點兒沒短路。ren cpu(奔騰的心) z80 
 
補充關於select在異步(非阻塞)connect中的應用,剛開始搞socket編程的時候 
我一直都用阻塞式的connect,非阻塞connect的問題是由於當時搞proxy scan 
而提出的呵呵 
通過在網上與網友們的交流及查找相關FAQ,總算知道了怎麼解決這一問題.同樣 
用select可以很好地解決這一問題.大致過程是這樣的: 
 
1.將打開的socket設爲非阻塞的,可以用fcntl(socket, F_SETFL, O_NDELAY)完 
成(有的系統用FNEDLAY也可). 
 
2.發connect調用,這時返回-1,但是errno被設爲EINPROGRESS,意即connect仍舊 
在進行還沒有完成. 
 
3.將打開的socket設進被監視的可寫(注意不是可讀)文件集合用select進行監視, 
如果可寫,用 
        getsockopt(socket, SOL_SOCKET, SO_ERROR, &error, sizeof(int)); 
來得到error的值,如果爲零,則connect成功. 
 
在許多unix版本的proxyscan程序你都可以看到類似的過程,另外在solaris精華 
區->編程技巧中有一個通用的帶超時參數的connect模塊. 

發佈了35 篇原創文章 · 獲贊 5 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章