PHP socket error codes, basic fsockopen() troubleshooting

fsockopen() initiates a socket connection to the resource specified by hostname. It’s useful function, but if you encounter error, here are basic troubleshooting tips and table with all error codes explained:

To solve fsockopen problems, use this code to ouput error. That way you will something to start with.

$start_time = microtime(true);
$file      = fsockopen ($host, $port, $errno, $errstr, 15);
$stop_time  = microtime(true);
$status    = 0;

if (!$file) {
	$status = -1;  // Site is down
} else {
	fclose($file);
	$status = ($stop_time - $start_time) * 1000;
	$status = floor($status);
}

echo '<br />Ping:' . $status;
echo '<br />Error code:' . $errno;
echo '<br />Error string:' . $errstr;

Start with checking is you’re using right host address (double check the format, port number), are there any firewalls blocking connection. Aren’t you in host’s blacklist. If you’re accessing file/folder, do you have permissions for that.

fsockopen() error codes

Error code Constant Message
1 SOCKET_EPERM Operation not permitted
2 SOCKET_ENOENT No such file or directory
4 SOCKET_EINTR Interrupted system call
5 SOCKET_EIO I/O error
6 SOCKET_ENXIO No such device or address
7 SOCKET_E2BIG Arg list too long
9 SOCKET_EBADF Bad file number
11 SOCKET_EWOULDBLOCK Operation would block
11 SOCKET_EAGAIN Try again
12 SOCKET_ENOMEM Out of memory
13 SOCKET_EACCES Permission denied
14 SOCKET_EFAULT Bad address
15 SOCKET_ENOTBLK Block device required
16 SOCKET_EBUSY Device or resource busy
17 SOCKET_EEXIST File exists
18 SOCKET_EXDEV Cross-device link
19 SOCKET_ENODEV No such device
20 SOCKET_ENOTDIR Not a directory
21 SOCKET_EISDIR Is a directory
22 SOCKET_EINVAL Invalid argument
23 SOCKET_ENFILE File table overflow
24 SOCKET_EMFILE Too many open files
25 SOCKET_ENOTTY Not a typewriter
28 SOCKET_ENOSPC No space left on device
29 SOCKET_ESPIPE Illegal seek
30 SOCKET_EROFS Read-only file system
31 SOCKET_EMLINK Too many links
32 SOCKET_EPIPE Broken pipe
36 SOCKET_ENAMETOOLONG File name too long
37 SOCKET_ENOLCK No record locks available
38 SOCKET_ENOSYS Function not implemented
39 SOCKET_ENOTEMPTY Directory not empty
40 SOCKET_ELOOP Too many symbolic links encountered
42 SOCKET_ENOMSG No message of desired type
43 SOCKET_EIDRM Identifier removed
44 SOCKET_ECHRNG Channel number out of range
45 SOCKET_EL2NSYNC Level 2 not synchronized
46 SOCKET_EL3HLT Level 3 halted
47 SOCKET_EL3RST Level 3 reset
48 SOCKET_ELNRNG Link number out of range
49 SOCKET_EUNATCH Protocol driver not attached
50 SOCKET_ENOCSI No CSI structure available
51 SOCKET_EL2HLT Level 2 halted
52 SOCKET_EBADE Invalid exchange
53 SOCKET_EBADR Invalid request descriptor
54 SOCKET_EXFULL Exchange full
55 SOCKET_ENOANO No anode
56 SOCKET_EBADRQC Invalid request code
57 SOCKET_EBADSLT Invalid slot
60 SOCKET_ENOSTR Device not a stream
61 SOCKET_ENODATA No data available
62 SOCKET_ETIME Timer expired
63 SOCKET_ENOSR Out of streams resources
64 SOCKET_ENONET Machine is not on the network
66 SOCKET_EREMOTE Object is remote
67 SOCKET_ENOLINK Link has been severed
68 SOCKET_EADV Advertise error
69 SOCKET_ESRMNT Srmount error
70 SOCKET_ECOMM Communication error on send
71 SOCKET_EPROTO Protocol error
72 SOCKET_EMULTIHOP Multihop attempted
74 SOCKET_EBADMSG Not a data message
76 SOCKET_ENOTUNIQ Name not unique on network
77 SOCKET_EBADFD File descriptor in bad state
78 SOCKET_EREMCHG Remote address changed
85 SOCKET_ERESTART Interrupted system call should be restarted
86 SOCKET_ESTRPIPE Streams pipe error
87 SOCKET_EUSERS Too many users
88 SOCKET_ENOTSOCK Socket operation on non-socket
89 SOCKET_EDESTADDRREQ Destination address required
90 SOCKET_EMSGSIZE Message too long
91 SOCKET_EPROTOTYPE Protocol wrong type for socket
92 SOCKET_ENOPROTOOPT Protocol not available
93 SOCKET_EPROTONOSUPPORT Protocol not supported
94 SOCKET_ESOCKTNOSUPPORT Socket type not supported
95 SOCKET_EOPNOTSUPP Operation not supported on transport endpoint
96 SOCKET_EPFNOSUPPORT Protocol family not supported
97 SOCKET_EAFNOSUPPORT Address family not supported by protocol
98 SOCKET_EADDRINUSE Address already in use
99 SOCKET_EADDRNOTAVAIL Cannot assign requested address
100 SOCKET_ENETDOWN Network is down
101 SOCKET_ENETUNREACH Network is unreachable
102 SOCKET_ENETRESET Network dropped connection because of reset
103 SOCKET_ECONNABORTED Software caused connection abort
104 SOCKET_ECONNRESET Connection reset by peer
105 SOCKET_ENOBUFS No buffer space available
106 SOCKET_EISCONN Transport endpoint is already connected
107 SOCKET_ENOTCONN Transport endpoint is not connected
108 SOCKET_ESHUTDOWN Cannot send after transport endpoint shutdown
109 SOCKET_ETOOMANYREFS Too many references: cannot splice
110 SOCKET_ETIMEDOUT Connection timed out
111 SOCKET_ECONNREFUSED Connection refused
112 SOCKET_EHOSTDOWN Host is down
113 SOCKET_EHOSTUNREACH No route to host
114 SOCKET_EALREADY Operation already in progress
115 SOCKET_EINPROGRESS Operation now in progress
120 SOCKET_EISNAM Is a named type file
121 SOCKET_EREMOTEIO Remote I/O error
122 SOCKET_EDQUOT Quota exceeded
123 SOCKET_ENOMEDIUM No medium found
124 SOCKET_EMEDIUMTYPE Wrong medium type