Linux网络编程socket错误码分析
- 格式:doc
- 大小:60.00 KB
- 文档页数:7
昆明理工大学Linux程序设计题目:Linux下程序设计之Socket编程学生姓名:学号:专业班级:指导教师:梁波摘要Socket接口是TCP/IP网络的API,Socket接口定义了许多函数或例程,程式员能用他们来研发TCP/IP网络上的应用程式。
要学Internet上的TCP/IP网络编程,必须理解Socket接口。
Socket接口设计者最先是将接口放在Unix操作系统里面的。
网络的Socket 数据传输是一种特别的I/O,Socket也是一种文件描述符。
Socket也具有一个类似于打开文件的函数调用Socket(),该函数返回一个整型的Socket描述符,随后的连接建立、数据传输等操作都是通过该Socket实现的。
常用的Socket类型有两种:流式Socket (SOCK_STREAM)和数据报式Socket(SOCK_DGRAM)。
流式是一种面向连接的Socket,针对于面向连接的TCP服务应用;数据报式Socket是一种无连接的Socket,对应于无连接的UDP服务应用。
本设计是面向客户—服务器模型设计,主要实现一个客户端向服务器传输文件数据,另一个客户端可以从服务器获取该数据的功能。
Linux下程序设计之Socket编程一、设计目的掌握Linux网络开发知识。
掌握课题的设计步骤和方法。
提高Linux下c程序设计的能力。
掌握应用程序开发中的需求分析与方法。
二、实验原理1.Socket建立为了建立Socket,程式能调用Socket函数,该函数返回一个类似于文件描述符的句柄。
socket函数原型为:int socket(int domain, int type, int protocol);domain指明所使用的协议族,通常为PF_INET,表示互连网协议族(TCP/IP协议族);type参数指定socket的类型: SOCK_STREAM 或SOCK_DGRAM,Socket 接口还定义了原始Socket(SOCK_RAW),允许程式使用低层协议;protocol通常赋值"0"。
Sockets/Windows Sockets错误码Windows Sockets在头文件winsock.h中定义了所有的错误码,它们包括以“WSA”打头的Windows Sockets实现返回的错误码和Berkeley Sockets定义的错误码全集。
定义Berkeley Sockets错误码是为了确保原有软件的可移植性。
WSAEACCES (10013) Permission denied.试图使用被禁止的访问权限去访问套接字。
例如,在没有使用函数setsockopt()的SO_BROADCAST命令设置广播权限的套接字上使用函数sendto()给一个广播地址发送数据。
WSAEADDRINUSE (10048)Address already in use.正常情况下每一个套接字地址(协议/IP地址/端口号)只允许使用一次。
当应用程序试图使用bind()函数将一个被已存在的或没有完全关闭的或正在关闭的套接字使用了的IP地址/端口号绑扎到一个新套接字上时,该错误发生。
对于服务器应用程序来说,如果需要使用bind()函数将多个套接字绑扎到同一个端口上,可以考虑使用setsockopt()函数的SO_REUSEADDR命令。
客户应用程序一般不必使用bind()函数——connect()函数总是自动选择没有使用的端口号。
当bind()函数操作的是通配地址(包括ADDR_ANY)时,错误WSAEADDRINUSE可能延迟到一个明确的地址被提交时才发生。
这可能在后续的函数如connect()、listen()、WSAConnect()或WSAJoinLeaf()调用时发生。
WSAEADDRNOTAVAIL (10049)Cannot assign requested address.被请求的地址在它的环境中是不合法的。
通常地在bind()函数试图将一个本地机器不合法的地址绑扎到套接字时产生。
它也可能在connect()、sendto()、WSAConnect()、WSAJoinLeaf()或WSASendTo()函数调用时因远程机器的远程地址或端口号非法(如0地址或0端口号)而产生。
Linux错误码大全查看错误代码errno是调试程序的一个重要方法。
当linuc C api函数发生异常时,一般会将errno变量(需include errno.h)赋一个整数值,不同的值表示不同的含义,可以通过查看该值推测出错的原因。
在实际编程中用这一招解决了不少原本看来莫名其妙的问题。
比较麻烦的是每次都要去linux源代码里面查找错误代码的含义,现在把它贴出来,以后需要查时就来这里看了。
1-34号错误号是在内核源码的include/asm-generic/errno-base.h定义35-132则是在include/asm-generic/errno.h中定义剩下还有一些更大的错误号是留给内核级别的,如系统调用等,用户程序一般是看不见的这些号的,Ubuntu9.10中/usr/src/linux-headers-2.6.31-21-generic/include/linux/errno.h#ifndef _ASM_GENERIC_ERRNO_BASE_H#define _ASM_GENERIC_ERRNO_BASE_H#define EPERM 1 /*Operation not permitted */ #define ENOENT 2 /*No such file or directory */ #define ESRCH 3 /*No such process */#define EINTR 4 /*Interrupted system call */ #define EIO 5 /*I/O error */#define ENXIO 6 /*No such device or address */ #define E2BIG 7 /*Argument list too long */ #define ENOEXEC 8 /*Exec format error */ #define EBADF 9 /*Bad file number */#define ECHILD 10 /*No child processes */ #define EAGAIN 11 /*Try again */#define ENOMEM 12 /*Out of memory */#define EACCES 13 /*Permission denied */ #define EFAULT 14 /*Bad address */#define ENOTBLK 15 /*Block device required */ #define EBUSY16 /*Device or resource busy */ #define EEXIST 17 /*File exists */#define EXDEV 18 /*Cross-device link */#define ENODEV 19 /*No such device */#define ENOTDIR 20 /*Not a directory */#define EISDIR 21 /*Is a directory */#define EINVAL 22 /*Invalid argument */#define ENFILE 23 /*File table overflow */#define EMFILE 24 /*Too many open files */#define ENOTTY 25 /*Not a typewriter */#define ETXTBSY 26 /*Text file busy */#define EFBIG 27 /*File too large */#define ENOSPC 28 /*No space left on device */ #define ESPIPE 29 /*Illegal seek */#define EROFS 30 /*Read-only file system */#define EMLINK 31 /*Too many links */#define EPIPE 32 /*Broken pipe */#define EDOM 33 /*Math argument out of domain of func */#define ERANGE 34 /*Math result not representable */#endif#include#define EDEADLK 35 /*Resource deadlock would occur */ #define ENAMETOOLONG 36 /*File name too long */#define ENOLCK 37 /*No record locks available */ #define ENOSYS 38 /*Function not implemented */ #defineENOTEMPTY 39 /*Directory not empty */#define ELOOP 40 /*Too many symbolic links encountered */#define EWOULDBLOCK EAGAIN /*Operation would block */ #define ENOMSG 42 /*No message of desired type */ #define EIDRM 43 /*Identifier removed */#define ECHRNG 44 /*Channel number out of range */ #define EL2NSYNC 45 /*Level 2 not synchronized */ #define EL3HLT 46 /*Level 3 halted */#define EL3RST 47 /*Level 3 reset */#define ELNRNG 48 /*Link number out of range */#define EUNATCH 49 /*Protocol driver not attached */ #define ENOCSI 50 /*No CSI structure available */ #define EL2HLT 51 /*Level 2 halted */#define EBADE 52 /*Invalid exchange */#define EBADR 53 /*Invalid request descriptor */ #define EXFULL 54 /*Exchange full */#define ENOANO 55 /*No anode */#define EBADRQC 56 /*Invalid request code */ #define EBADSLT 57 /*Invalid slot */#define EDEADLOCK EDEADLK#define EBFONT 59 /*Bad font file format */#define ENOSTR 60 /*Device not a stream */#define ENODATA 61 /*No data available */#define ETIME 62 /*Timer expired */#define ENOSR 63 /*Out of streams resources */#define ENONET 64 /*Machine is not on the network */ #define ENOPKG 65 /*Package not installed */ #define EREMOTE 66 /*Object is remote */#define ENOLINK 67 /*Link has been severed */ #define EADV 68 /*Advertise error */#define ESRMNT 69 /*Srmount error */#define ECOMM 70 /*Communication error on send */ #define EPROTO 71 /*Protocol error */#define EMULTIHOP 72 /*Multihop attempted */#define EDOTDOT 73 /*RFS specific error */#define EBADMSG 74 /*Not a data message */#define EOVERFLOW 75 /*Value too large for defined data type */#define ENOTUNIQ 76 /*Name not unique on network */#define EBADFD 77 /*File descriptor in bad state */ #define EREMCHG 78 /*Remote address changed */ #define ELIBACC 79 /*Can not access a needed shared library */#define ELIBBAD 80 /*Accessing a corrupted shared library */ #define ELIBSCN 81 /* .lib section in a.out corrupted */#define ELIBMAX 82 /*Attempting to link in too many shared libraries */#define ELIBEXEC 83 /*Cannot exec a shared library directly */ #define EILSEQ 84 /*Illegal byte sequence */#define ERESTART 85 /*Interrupted system call should be restarted */#define ESTRPIPE 86 /*Streams pipe error */#define EUSERS 87 /*Too many users */#define ENOTSOCK 88 /*Socket operation on non-socket */#define EDESTADDRREQ 89 /*Destination address required */ #define EMSGSIZE 90 /*Message too long */#define EPROTOTYPE 91 /*Protocol wrong type for socket */ #define ENOPROTOOPT 92 /*Protocol not available */#define EPROTONOSUPPORT 93 /*Protocol not supported */ #define ESOCKTNOSUPPORT 94 /*Socket type not supported */ #define EOPNOTSUPP 95 /*Operation not supported on transport endpoint */#define EPFNOSUPPORT 96 /*Protocol family not supported */ #define EAFNOSUPPORT 97 /*Address family not supported by protocol */#define EADDRINUSE 98 /*Address already in use */#define EADDRNOTAVAIL 99 /*Cannot assign requested address */#define ENETDOWN 100 /*Network is down */#define ENETUNREACH 101 /*Network is unreachable */#define ENETRESET 102 /*Network dropped connection because of reset */#define ECONNABORTED 103 /*Software caused connection abort */#define ECONNRESET 104 /*Connection reset by peer */#define ENOBUFS 105 /*No buffer space available */ #define EISCONN 106 /*Transport endpoint is already connected */#define ENOTCONN 107 /*Transport endpoint is not connected */#define ESHUTDOWN 108 /*Cannot send after transport endpoint shutdown */#define ETOOMANYREFS 109 /*T oo many references: cannot splice */#define ETIMEDOUT 110 /*Connection timed out */#define ECONNREFUSED 111 /*Connection refused */ #define EHOSTDOWN 112 /*Host is down */#define EHOSTUNREACH 113 /*No route to host */#define EALREADY 114 /*Operation already in progress */ #define EINPROGRESS 115 /*Operation now in progress */ #define ESTALE 116 /*Stale NFS file handle */ #define EUCLEAN 117 /*Structure needs cleaning */ #define ENOTNAM 118 /*Not a XENIX named type file */ #define ENAVAIL 119 /*No XENIX semaphores available */ #define EISNAM 120 /*Is a named type file */ #define EREMOTEIO 121 /*Remote I/O error */#define EDQUOT 122 /*Quota exceeded */#define ENOMEDIUM 123 /*No medium found */#define EMEDIUMTYPE 124 /*Wrong medium type *#define ECANCELED 125 / *操作已取消*/#define ENOKEY 126 / *必需的密钥不可用*/ #define EKEYEXPIRED 127 / *密钥已过期*/#define EKEYREVOKED 128 / *密钥已被撤销*/#define EKEYREJECTED 129 / *密钥被服务拒绝*// *用于强大的互斥体*/#define EOWNERDEAD 130 / *所有者死亡*/#define ENOTRECOVERABLE 131 / *状态不可恢复*/#define ERFKILL 132 / *由于射频杀死*/#ifdef __KERNEL__/ **用户程序切勿看到这些内容。
WSAEINTR (10004)∙翻译:中断函数调用。
∙说明:阻止操作被中断通过调用WSACancelBlockingCall (Wsapiref_704y.asp)。
WSAEACCES (10013)∙翻译:权限被拒绝。
∙说明:尝试访问套接字访问权限被禁止的方式。
例如,用于发送到广播的地址,但广播的权限未设置通过使用setsockopt(SO_BROADCAST) 时,将发生此错误。
另一个可能导致WSAEACCES 错误的原因是,当调用绑定(Wsapiref_6vzm.asp)函数(在Microsoft Windows NT 4.0 Service Pack 4 [SP4] 或更高版本),另一个程序、服务或内核模式驱动程序绑定到同一地址具有独占访问权。
这种独占的访问是一项新功能的Windows NT 4.0 SP4 和更高版本,并且它使用SO_EXCLUSIVEADDRUSE 选项的实现。
WSAEFAULT (10014)∙翻译:错误的地址。
∙说明:尝试使用指针参数的调用时,系统检测到一个无效的指针地址。
如果程序传递了无效的指针值,或者如果缓冲区的长度太小,则会发生此错误。
例如,如果一个参数,它是一种SOCKADDR 结构的长度小于sizeof(SOCKADDR) 的值,将发生此问题。
WSAEINVAL (10022)∙翻译:无效的参数。
∙说明:setsockopt (Wsapiref_94aa.asp) 函数提供了无效的参数(例如,指定参数的%)。
有时,它也就是从插座的当前状态,调用例如,未在侦听的套接字接受(Wsapiref_13aq.asp)。
WSAEMFILE (10024)∙翻译:打开的文件太多。
∙说明:有太多打开的套接字。
每个实现都可能具有套接字句柄可用的最大数目。
这些句柄可能会提供每个进程的全局,或每个线程。
WSAEWOULDBLOCK (10035)∙翻译:资源暂时不可用。
∙说明:将返回此错误,无法立即完成,例如,非阻塞套接字操作从接收(Wsapiref_2i9e.asp)时无数据排队要从套接字读取。
WSAEINTR (10004)∙翻译:中断函数调用。
∙说明:阻止操作被中断通过调用WSACancelBlockingCall (Wsapiref_704y.asp)。
WSAEACCES (10013)∙翻译:权限被拒绝。
∙说明:尝试访问套接字访问权限被禁止的方式。
例如,用于发送到广播的地址,但广播的权限未设置通过使用setsockopt(SO_BROADCAST) 时,将发生此错误。
另一个可能导致WSAEACCES 错误的原因是,当调用绑定(Wsapiref_6vzm.asp)函数(在Microsoft Windows NT 4.0 Service Pack 4 [SP4] 或更高版本),另一个程序、服务或内核模式驱动程序绑定到同一地址具有独占访问权。
这种独占的访问是一项新功能的Windows NT 4.0 SP4 和更高版本,并且它使用SO_EXCLUSIVEADDRUSE 选项的实现。
WSAEFAULT (10014)∙翻译:错误的地址。
∙说明:尝试使用指针参数的调用时,系统检测到一个无效的指针地址。
如果程序传递了无效的指针值,或者如果缓冲区的长度太小,则会发生此错误。
例如,如果一个参数,它是一种SOCKADDR 结构的长度小于sizeof(SOCKADDR) 的值,将发生此问题。
WSAEINVAL (10022)∙翻译:无效的参数。
∙说明:setsockopt (Wsapiref_94aa.asp) 函数提供了无效的参数(例如,指定参数的%)。
有时,它也就是从插座的当前状态,调用例如,未在侦听的套接字接受(Wsapiref_13aq.asp)。
WSAEMFILE (10024)∙翻译:打开的文件太多。
∙说明:有太多打开的套接字。
每个实现都可能具有套接字句柄可用的最大数目。
这些句柄可能会提供每个进程的全局,或每个线程。
WSAEWOULDBLOCK (10035)∙翻译:资源暂时不可用。
∙说明:将返回此错误,无法立即完成,例如,非阻塞套接字操作从接收(Wsapiref_2i9e.asp)时无数据排队要从套接字读取。
一、概述在计算机编程中,socket编程是一种常见的网络通信方式,通过socket可以实现不同主机之间的网络通信。
在使用socket编程时,经常会遇到各种错误,而这些错误通常会用errno枚举来表示。
errno 枚举定义了各种可能出现的错误类型,程序员可以根据errno的值来判断程序运行时出现的具体错误,从而进行相应的处理。
二、errno枚举errno枚举定义了许多可能出现的错误类型,下面我将按照错误类型进行分类介绍。
1. 常见错误类型在socket编程中,常见的错误类型包括但不限于以下几种:- EACCES:权限不足,通常指的是对某些系统资源的权限不够。
- EADDRINUSE:位置区域已被使用,通常指的是在绑定socket位置区域时,该位置区域已被其他进程占用。
- EAG本人N:资源暂时不可用,通常指的是资源暂时不可用,需要稍后重试。
- ECONNREFUSED:连接被拒绝,通常指的是远程主机拒绝连接请求。
- EFAULT:位置区域错误,通常指的是指针参数指向的位置区域无效。
- EINTR:中断系统调用,通常指的是系统调用被信号中断。
- EINVAL:无效参数,通常指的是传递给系统调用的参数无效。
- EIO:I/O错误,通常指的是发生了I/O错误。
- EISCONN:已连接,通常指的是socket已经连接。
2. 其他错误类型除了上述常见的错误类型外,errno枚举还定义了许多其他的错误类型,程序员在使用socket编程时可以根据实际情况进行具体的处理。
三、errno值每个错误类型在errno枚举中都有对应的数值,程序员可以通过这些数值来判断程序运行时出现的具体错误。
下面我将列举一些常见的errno值:1. EACCES的值为132. EADDRINUSE的值为483. EAG本人N的值为114. ECONNREFUSED的值为1115. EFAULT的值为146. EINTR的值为47. EINVAL的值为228. EIO的值为59. EISCONN的值为106四、处理错误在程序编写过程中,我们需要针对不同的错误类型进行相应的处理,以保证程序的健壮性和稳定性。
在 4.2 BSD UNIX® 操作系统中首次引入,Sockets API 现在是任何操作系统的标准特性。
事实上,很难找到一种不支持Sockets API 的现代语言。
该API 相当简单,但新的开发人员仍然会遇到一些常见的隐患。
本文识别那些隐患并向您显示如何避开它们。
隐患1.忽略返回状态第一个隐患很明显,但它是开发新手最容易犯的一个错误。
如果您忽略函数的返回状态,当它们失败或部分成功的时候,您也许会迷失。
反过来,这可能传播错误,使定位问题的源头变得困难。
捕获并检查每一个返回状态,而不是忽略它们。
考虑清单1 显示的例子,一个套接字send 函数。
清单 1. 忽略API 函数返回状态int status, sock, mode;/* Create a new stream (TCP) socket */sock = socket( AF_INET, SOCK_STREAM, 0 );...status = send( sock, buffer, buflen, MSG_DONTW AIT );if (status == -1) {/* send failed */printf( "send failed: %s\n", strerror(errno) );} else {/* send succeeded -- or did it? */}清单 1 探究一个函数片断,它完成套接字send 操作(通过套接字发送数据)。
函数的错误状态被捕获并测试,但这个例子忽略了send 在无阻塞模式(由MSG_DONTWAIT 标志启用)下的一个特性。
send API 函数有三类可能的返回值:∙如果数据成功地排到传输队列,则返回0。
∙如果排队失败,则返回-1(通过使用errno 变量可以了解失败的原因)。
∙如果不是所有的字符都能够在函数调用时排队,则最终的返回值是发送的字符数。
由于send 的MSG_DONTWAIT 变量的无阻塞性质,函数调用在发送完所有的数据、一些数据或没有发送任何数据后返回。
socket 读写超时详解摘要:1.概述2.Socket 读写超时的原因3.Socket 读写超时的解决方法4.总结正文:1.概述在网络编程中,Socket 是一种应用层协议,它提供了一个应用程序与网络之间的接口,使得数据能够在网络中进行传输。
在使用Socket 进行网络通信时,可能会遇到读写超时的问题。
本文将详细解释Socket 读写超时的原因,并提供相应的解决方法。
2.Socket 读写超时的原因Socket 读写超时通常是由于以下原因导致的:(1) 网络拥堵:当网络中的数据包数量过多,导致网络拥堵时,数据包的传输速度会降低,从而导致Socket 读写超时。
(2) 服务器负载过高:当服务器的负载过高时,它可能无法及时响应客户端的请求,从而导致Socket 读写超时。
(3) 网络故障:当网络出现故障时,数据包的传输可能会受到影响,从而导致Socket 读写超时。
(4) 程序错误:当程序中存在错误时,可能会导致Socket 读写超时。
3.Socket 读写超时的解决方法针对Socket 读写超时的问题,可以采取以下措施进行解决:(1) 优化网络环境:尽量避免在网络拥堵的时段进行数据传输,或者使用质量更高的网络线路。
(2) 提高服务器性能:通过提高服务器的性能,可以降低服务器的负载,从而提高其响应速度。
(3) 调整Socket 超时设置:通过调整Socket 的超时设置,可以增加读写操作的时间,从而减少超时发生的概率。
在设置超时时间时,需要根据实际情况进行调整,以保证既能减少超时发生的概率,又不会影响程序的性能。
(4) 检查程序代码:通过检查程序代码,可以发现并修复程序中存在的错误,从而避免Socket 读写超时的问题。
4.总结Socket 读写超时是网络编程中常见的问题,它可能会导致程序无法正常运行。
实验6. Linux Socket 编程实验1、实验目的:(1) 了解TCP/IP 协议;(2) 掌握socket 编程。
2、实验设备:(1) PC 机的VMware 虚拟机运行Ubuntu Linux 系统;(2) 两机对连的网络线;(3) 带网口的测试计算机;(4) WINDOWS“SOCKET TOOL”调试工具。
3、实验内容:实现典型客户机/服务器程序中的服务器及客户机。
4、实验原理4.1 客户机/服务器工作流程使用TCP协议的客户机/服务器进程的工作过程如下图4.2 Socket 编程相关函数常用的socket函数有:socket,bind,listen,accept,connect,send,recv。
1)socket(建立连接)表头文件:#include<sys/types.h>#include<sys/socket.h>定义函数:int socket(int family,int type,int protocol);函数说明:socket()函数用来生成一个套接口描述字,也称为套接字,指定协议簇和套接口。
参数:family指定协议族,type指明字节流方式,而protocol一般为0Family的取值范围:AF_LOCALUNIX协议族AF_ROUTE路由套接口AF_INETIPv4协议AF_INET6IPv6协议AF_KEY密钥套接口参数type的取值范围:SOCK_STREAMTCP套接口SOCK_DGRAMUDP套接口SOCK_PACKET支持数据链路访问SOCK_RAM原始套接口返回值:成功返回非负描述字,失败返回负值2)bind(对socket定位)表头文件:#include<sys/types.h>#include<sys/socket.h>定义函数:Int bind(int sockfd,struct sockaddr * my_addr,int addrlen);函数说明bind()用来设置给参数sockfd的socket一个名称。
Linux socket通信出现CLOSE_WAIT状态的原因与解决方法这个问题之前没有怎么留意过,是最近在面试过程中遇到的一个问题,面了两家公司,两家公司竟然都面到到了这个问题,不得不使我开始关注这个问题。
说起CLOSE_WAIT状态,如果不知道的话,还是先瞧一下TCP的状态转移图吧。
关闭socket分为主动关闭(Active closure)和被动关闭(Passive closure)两种情况。
前者是指有本地主机主动发起的关闭;而后者则是指本地主机检测到远程主机发起关闭之后,作出回应,从而关闭整个连接。
将关闭部分的状态转移摘出来,就得到了下图:产生原因通过TCP状态转换图,我们来分析,什么情况下,连接处于CLOSE_WAIT状态呢?在被动关闭连接情况下,在已经接收到FIN,但是还没有发送自己的FIN的时刻,连接处于CLOSE_WAIT状态。
通常来讲,CLOSE_WAIT状态的持续时间应该很短,正如SYN_RCVD状态。
但是在一些特殊情况下,就会出现连接长时间处于CLOSE_WAIT状态的情况。
出现大量close_wait的现象,主要原因是某种情况下对方关闭了socket链接,但是我方忙与读或者写,没有关闭连接。
代码需要判断socket,一旦读到0,断开连接,read返回负,检查一下errno,如果不是AGAIN,就断开连接。
参考资料4中描述,通过发送SYN-FIN报文来达到产生CLOSE_WAIT状态连接,没有进行具体实验。
不过个人认为协议栈会丢弃这种非法报文,感兴趣的同学可以测试一下,然后把结果告诉我;-)为了更加清楚的说明这个问题,我们写一个测试程序,注意这个测试程序是有缺陷的。
只要我们构造一种情况,使得对方关闭了socket,我们还在read,或者是直接不关闭socket就会构造这样的情况。
server.c:#include#include#include#define MAXLINE 80#define SERV_PORT 8000int main(void){struct sockaddr_in servaddr, cliaddr;socklen_t cliaddr_len;int listenfd, connfd;char buf[MAXLINE];char str[INET_ADDRSTRLEN];int i, n;listenfd = socket(AF_INET, SOCK_STREAM, 0);int opt = 1;setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));bzero(&servaddr, sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_addr.s_addr = htonl(INADDR_ANY);servaddr.sin_port = htons(SERV_PORT);bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));listen(listenfd, 20);printf("Accepting connections ...\n");while (1) {cliaddr_len = sizeof(cliaddr);connfd = accept(listenfd,(struct sockaddr *)&cliaddr, &cliaddr_len);//while (1){n = read(connfd, buf, MAXLINE);if (n == 0) {printf("the other side has been closed.\n");break;}printf("received from %s at PORT %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));for (i = 0; i //这里故意不关闭socket,或者是在close之前加上一个sleep都可以//sleep(5);//close(connfd);}}client.c:#include#include#include#include#include#include#define MAXLINE 80#define SERV_PORT 8000int main(int argc, char *argv[]){struct sockaddr_in servaddr;char buf[MAXLINE];int sockfd, n;char *str;if (argc != 2) { fputs("usage: ./client message\n", stderr);exit(1);}str = argv[1];sockfd = socket(AF_INET, SOCK_STREAM, 0);bzero(&servaddr, sizeof(servaddr));servaddr.sin_family = AF_INET;inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);servaddr.sin_port = htons(SERV_PORT);connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));write(sockfd, str, strlen(str));n = read(sockfd, buf, MAXLINE);printf("Response from server:\n");write(STDOUT_FILENO, buf, n);write(STDOUT_FILENO, "\n", 1);close(sockfd);return 0;}结果如下:debian-wangyao:~$ ./client aResponse from server:Adebian-wangyao:~$ ./client bResponse from server:Bdebian-wangyao:~$ ./client cResponse from server:Cdebian-wangyao:~$ netstat -antp | grep CLOSE_WAIT(Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.)tcp 1 0 127.0.0.1:8000 127.0.0.1:58309 CLOSE_WAIT 6979/servertcp 1 0 127.0.0.1:8000 127.0.0.1:58308 CLOSE_WAIT 6979/servertcp 1 0 127.0.0.1:8000 127.0.0.1:58307 CLOSE_WAIT 6979/server解决方法:基本的思想就是要检测出对方已经关闭的socket,然后关闭它。
常见SOCK ET错误返回码(转)WSAEIN TR (10004)被中断的系统呼叫当以阻拦式进行的Win Sock函式被WSA Cance lBloc kingC all()中断的时候,这个阻拦式函式会得到W SAEIN TR这个错误讯息。
读者要注意的是,当你的程式有用WSAC ancel Block ingCa ll去中断阻拦式函式的时候,你必须为这个阻拦式函式写处理WS AEINT R错误讯息的程式码,否则你的程式可能会出现些无预期的错误。
原则上,所有能以阻拦式进行的函式都可能会发生这个错误。
WSAEBA DF (10009)错误的档案代码柏克莱soc ket介面中,档案描述子和socke t描述子是相通的,开启sock et可以想像成开启一个档案。
WSAEBA DF在柏克莱sock et介面的意义是指错误的soc ket描述子,这个错误的s ocket描述子可能是未经开启的sock et或是以关闭的so cket。
在WinSo ck中有个相通的错误代码WSA ENOTS OCK,用来指定错误的sock et描述子。
详细说明请参考WSAE NOTSO CK部分。
WSAEAC CES (10013)无此权限对于柏克莱s ocket介面函式,这个错误发生的原因通常是开启一个不具有开启权限的档案或sock et。
例如在UNI X上,一般的使用者不能开启S OCK_R AW的so cket,通常是超级使用者(superuser)才有此权限。
如果一般的使用者企图开启SOCK_RAW的socke t,将会得到这个错误讯息。
对于WinS ock API函式介面,发生此错误的函式有二:send()和sendt o()。
Linux系统错误码对照表C Name Value DescriptionEPERM1Operation not permittedENOENT2No such file or directoryESRCH3No such processEINTR4Interrupted system callEIO5I/O errorENXIO6No such device or addressE2BIG7Arg list too longENOEXEC8Exec format errorEBADF9Bad file numberECHILD10No child processesEAGAIN11Try againENOMEM12Out of memoryEACCES13Permission deniedEFAULT14Bad addressENOTBLK15Block device requiredEBUSY16Device or resource busyEEXIST17File existsEXDEV18Cross-device linkENODEV19No such deviceENOTDIR20Not a directoryEISDIR21Is a directoryEINVAL22Invalid argumentENFILE23File table overflowEMFILE24Too many open filesENOTTY25Not a tty deviceETXTBSY26Text file busyEFBIG27File too largeENOSPC28No space left on deviceESPIPE29Illegal seekEROFS30Read-only file systemEMLINK31Too many linksEPIPE32Broken pipeEDOM33Math argument out of domainERANGE34Math result not representableEDEADLK35Resource deadlock would occurENAMETOOLONG36Filename too longENOLCK37No record locks availableENOSYS38Function not implementedENOTEMPTY39Directory not emptyELOOP40Too many symbolic links encounteredEWOULDBLOCK41Same as EAGAINENOMSG42No message of desired typeEIDRM43Identifier removedECHRNG44Channel number out of rangeEL2NSYNC45Level 2 not synchronizedEL3HLT46Level 3 haltedEL3RST47Level 3 resetELNRNG48Link number out of rangeEUNATCH49Protocol driver not attachedENOCSI50No CSI structure availableEL2HLT51Level 2 haltedEBADE52Invalid exchangeEBADR53Invalid request descriptorEXFULL54Exchange fullEXFULL54Exchange fullENOANO55No anodeEBADRQC56Invalid request codeEBADSLT57Invalid slotEDEADLOCK -Same as EDEADLKEBFONT59Bad font file formatENOSTR60Device not a streamENODATA61No data availableETIME62Timer expiredENOSR63Out of streams resourcesENONET64Machine is not on the network ENOPKG65Package not installedEREMOTE66Object is remoteENOLINK67Link has been severedEADV68Advertise errorESRMNT69Srmount errorECOMM70Communication error on send EPROTO71Protocol errorEMULTIHOP72Multihop attemptedEDOTDOT73RFS specific errorEBADMSG74Not a data messageEOVERFLOW75Value too large for defined data type ENOTUNIQ76Name not unique on networkEBADFD77File descriptor in bad state EREMCHG78Remote address changedELIBACC79Cannot access a needed shared library ELIBBAD80Accessing a corrupted shared library ELIBSCN81 A .lib section in an .out is corrupted ELIBMAX82Linking in too many shared libraries ELIBEXEC83Cannot exec a shared library directly EILSEQ84Illegal byte sequenceERESTART85Interrupted system call should be restarted ESTRPIPE86Streams pipe errorEUSERS87Too many usersENOTSOCK88Socket operation on non-socket EDESTADDRREQ89Destination address required EMSGSIZE90Message too longEPROTOTYPE91Protocol wrong type for socket ENOPROTOOPT92Protocol not available EPROTONOSUPPORT93Protocol not supported ESOCKTNOSUPPORT94Socket type not supported EOPNOTSUPP95Operation not supported on transport EPFNOSUPPORT96Protocol family not supported EAFNOSUPPORT97Address family not supported by protocol EADDRINUSE98Address already in use EADDRNOTAVAIL99Cannot assign requested address ENETDOWN100Network is downENETUNREACH101Network is unreachableENETRESET102Network dropped ECONNABORTED103Software caused connection ECONNRESET104Connection reset byENOBUFS105No buffer space availableEISCONN106Transport endpointENOTCONN107Transport endpointESHUTDOWN108Cannot send after transport ETOOMANYREFS109Too many referencesETIMEDOUT110Connection timed ECONNREFUSED111Connection refusedECONNREFUSED111Connection refused EHOSTDOWN112Host is down EHOSTUNREACH113No route to host EALREADY114Operation already EINPROGRESS115Operation now in ESTALE116Stale NFS file handle EUCLEAN117Structure needs cleaning ENOTNAM118Not a XENIX-named ENAVAIL119No XENIX semaphores EISNAM120Is a named type file EREMOTEIO121Remote I/O error EDQUOT122Quota exceeded ENOMEDIUM123No medium found EMEDIUMTYPE124Wrong medium type。
Linux错误代码含义常⽤Linux错误代码含义,如下表所⽰:名称值描述EPERM1操作不允许ENOENT2⽆此⽂件或⽬录ESRCH3⽆此进程EINTR4中断系统调⽤EIO5I/O 错误ENXIO6⽆此设备或地址E2BIG7参数列表过长ENOEXEC8执⾏⽂件错误EBADF9错误的⽂件数ECHILD10⽆⼦进程EAGAIN11再尝试⼀下ENOMEM12内存溢出EACCES13要求被否定EFAULT14错误地址ENOTBLK15块设备请求EBUSY16设备或者资源忙EEXIST17⽂件存在EXDEV18Cross-device linkENODEV19⽆此设备ENOTDIR20不是⼀个⽬录⽂件EISDIR21I是⼀个⽬录⽂件EINVAL22⽆效的参数ENFILE23⽂件表溢出EMFILE24打开⽂件过多ENOTTY25不是⼀个TTY设备ETXTBSY26⽂件忙EFBIG27⽂件过⼤ENOSPC28此设备上没有空间了ESPIPE29⽆效的偏移EROFS30只读⽂件系统EMLINK31链接过多EPIPE32错误的管道EDOM33Math argument out of domainERANGE34Math result not representableEDEADLK35Resource deadlock would occur ENAMETOOLONG36⽂件名过长ENOLCK37No record locks availableENOSYS38函数没有实现ENOTEMPTY39⽬录⾮空ELOOP40Too many symbolic links encountered EWOULDBLOCK41Same as EAGAINENOMSG42No message of desired typeEIDRM43Identifier removedECHRNG44Channel number out of rangeEL2NSYNC45Level 2 not synchronizedEL3HLT46Level 3 haltedEL3RST47Level 3 resetELNRNG48Link number out of rangeEUNATCH49Protocol driver not attachedENOCSI50No CSI structure availableEL2HLT51Level 2 haltedEBADE52Invalid exchangeEBADR53Invalid request descriptorEXFULL54Exchange fullENOANO55No anodeEBADRQC56Invalid request codeEBADSLT57Invalid slotEDEADLOCK -Same as EDEADLKEBFONT59Bad font file formatENOSTR60Device not a streamENODATA61No data availableETIME62Timer expiredETIME62Timer expiredENOSR63Out of streams resourcesENONET64Machine is not on the network ENOPKG65Package not installedEREMOTE66Object is remoteENOLINK67Link has been severedEADV68Advertise errorESRMNT69Srmount errorECOMM70Communication error on send EPROTO71Protocol errorEMULTIHOP72Multihop attemptedEDOTDOT73RFS specific errorEBADMSG74Not a data messageEOVERFLOW75Value too large for defined data type ENOTUNIQ76Name not unique on networkEBADFD77File descriptor in bad state EREMCHG78Remote address changedELIBACC79Cannot access a needed shared library ELIBBAD80Accessing a corrupted shared library ELIBSCN81 A .lib section in an .out is corrupted ELIBMAX82Linking in too many shared libraries ELIBEXEC83Cannot exec a shared library directly EILSEQ84Illegal byte sequenceERESTART85Interrupted system call should be restarted ESTRPIPE86Streams pipe errorEUSERS87Too many usersENOTSOCK88Socket operation on non-socket EDESTADDRREQ89Destination address required EMSGSIZE90Message too longEPROTOTYPE91Protocol wrong type for socket ENOPROTOOPT92Protocol not available EPROTONOSUPPORT93Protocol not supported ESOCKTNOSUPPORT94Socket type not supported EOPNOTSUPP95Operation not supported on transport EPFNOSUPPORT96Protocol family not supported EAFNOSUPPORT97Address family not supported by protocol EADDRINUSE98Address already in use EADDRNOTAVAIL99Cannot assign requested address ENETDOWN100Network is downENETUNREACH101Network is unreachable ENETRESET102Network dropped ECONNABORTED103Software caused connection ECONNRESET104Connection reset byENOBUFS105No buffer space availableEISCONN106Transport endpointENOTCONN107Transport endpointESHUTDOWN108Cannot send after transport ETOOMANYREFS109Too many referencesETIMEDOUT110Connection timed ECONNREFUSED111Connection refusedEHOSTDOWN112Host is downEHOSTUNREACH113No route to hostEALREADY114Operation alreadyEINPROGRESS115Operation now inESTALE116Stale NFS file handleEUCLEAN117Structure needs cleaningENOTNAM118Not a XENIX-namedENAVAIL119No XENIX semaphoresEISNAM120Is a named type fileEREMOTEIO121Remote I/O errorEDQUOT122Quota exceededENOMEDIUM123No medium foundEMEDIUMTYPE124Wrong medium type。
常见的Linux系统错误码(5篇可选)第一篇:常见的Linux系统错误码常见的Linux系统错误码,即errorno的值EPERM Operation not permitted 操作不许可 ENOENT No such file or directory 无此文件或目录ESRCH No such process 无此过程EINTR Interrupted system call 系统调用被禁止EIO I/O error I/O 错误 ENXIO No such device or address 无此器件或地址E2BIG Arg list too long Arg 列表太长ENOEXEC Exec format error Exec 格式错误EBADF Bad file number 文件数目错误 10 ECHILD No child processes 无子过程 11 EAGAIN Try again 再试一遍 ENOMEM Out of memory 内存溢出 13 EACCES Permission denied 许可拒绝 14 EFAULT Bad address 错误的地址ENOTBLK Block device required 需要块设备16 EBUSY Device or resource busy 设备或资源忙 17 EEXIST File exists 文件存在 EXDEV Cross-device link 跨器链接 19 ENODEV No such device 无此设备ENOTDIR Not a directory 不是一个目录21 EISDIR Is a directory 是一个目录EINVAL Invalid argument 无效的函数自变量23 ENFILE File table overflow 文件表溢出 EMFILE T oo many open files 打开的文件太多 25 ENOTTY Inappropriate ioctl for device 26 ETXTBSY Text file busy 文本文件忙 27 EFBIG File too large 文件太大 ENOSPC No space left on device 磁盘空间不足 29 ESPIPE Illegal seek 不合法的寻找 EROFS Read-only file system 只读文件系统 31 EMLINK Too many links 太多的链接/usr/include/asm-generic/errno-base.h#define EPERM/* Operation not permitted */ #define ENOENT/* No such file or directory */ #define ESRCH/* No such process */ #define EINTR/* Interrupted system call */ #define EIO 5 /* I/O error */ #define ENXIO/* No such device or address */ #define E2BIG/* Argument list too long */ #define ENOEXEC/* Exec format error */ #define EBADF/* Bad file number */ #define ECHILD/* No child processes */ #define EAGAIN/* Try again */ #define ENOMEM/* Out of memory */ #define EACCES/* Permission denied */ #define EFAULT/* Bad address */ #define ENOTBLK/* Block device required */ #define EBUSY/* Device or resource busy */ #define EEXIST/* File exists */ #define EXDEV/* Cross-device link */ #define ENODEV/* No such device */ #define ENOTDIR/* Not a directory */ #define EISDIR/* Is a directory */ #define EINVAL/* Invalid argument */ #define ENFILE/* File table overflow */ #define EMFILE/* Too many open files */ #define ENOTTY/* Not a typewriter */ #define ETXTBSY/* Text file busy */ #define EFBIG/* File too large */ #define ENOSPC/* No space left on device */ #define ESPIPE/* Illegal seek */ #define EROFS/* Read-only file system */ #define EMLINK/* Too many links */ #define EPIPE 32 /* Broken pipe */ #define EDOM/* Math argument out of domain of func */ #define ERANGE /* Math result not representable *//usr/include/asm-generic/errno.h#define EDEADLK/* Resource deadlock would occur */ #define ENAMETOOLONG 36 /* File name too long */ #define ENOLCK /* No record locks available */ #define ENOSYS/* Function not implemented */ #define ENOTEMPTY/* Directory not empty */ #define ELOOP/* Too many symbolic links encountered */ #define EWOULDBLOCK EAGAIN /* Operation would block */ #define ENOMSG/* No message of desired type */ #define EIDRM/* Identifier removed */ #define ECHRNG/* Channel number out of range */ #define EL2NSYNC 45 /* Level 2 not synchronized */ #define EL3HLT/* Level 3 halted */ #define EL3RST/* Level 3 reset */ #define ELNRNG/* Link number out of range */ #define EUNATCH/* Protocol driver not attached */ #define ENOCSI/* No CSI structure available */ #define EL2HLT/* Level 2 halted */ #define EBADE/* Invalid exchange */ #define EBADR/* Invalid request descriptor */ #define EXFULL/* Exchange full */ #define ENOANO/* No anode */ #define EBADRQC/* Invalid request code */ #define EBADSLT/* Invalid slot */ #define EDEADLOCKEDEADLK#define EBFONT/* Bad font file format */ #define ENOSTR/* Device not a stream */ #define ENODATA/* No data available */ #define ETIME/* Timer expired */ #define ENOSR/* Out of streams resources */ #define ENONET/* Machine is not on the network */ #define ENOPKG/* Package not installed */ #define EREMOTE/* Object is remote */ #define ENOLINK/* Link has been severed */ #define EADV/* Advertise error */ #define ESRMNT/* Srmount error */ #define ECOMM/* Communication error on send */ #define EPROTO/* Protocol error */ #define EMULTIHOP/* Multihop attempted */ #define EDOTDOT/* RFS specific error */ #define EBADMSG/* Not a data message */ #define EOVERFLOW/* Value too large for defined data type */ #define ENOTUNIQ 76 /* Name not unique on network */ #define EBADFD/* File descriptor in bad state */ #define EREMCHG/* Remote address changed */ #define ELIBACC/* Can not access a needed shared library */ #define ELIBBAD /* Accessing a corrupted shared library */ #define ELIBSCN /*.lib section in a.out corrupted */ #define ELIBMAX/* Attempting to link in too many shared libraries */ #define ELIBEXEC 83 /* Cannot exec a shared library directly */ #define EILSEQ/* Illegal byte sequence */ #define ERESTART 85 /* Interrupted system call should be restarted */ #define ESTRPIPE 86 /* Streams pipe error */ #define EUSERS/* Too many users */ #define ENOTSOCK 88 /* Socket operation on non-socket */ #define EDESTADDRREQ 89 /* Destination address required */ #define EMSGSIZE 90 /* Messagetoo long */ #define EPROTOTYPE 91 /* Protocol wrong type for socket */ #define ENOPROTOOPT 92 /* Protocol not available */ #define EPROTONOSUPPORT 93 /* Protocol not supported */ #define ESOCKTNOSUPPORT 94 /* Socket type not supported */ #define EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ #define EPFNOSUPPORT 96 /* Protocol family not supported */ #define EAFNOSUPPORT 97 /* Address family not supported by protocol */ #define EADDRINUSE 98 /* Address already in use */ #define EADDRNOTAVAIL 99 /* Cannot assign requested address */ #define ENETDOWN 100 /* Network is down */ #define ENETUNREACH 101 /* Network is unreachable */ #define ENETRESET/* Network dropped connection because of reset */ #define ECONNABORTED 103 /* Software caused connection abort */ #define ECONNRESET 104 /* Connection reset by peer */ #define ENOBUFS/* No buffer space available */ #define EISCONN/* Transport endpoint is already connected */ #define ENOTCONN 107 /* Transport endpoint is not connected */ #define ESHUTDOWN/* Cannot send after transport endpoint shutdown */ #define ETOOMANYREFS 109 /* Too many references: cannot splice */ #define ETIMEDOUT/* Connection timed out */ #define ECONNREFUSED 111 /* Connection refused */ #define EHOSTDOWN/* Host is down */ #define EHOSTUNREACH 113 /* No route to host */ #define EALREADY 114 /* Operation already in progress */ #define EINPROGRESS 115 /* Operation now in progress */ #define ESTALE/* Stale NFS file handle */ #define EUCLEAN/* Structure needs cleaning */ #define ENOTNAM/* Not a XENIX named type file */ #define ENAVAIL/* No XENIX semaphores available */ #define EISNAM/* Is a named type file */ #define EREMOTEIO/* Remote I/O error */ #define EDQUOT/* Quota exceeded */#define ENOMEDIUM 123 /* No medium found */ #define EMEDIUMTYPE 124 /* Wrong medium type */ #define ECANCELED/* Operation Canceled */ #define ENOKEY 126 /* Required key not available */ #define EKEYEXPIRED 127 /* Key has expired */ #define EKEYREVOKED 128 /* Key has been revoked */ #define EKEYREJECTED 129 /* Key was rejected by service */ /* for robust mutexes */ #define EOWNERDEAD 130 /* Owner died */ #define ENOTRECOVERABLE 131 /* State not recoverable */第二篇:http错误码http 错误代码表所有 HTTP 状态代码及其定义。
Socket错误码大全errno.00 is: Error 0errno.01 is: Not ownererrno.02 is: No such file or directoryerrno.03 is: No such processerrno.04 is: Interrupted system callerrno.05 is: I/O errorerrno.06 is: No such device or addresserrno.07 is: Arg list too longerrno.08 is: Exec format errorerrno.09 is: Bad file numbererrno.10 is: No child processeserrno.11 is: Resource temporarily unavailableerrno.12 is: Not enough spaceerrno.13 is: Permission deniederrno.14 is: Bad addresserrno.15 is: Block device requirederrno.16 is: Device busyerrno.17 is: File existserrno.18 is: Cross-device linkerrno.19 is: No such deviceerrno.20 is: Not a directoryerrno.21 is: Is a directoryerrno.22 is: Invalid argumenterrno.23 is: File table overflowerrno.24 is: Too many open fileserrno.25 is: Not a typewritererrno.26 is: Text file busyerrno.27 is: File too largeerrno.28 is: No space left on deviceerrno.29 is: Illegal seekerrno.30 is: Read-only file systemerrno.31 is: Too many linkserrno.32 is: Broken pipeerrno.33 is: Argument out of domainerrno.34 is: Result too largeerrno.35 is: No message of desired typeerrno.36 is: Identifier removederrno.37 is: Channel number out of rangeerrno.38 is: Level 2 not synchronizederrno.39 is: Level 3 haltederrno.40 is: Level 3 reseterrno.41 is: Link number out of rangeerrno.42 is: Protocol driver not attachederrno.43 is: No CSI structure availableerrno.44 is: Level 2 haltederrno.45 is: Deadlock condition if locked errno.46 is: Device not readyerrno.47 is: Write-protected mediaerrno.48 is: Unformatted or incompatible media errno.49 is: No locks availableerrno.50 is: Cannot Establish Connection errno.51 is: Connection Downerrno.52 is: Missing file or filesystemerrno.53 is: Requests blocked by Administrator errno.54 is: Operation would blockerrno.55 is: Operation now in progresserrno.56 is: Operation already in progress errno.57 is: Socket operation on non-socket errno.58 is: Destination address required errno.59 is: Message too longerrno.60 is: Protocol wrong type for socket errno.61 is: Protocol not availableerrno.62 is: Protocol not supportederrno.63 is: Socket type not supportederrno.64 is: Operation not supported on socket errno.65 is: Protocol family not supported errno.66 is: Addr family not supported by protocol errno.67 is: Address already in useerrno.68 is: Can't assign requested address errno.69 is: Network is downerrno.70 is: Network is unreachableerrno.71 is: Network dropped connection on reset errno.72 is: Software caused connection abort errno.73 is: Connection reset by peererrno.74 is: No buffer space availableerrno.75 is: Socket is already connectederrno.76 is: Socket is not connectederrno.77 is: Can't send after socket shutdown errno.78 is: Connection timed outerrno.79 is: Connection refusederrno.80 is: Host is downerrno.81 is: No route to hosterrno.82 is: Restart the system callerrno.83 is: Too many processeserrno.84 is: Too many userserrno.85 is: Too many levels of symbolic links errno.86 is: File name too longerrno.87 is: Directory not emptyerrno.88 is: Disk quota exceedederrno.89 is: Invalid file system control data detected errno.90 is: For future useerrno.91 is: For future useerrno.92 is: For future useerrno.93 is: Item is not local to hosterrno.94 is: For future useerrno.95 is: For future useerrno.96 is: For future useerrno.97 is: For future useerrno.98 is: For future useerrno.99 is: For future useerrno.100 is: For future useerrno.101 is: For future useerrno.102 is: For future useerrno.103 is: For future useerrno.104 is: For future useerrno.105 is: For future useerrno.106 is: For future useerrno.107 is: For future useerrno.108 is: For future useerrno.109 is: Function not implementederrno.110 is: Media surface errorerrno.111 is: I/O completed, but needs relocation errno.112 is: No attribute founderrno.113 is: Security Authentication Denied errno.114 is: Not a Trusted Programerrno.115 is: Too many references: can't splice errno.116 is: Invalid wide charactererrno.117 is: Asynchronous I/O cancellederrno.118 is: Out of STREAMS resourceserrno.119 is: System call timed outerrno.120 is: Next message has wrong typeerrno.121 is: Error in protocolerrno.122 is: No message on stream head read q errno.123 is: fd not associated with a stream errno.124 is: Unsupported attribute valueerrno.125 is: Multihop is not allowederrno.126 is: The server link has been severed errno.127 is: Value too large to be stored in data type打印fangshiintnPrintErrno()/*打印socket错误*/ {inti = 0;for(i = 0; i< 256; i++)printf("errno.%02d is: %s\n", i, strerror(i));return 0;}。
代码错误信息解释0 操作成功完成。
1 函数不正确。
2 系统找不到指定的文件。
3 系统找不到指定的路径。
4 系统无法打开文件。
5 拒绝访问。
6 句柄无效。
7 存储控制块被损坏。
8 存储空间不足,无法处理此命令。
9 存储控制块地址无效。
10 环境不正确。
11 试图加载格式不正确的程序。
12 访问码无效。
13 数据无效。
14 存储空间不足,无法完成此操作。
15 系统找不到指定的驱动器。
16 无法删除目录。
17 系统无法将文件移到不同的驱动器。
18 没有更多文件。
19 介质受写入保护。
20 系统找不到指定的设备。
21 设备未就绪。
22 设备不识别此命令。
23 数据错误(循环冗余检查)。
24 程序发出命令,但命令长度不正确。
25 驱动器找不到磁盘上特定区域或磁道。
26 无法访问指定的磁盘或软盘。
27 驱动器找不到请求的扇区。
28 打印机缺纸。
29 系统无法写入指定的设备。
30 系统无法从指定的设备上读取。
31 连到系统上的设备没有发挥作用。
32 另一个程序正在使用此文件,进程无法访问。
33 另一个程序已锁定文件的一部分,进程无法访问。
36 用来共享的打开文件过多。
38 已到文件结尾。
39 磁盘已满。
50 不支持请求。
51 Window s 无法找到网络路径。
请确认网络路径正确并且目标计算机不忙或已关闭。
如果Window s 仍然无法找到网络路径,请与网络管理员联系。
52 由于网络上有重名,没有连接。
请到“控制面板”中的“系统”更改计算机名,然后重试。
53 找不到网络路径。
54 网络很忙。
55 指定的网络资源或设备不再可用。
56 已达到网络 BIOS 命令限制。
57 网络适配器硬件出错。
58 指定的服务器无法运行请求的操作。
59 出现了意外的网络错误。
Linux网络编程socket错误分析socket错误码:EINTR:4阻塞的操作被取消阻塞的调用打断。
如设置了发送接收超时,就会遇到这种错误。
只能针对阻塞模式的socket。
读,写阻塞的socket时,-1返回,错误号为INTR。
另外,如果出现EINTR即errno为4,错误描述Interrupted system call,操作也应该继续。
如果recv 的返回值为0,那表明连接已经断开,接收操作也应该结束。
ETIMEOUT:1101、操作超时。
一般设置了发送接收超时,遇到网络繁忙的情况,就会遇到这种错误。
2、服务器做了读数据做了超时限制,读时发生了超时。
3、错误被描述为“connect time out”,即“连接超时”,这种情况一般发生在服务器主机崩溃。
此时客户TCP 将在一定时间内(依具体实现)持续重发数据分节,试图从服务TCP 获得一个ACK 分节。
当最终放弃尝试后(此时服务器未重新启动),内核将会向客户进程返回ETIMEDOUT 错误。
如果某个中间路由器判定该服务器主机已经不可达,则一般会响应“destination unreachable”-“目的地不可达”的ICMP消息,相应的客户进程返回的错误是EHOSTUNREACH 或ENETUNREACH。
当服务器重新启动后,由于TCP 状态丢失,之前所有的连接信息也不存在了,此时对于客户端发来请求将回应RST。
如果客户进程对检测服务器主机是否崩溃很有必要,要求即使客户进程不主动发送数据也能检测出来,那么需要使用其它技术,如配置SO_KEEPALIVE Socket 选项,或实现某些心跳函数。
EAGAIN:1、Send返回值小于要发送的数据数目,会返回EAGAIN和EINTR。
2、recv 返回值小于请求的长度时说明缓冲区已经没有可读数据,但再读不一定会触发EAGAIN,有可能返回0表示TCP连接已被关闭。
3、当socket是非阻塞时,如返回此错误,表示写缓冲队列已满,可以做延时后再重试.4、在Linux进行非阻塞的socket接收数据时经常出现Resource temporarily unavailable,errno 代码为11(EAGAIN),表明在非阻塞模式下调用了阻塞操作,在该操作没有完成就返回这个错误,这个错误不会破坏socket的同步,不用管它,下次循环接着recv就可以。
对非阻塞socket而言,EAGAIN不是一种错误。
EPIPE:1、Socket 关闭,但是socket号并没有置-1。
继续在此socket上进行send和recv,就会返回这种错误。
这个错误会引发SIGPIPE信号,系统会将产生此EPIPE错误的进程杀死。
所以,一般在网络程序中,首先屏蔽此消息,以免发生不及时设置socket进程被杀死的情况。
2、write(..) on a socket that has been closed at the other end will cause a SIGPIPE.3、错误被描述为“broken pipe”,即“管道破裂”,这种情况一般发生在客户进程不理会(或未及时处理)Socket 错误,继续向服务TCP 写入更多数据时,内核将向客户进程发送SIGPIPE 信号,该信号默认会使进程终止(此时该前台进程未进行core dump)。
结合上边的ECONNRESET 错误可知,向一个FIN_WAIT2 状态的服务TCP(已ACK 响应FIN 分节)写入数据不成问题,但是写一个已接收了RST 的Socket 则是一个错误。
EBADF:read(..) or write(..) on a locally closed socket will return EBADFEFAULT:地址错误。
EBUSY:ECONNREFUSED:1、拒绝连接。
一般发生在连接建立时。
拔服务器端网线测试,客户端设置keep alive时,recv较快返回0,先收到ECONNREFUSED (Connection refused)错误码,其后都是ETIMEOUT。
2、an error returned from connect(), so it can only occur in a client (if a client is defined as the party that initiates the connectionECONNRESET:1、在客户端服务器程序中,客户端异常退出,并没有回收关闭相关的资源,服务器端会先收到ECONNRESET错误,然后收到EPIPE错误。
2、连接被远程主机关闭。
有以下几种原因:远程主机停止服务,重新启动;当在执行某些操作时遇到失败,因为设置了“keep alive”选项,连接被关闭,一般与ENETRESET一起出现。
3、远程端执行了一个“hard”或者“abortive”的关闭。
应用程序应该关闭socket,因为它不再可用。
当执行在一个UDP socket上时,这个错误表明前一个send操作返回一个ICMP “port unreachable”信息。
4、如果client关闭连接,server端的select并不出错(不返回-1,使用select对唯一一个socket 进行non- blocking检测),但是写该socket就会出错,用的是send.错误号:ECONNRESET.读(recv)socket并没有返回错误。
5、该错误被描述为“connection reset by peer”,即“对方复位连接”,这种情况一般发生在服务进程较客户进程提前终止。
当服务进程终止时会向客户TCP 发送FIN 分节,客户TCP 回应ACK,服务TCP 将转入FIN_WAIT2 状态。
此时如果客户进程没有处理该FIN (如阻塞在其它调用上而没有关闭Socket 时),则客户TCP 将处于CLOSE_WAIT 状态。
当客户进程再次向FIN_WAIT2 状态的服务TCP 发送数据时,则服务TCP 将立刻响应RST。
一般来说,这种情况还可以会引发另外的应用程序异常,客户进程在发送完数据后,往往会等待从网络IO接收数据,很典型的如read 或readline 调用,此时由于执行时序的原因,如果该调用发生在RST 分节收到前执行的话,那么结果是客户进程会得到一个非预期的EOF 错误。
此时一般会输出“server terminated prematurely”-“服务器过早终止”错误。
EINV AL:无效参数。
提供的参数非法。
有时也会与socket的当前状态相关,如一个socket并没有进入listening状态,此时调用accept,就会产生EINV AL错误。
EMFILE:打开了太多的socket。
对进程或者线程而言,每种实现方法都有一个最大的可用socket数目处理,或者是全局的,或者是局部的。
EWOULDBLOCK:EAGAIN资源暂时不可用。
这个错误是从对非阻塞socket进行的不能立即结束的操作返回的,如当没有数据在队列中可以读时,调用recv。
并不是fatal错误,稍后操作可以被重复。
调用在一个非阻塞的SOCK_STREAM socket 上调用connect时会产生这个错误,因为有时连接建立必须消耗一定的时间。
ENOTCONN在一个没有建立连接的socket上,进行read,write操作会返回这个错误。
出错的原因是socket没有标识地址。
Setsoc也可能会出错。
ECONNRESETConnection reset by peer.连接被远程主机关闭。
有以下几种原因:远程主机停止服务,重新启动;当在执行某些操作时遇到失败,因为设置了“keep alive”选项,连接被关闭,一般与ENETRESET一起出现。
ECONNABORTED1、软件导致的连接取消。
一个已经建立的连接被host方的软件取消,原因可能是数据传输超时或者是协议错误。
2、该错误被描述为“software caused connection abort”,即“软件引起的连接中止”。
原因在于当服务和客户进程在完成用于TCP 连接的“三次握手”后,客户TCP 却发送了一个RST (复位)分节,在服务进程看来,就在该连接已由TCP 排队,等着服务进程调用accept 的时候RST 却到达了。
POSIX 规定此时的errno 值必须ECONNABORTED。
源自Berkeley 的实现完全在内核中处理中止的连接,服务进程将永远不知道该中止的发生。
服务器进程一般可以忽略该错误,直接再次调用accept。
当TCP协议接收到RST数据段,表示连接出现了某种错误,函数read将以错误返回,错误类型为ECONNERESET。
并且以后所有在这个套接字上的读操作均返回错误。
错误返回时返回值小于0。
ENETUNREACH网络不可达。
Socket试图操作一个不可达的网络。
这意味着local的软件知道没有路由到达远程的host。
ENETRESET网络重置时丢失连接。
由于设置了"keep-alive"选项,探测到一个错误,连接被中断。
在一个已经失败的连接上试图使用setsockopt操作,也会返回这个错误。
EINPROGRESS:操作正在进行中。
一个阻塞的操作正在执行。
ENOTSOCK:在非socket上执行socket操作。
EDESTADDRREQ:需要提供目的地址。
在一个socket上的操作需要提供地址。
如往一个ADDR_ANY 地址上进行sendto操作会返回这个错误。
EMSGSIZE:消息体太长。
发送到socket上的一个数据包大小比内部的消息缓冲区大,或者超过别的网络限制,或是用来接收数据包的缓冲区比数据包本身小。
EPROTOTYPE协议类型错误。
标识了协议的Socket函数在不支持的socket上进行操作。
如ARPA Internet UDP协议不能被标识为SOCK_STREAM socket类型。
ENOPROTOOPT该错误不是一个Socket 连接相关的错误。
errno 给出该值可能由于,通过getsockopt 系统调用来获得一个套接字的当前选项状态时,如果发现了系统不支持的选项参数就会引发该错误。
EPROTONOSUPPORT不支持的协议。
系统中没有安装标识的协议,或者是没有实现。
如函数需要SOCK_DGRAM socket,但是标识了stream protocol.。
ESOCKTNOSUPPORTSocket类型不支持。
指定的socket类型在其address family中不支持。
如可选选中选项SOCK_RAW,但实现并不支持SOCK_RAW sockets。
EOPNOTSUPPOperation not supported.The attempted operation is not supported for the type of object referenced. Usually this occurs when a socket descriptor to a socket that cannot support this operation, for example, trying to accept a connection on a datagram socket.EPFNOSUPPORTProtocol family not supported.The protocol family has not been configured into the system or no implementation for it exists. Has a slightly different meaning to EAFNOSUPPORT, but is interchangeable in most cases, and all Windows Sockets functions that return one of these specify EAFNOSUPPORT.EAFNOSUPPORTAddress family not supported by protocol family.An address incompatible with the requested protocol was used. All sockets are created with an associated "address family" (i.e. AF_INET for Internet Protocols) and a generic protocol type (i.e. SOCK_STREAM). This error will be returned if an incorrect protocol is explicitly requested in the socket call, or if an address of the wrong family is used for a socket, e.g. in sendto.EADDRINUSEAddress already in use.Only one usage of each socket address (protocol/IP address/port) is normally permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that wasn't closed properly, or one that is still in the process of closing. For server applications that need to bind multiple sockets to the same port number, consider using setsockopt(SO_REUSEADDR). Client applications usually need not call bind at all - connect will choose an unused port automatically. When bind is called with a wild-card address (involving ADDR_ANY), a EADDRINUSE error could be delayed until the specific address is "committed." This could happen with a call to other function later, including connect, listen, Connect or JoinLeaf.EADDRNOTA V AILCannot assign requested address.The requested address is not valid in its context. Normally results from an attempt to bind to an address that is not valid for the local machine. This can also result from connect, sendto, Connect, JoinLeaf, or SendTo when the remote address or port is not valid for a remote machine (e.g. address or port 0).ENETDOWNNetwork is down.A socket operation encountered a dead network. This could indicate a serious failure of the network system (i.e. the protocol stack that the WinSock DLL runs over), the network interface, or the local network itself.ENOBUFSNo buffer space available.An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.EISCONNSocket is already connected.A connect request was made on an already connected socket. Some implementations also return this error if sendto is called on a connected SOCK_DGRAM socket (For SOCK_STREAM sockets, the to parameter in sendto is ignored), although other implementations treat this as a legal occurrence.连接过程可能出现的错误情况有:(1)如果客户机TCP协议没有接收到对它的SYN数据段的确认,函数以错误返回,错误类型为ETIMEOUT。