ORDER *head=creawriterte( ) creawriterte( )函数有返回值 是ORDER*creawriter

& Create a socket (endpoint for communication)
PHP socket_create Create a socket (endpoint for communication)
socket_create
socket_create & Create a socket (endpoint for communication)
resource socket_create
( int $domain
, int $type
, int $protocol
The domain parameter specifies the protocol
family to be used by the socket.
Available address/protocol families
Description
IPv4 Internet based protocols. TCP and UDP are common protocols of
this protocol family.
IPv6 Internet based protocols. TCP and UDP are common protocols of
this protocol family.
Local communication protocol family. High efficiency and low
overhead make it a great form of IPC (Interprocess Communication).
The type parameter selects the type of communication
to be used by the socket.
Available socket types
Description
SOCK_STREAM
Provides sequenced, reliable, full-duplex, connection-based byte streams.
An out-of-band data transmission mechanism may be supported.
The TCP protocol is based on this socket type.
SOCK_DGRAM
Supports datagrams (connectionless, unreliable messages of a fixed maximum length).
The UDP protocol is based on this socket type.
SOCK_SEQPACKET
Provides a sequenced, reliable, two-way connection-based data transmission path for
datagrams of
a consumer is required to read an
entire packet with each read call.
Provides raw network protocol access. This special type of socket
can be used to manually construct any type of protocol. A common use
for this socket type is to perform ICMP requests (like ping).
Provides a reliable datagram layer that does not guarantee ordering.
This is most likely not implemented on your operating system.
The protocol parameter sets the specific
protocol within the specified domain to be used
when communicating on the returned socket. The proper value can be
retrieved by name by using . If
the desired protocol is TCP, or UDP the corresponding constants
SOL_TCP, and SOL_UDP
can also be used.
Common protocols
Description
The Internet Control Message Protocol is used primarily by gateways
and hosts to report errors in datagram communication. The &ping&
command (present in most modern operating systems) is an example
application of ICMP.
The User Datagram Protocol is a connectionless, unreliable,
protocol with fixed record lengths. Due to these aspects, UDP
requires a minimum amount of protocol overhead.
The Transmission Control Protocol is a reliable, connection based,
stream oriented, full duplex protocol. TCP guarantees that all data packets
will be received in the order in which they were sent. If any packet is somehow
lost during communication, TCP will automatically retransmit the packet until
the destination host acknowledges that packet. For reliability and performance
reasons, the TCP implementation itself decides the appropriate octet boundaries
of the underlying datagram communication layer. Therefore, TCP applications must
allow for the possibility of partial record transmission.
socket_create() returns a socket resource on success,
or FALSE on error. The actual error code can be retrieved by calling
. This error code may be passed to
to get a textual explanation of the
If an invalid domain or
type is given, socket_create()
defaults to AF_INET and
SOCK_STREAM respectively and additionally emits an
E_WARNING message.
- Accepts a connection on a socket
- Binds a name to a socket
- Initiates a connection on a socket
- Listens for a connection on a socket
- Returns the last error on the socket
- Return a string describing a socket error
PHP socket_create note #1
Here is a ping function for PHP without using exec/system/passthrough/etc... Very useful to use to just test if a host is online before attempting to connect to it. Timeout is in seconds.
&& & && function ping($host, $timeout = 1) {
&& & & & & & && $package = "x08x00x7dx4bx00x00x00x00PingHost";
&& & & & & & && $socket& = socket_create(AF_INET, SOCK_RAW, 1);
&& & & & & & && socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' =& $timeout, 'usec' =& 0));
&& & & & & & && socket_connect($socket, $host, null);
&& & & & & & && $ts = microtime(true);
&& & & & & & && socket_send($socket, $package, strLen($package), 0);
&& & & & & & && if (socket_read($socket, 255))
&& & & & & & & & & & && $result = microtime(true) - $ts;
&& & & & & & && else& & $result = false;
&& & & & & & && socket_close($socket);
&& & & & & & && return $result;
PHP socket_create note #2
Note that if you create a socket with AF_UNIX, a file will be created in the filesystem. This file is not removed when you call socket_close - you should unlink the file after you close the socket.
PHP socket_create note #3
A simple example how to send a raw udp packet
$frame = array(
&&& array(1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1),
&&& array(1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1),
&&& array(1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1),
&&& array(1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1),
&&& array(1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1),
&&& array(1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1),
&&& array(1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1),
&&& array(1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1)
send_frame($frame, 1500);
function send_frame($frame, $delay, $host="192.168.0.23", $port=2323) {
&&& $header = "x23x54x26x66x00x08x00x12x00x01x00xff";
&&& $buf = $header;
&&& for ($i=0;$i&8;$i++) {
&& & && for ($j=0;$j&18;$j++) {
&& & & & && if ($frame[$i][$j]) {
&& & & & & & && $buf.="xff";
&& & & & && } else& {
&& & & & & & && $buf.="x00";
&& & & & && }
&&& $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
&&& socket_sendto($socket, $buf, strlen($buf), 0, $host, $port);
&&& socket_close($socket);
&&& usleep($delay*1000);
PHP socket_create note #4
It took some investigation to understand the bit about raw sockets requiring root access and raw sockets being required to perform a seemingly simple "ping" on a Linux server. My source of confusion was that the regular command line "ping" utility did not require me to be root to exec it, so what was the difference? I found the answer starting with a forum posting to look up `man 7 raw` for raw sockets info which reveals:
"Only processes with an effective user ID of 0 or the CAP_NET_RAW& capability are allowed to open raw sockets."
So root the ping utility normally works because it has suid permission. There is some interesting source code however that allows you to selectively assign the CAP_NET_RAW capability without the need for suid. Use at your own risk, this is provided for informational purposes only:
PHP socket_create note #5
After weeks of trying to find a PHP network client that supports TFTP and Telnet I finally break down and wrote a client library. You can find it here:
This isn't like other telnet libraries that just exec the OS's telnet, it makes and negotiates its own options. It creates the TFTP packets to send data back and forth.
PHP socket_create note #6
On UNIX systems php needs /etc/protocols for constants like SOL_UDP and SOL_TCP.
This file was missing on my embedded platform.
PHP socket_create note #7
I've written the ping() function using socket_create() with SOCK_RAW.
(on Unix System, you need to have the root acces to execute this function)
$g_icmp_error = "No Error";
function ping($host, $timeout)
&& & && $port = 0;
&& & && $datasize = 64;
&& & && global $g_icmp_error;
&& & && $g_icmp_error = "No Error";
&& & && $ident = array(ord('J'), ord('C'));
&& & && $seq&& = array(rand(0, 255), rand(0, 255));
&& & $packet = '';
&& & $packet .= chr(8); $packet .= chr(0); $packet .= chr(0); $packet .= chr(0); $packet .= chr($ident[0]); $packet .= chr($ident[1]); $packet .= chr($seq[0]); $packet .= chr($seq[1]); for ($i = 0; $i & $datasize; $i++)
&& & & & & & && $packet .= chr(0);
&& & && $chk = icmpChecksum($packet);
&& & && $packet[2] = $chk[0]; $packet[3] = $chk[1]; $sock = socket_create(AF_INET, SOCK_RAW,& getprotobyname('icmp'));
&& & && $time_start = microtime();
&&& socket_sendto($sock, $packet, strlen($packet), 0, $host, $port);
&&& $read&& = array($sock);
&& & && $write& = NULL;
&& & && $except = NULL;
&& & && $select = socket_select($read, $write, $except, 0, $timeout * 1000);
&& & && if ($select === NULL)
&& & & & & & && $g_icmp_error = "Select Error";
&& & & & & & && socket_close($sock);
&& & & & & & && return -1;
&& & && elseif ($select === 0)
&& & & & & & && $g_icmp_error = "Timeout";
&& & & & & & && socket_close($sock);
&& & & & & & && return -1;
&&& $recv = '';
&&& $time_stop = microtime();
&&& socket_recvfrom($sock, $recv, 65535, 0, $host, $port);
&& & && $recv = unpack('C*', $recv);
&& & && if ($recv[10] !== 1) {
&& & & & & & && $g_icmp_error = "Not ICMP packet";
&& & & & & & && socket_close($sock);
&& & & & & & && return -1;
&& & && if ($recv[21] !== 0) {
&& & & & & & && $g_icmp_error = "Not ICMP response";
&& & & & & & && socket_close($sock);
&& & & & & & && return -1;
&& & && if ($ident[0] !== $recv[25] || $ident[1] !== $recv[26])
&& & & & & & && $g_icmp_error = "Bad identification number";
&& & & & & & && socket_close($sock);
&& & & & & & && return -1;
&& & && if ($seq[0] !== $recv[27] || $seq[1] !== $recv[28])
&& & & & & & && $g_icmp_error = "Bad sequence number";
&& & & & & & && socket_close($sock);
&& & & & & & && return -1;
&& & && $ms = ($time_stop - $time_start) * 1000;
&& & && if ($ms & 0)
&& & & & & & && $g_icmp_error = "Response too long";
&& & & & & & && $ms = -1;
&& & && socket_close($sock);
&& & && return $ms;
function icmpChecksum($data)
&& & && $bit = unpack('n*', $data);
&& & && $sum = array_sum($bit);
&& & && if (strlen($data) % 2) {
&& & & & & & && $temp = unpack('C*', $data[strlen($data) - 1]);
&& & & & & & && $sum += $temp[1];
&& & && $sum = ($sum && 16) + ($sum & 0xffff);
&& & && $sum += ($sum && 16);
&& & && return pack('n*', ~$sum);
function getLastIcmpError()
&& & && global $g_icmp_error;
&& & && return $g_icmp_error;
PHP socket_create note #8
to ionic and david...
I'm just taking a stab at this, but wouldn't some combination of gethostbynamel and php_uname('n') provide you with the world-facing IP address without making a system call or using expensive socket operations?& Don't get me wrong, socket operations have their place, but if you just need the server IP from a cli script, socket operations seem expensive. :)
print_r(gethostbynamel(php_uname('n')));
PHP socket_create note #9
When running these types of sockets, typically you need to be root or else they fail (not the case with stream sockets: )
This is an example for Linux/Unix type systems to use sockets without being root. (tested on Debian and CentOS)
$user = "daemon";
$script_name = "uid"; echo "
__________________________________________
echo "Trying to start a socket as user $user
$uid_name = posix_getpwnam($user);
$uid_name = $uid_name['uid'];
if(posix_seteuid($uid_name))
&& & && echo "SUCCESS: You are now $user!
&& & && if($socket = @socket_create(AF_INET, SOCK_RAW, 1))
&& & & & & & && echo "SUCCESS: You are NOT root and created a socket! This should not happen!
&& & && } else {
&& & & & & & && echo "ERROR: socket_create() failed because you're not root!
&& & && $show_process = shell_exec("ps aux | grep -v grep | grep $script_name");
&& & && echo "Current process stats::--&
$show_process";
&& & && exit("ERROR: seteuid($uid_name) failed!
__________________________________________
echo "Trying to start a socket as user 'root'
if(posix_seteuid(0))
&& & && echo "SUCCESS: You are now root!
&& & && $show_process = shell_exec("ps aux | grep -v grep | grep $script_name");
&& & && echo "Current process stats::--&
$show_process";
&& & && if($socket = @socket_create(AF_INET, SOCK_RAW, 1))
&& & & & & & && echo "SUCCESS: You created a socket as root and now should seteuid() to another user
&& & & & & & && echo "
__________________________________________
&& & & & & & && echo "Switching to user $user
&& & & & & & && if(posix_seteuid($uid_name))
&& & & & & & && {
&& & & & & & & & & & && echo "SUCCESS: You are now $user!
&& & & & & & & & & & && if(socket_bind($socket, 0, 8000))
&& & & & & & & & & & && {
&& & & & & & & & & & & & & & && echo "SUCCESS: socket_bind() worked as $user!
&& & & & & & & & & & && } else {
&& & & & & & & & & & & & & & && echo "ERROR: Must be root to user socket_bind()
&& & & & & & & & & & && }
&& & & & & & & & & & && $show_process = shell_exec("ps aux | grep -v grep | grep $script_name");
&& & & & & & & & & & && echo "Current process stats::--&
$show_process";
&& & & & & & & & & & && socket_close($socket); echo "SUCCESS: You closed the socket as user $user!
&& & & & & & && } else {
&& & & & & & & & & & && echo "ERROR: seteuid($uid_name) failed while socket was open!
&& & & & & & && }
&& & && } else {
&& & & & & & && echo "ERROR: Socket failed for some reason!
&& & && exit("ERROR: Changing to root failed!
PHP socket_create note #10
To david _at* eder # us:
Dependent on your system, you could, at least on LINUX, use exec with "/sbin/ifconfig eth0 | awk '/inet /{ print substr($2,9) }'" (also working for non-root users), that will work a little bit faster as your PHP function.
Though, we should keep in mind that users with safe_mode enabled are more or less forced to use the socket thing. :)
PHP socket_create note #11
Please be aware that RAW sockets (as used for the ping example) are restricted to root accounts on *nix systems. Since web servers hardly ever run as root, they won't work on webpages.
On Windows based servers it should work regardless.
PHP socket_create note #12
Here's a ping function that uses sockets instead of exec().& Note: I was unable to get socket_create() to work without running from CLI as root.& I've already calculated the package's checksum to simplify the code (the message is 'ping' but it doesn't actually matter).
function ping($host) {
&&& $package = "x08x00x19x2fx00x00x00x00x70x69x6ex67";
&&& $socket = socket_create(AF_INET, SOCK_RAW, 1);
&&& socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" =& 1, "usec" =& 0));
&&& socket_connect($socket, $host, null);
&&& list($start_usec, $start_sec) = explode(" ", microtime());
&&& $start_time = ((float) $start_usec + (float) $start_sec);
&&& socket_send($socket, $package, strlen($package), 0);
&&& if(@socket_read($socket, 255)) {
&& & && list($end_usec, $end_sec) = explode(" ", microtime());
&& & && $end_time = ((float) $end_usec + (float) $end_sec);
&& & && $total_time = $end_time - $start_time;
&& & && return $total_time;
&&& } else {
&& & && return false;
&&& socket_close($socket);
PHP socket_create note #13
Took me about 20 minutes to figure out the proper arguments to supply for a AF_UNIX socket. Anything else, and I would get a PHP warning about the 'type' not being supported. I hope this saves someone else time.
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
PHP socket_create note #14
Sometimes when you are running CLI, you need to know your own ip address.
& $addr = my_ip();
& echo "my ip address is $addr
& function my_ip($dest='64.0.0.0', $port=80)
&&& $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
&&& socket_connect($socket, $dest, $port);
&&& socket_getsockname($socket, $addr, $port);
&&& socket_close($socket);
&&& return $addr;
PHP socket_create note #15
Seems there aren't any examples of UDP clients out there.& This is a tftp client.& I hope this makes someone's life easier.
& function tftp_fetch($host, $filename)
&&& $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
&&& $packet = chr(0) . chr(1) . $filename . chr(0) . 'octet' . chr(0);
&&& socket_sendto($socket, $packet, strlen($packet), 0x100, $host, 69);
&&& $buffer = '';
&&& $port = '';
&&& $ret = '';
&& && socket_recvfrom($socket, $buffer, 516, 0, $host, $port);
&& && $packet = chr(0) . chr(4) . substr($buffer, 2, 2);
&& && socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);
&& && $ret .= substr($buffer, 4);
&&& while(strlen($buffer) == 516);& return $ret;
PHP socket_create note #16
Okay I talked with Richard a little (via e-mail). We agree that getprotobyname() and using the constants should be the same in functionality and speed, the use of one or the other is merely coding style. Personally, we both think the constants are prettier :).
The eight different protocols are the ones implemented in PHP- not the total number in existance (RFC 1340 has 98).
All we disagree on is using 0- Richard says that "accordning to the official unix/bsd sockets 0 is more than fine." I think that since 0 is a reserved number according to RFC 1320, and when used usually refers to IP, not one of it's sub-protocols (TCP, UDP, etc.)
PHP socket_create note #17
Actually, you don't need to use
getprotobyname("tcp") but instead can use
the constants:& SOL_TCP& and&& SOL_UDP.
Here an extract of the source from
ext/sockets which should make this clear.
if ((pe = getprotobyname("tcp"))) {
&& & && REGISTER_LONG_CONSTANT("SOL_TCP", pe-&p_proto,
&& & && CONST_CS | CONST_PERSISTENT);
Normally the third parameter can be set to 0. In the
original BSD Socket implementation the third parameter
(there are 8 different types, here only two) should be
IPPROTO_TCP or IPPROTO_UPD (or one of the 6 others ones).
These two parameters are though not warpped in PHP as
constants and therefore not available.
Please use SOL_TCP& and&& SOL_UDP. e.g.:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
Please be aware of the fact that UDP
and TCP can only be used with AF_INET which is: "Adress Family Internet". With UNIX Domain sockets TCP/UDP would make no sense!
best regards
-Richard-Moh Samar
PHPSockets - 函数pthread_create函数的详细讲解(包括向线程函数传递参数详解) - liangxanhai的专栏
- 博客频道 - CSDN.NET
28809人阅读
pthread_create是UNIX环境创建线程函数
  #include&pthread.h&
  int pthread_create(*restrict
tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);
  若成功则返回0,否则返回出错编号
  返回成功时,由tidp指向的内存单元被设置为新创建线程的线程ID。attr参数用于制定各种不同的线程属性。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。
  linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。
  由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取。对对象的存取都限定于基于由 restrict 修饰的指针表达式中。 由 restrict 修饰的指针主要用于函数形参,或指向由 malloc() 分配的内存空间。restrict 数据类型不改变程序的语义。&能通过作出
restrict 修饰的指针是存取对象的唯一方法的假设,更好地优化某些类型的例程。
  第一个参数为指向线程的指针。
  第二个参数用来设置线程属性。
  第三个参数是线程运行函数的起始地址。
  最后一个参数是运行函数的参数。
  另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库
  打印线程 IDs
  #include &pthread.h&
  #include &stdlib.h&
  #include &stdio.h&
  #include &unistd.h&
  #include &string.h&
  pthread_
  void printids(const char *s)
  pthread_
  pid = getpid();
  tid = pthread_self();
  printf(&%s pid %u tid %u (0x%x)\n&, s,
  (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
  } void *thr_fn(void *arg)
  printids(&new thread: &);
  return((void *)0);
  int main(void)
  err = pthread_create(&ntid, NULL, thr_fn, NULL);
  if (err != 0)
  printf(&can't create thread: %s\n&, strerror(err));
  printids(&main thread:&);
  sleep(1);
  exit(0);
  $ gcc main.c -lpthread
  $ ./a.out
向线程函数传递参数详解:
向线程函数传递参数分为两种:
(1)线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数。
#include &iostream&
#include &pthread.h&
void fn(void *arg)
& & int i = *(int *)
& & cout&&&i = &&&i&&
& & return ((void *)0);
int main()
& & int err1;
& & int i=10;
& &err1 = pthread_create(&thread, NULL, fn, &i);
& & pthread_join(thread, NULL);
2、线程函数有多个参数的情况:这种情况就必须申明一个结构体来包含所有的参数,然后在传入线程函数,具体如下:
首先定义一个结构体:
struct &parameter
& int size,
。。。。。
然后在main函数将这个结构体指针,作为void
*形参的实际参数传递
通过如下的方式来调用函数:
pthread_create(&ntid, NULL, fn,& (arg));
函数中需要定义一个parameter类型的结构指针来引用这个参数
void fn(void *arg)
& & int i = *(int *)
& & cout&&&i = &&&i&&
& & return ((void *)0);
void thr_fn(void *arg)
struct parameter *
pstru = ( struct parameter *)
然后在这个函数中就可以使用指针来使用相应的变量的值了。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:176737次
积分:2023
积分:2023
排名:第9080名
原创:43篇
转载:15篇
评论:21条
(1)(2)(2)(1)(1)(11)(4)(3)(25)(8)

我要回帖

更多关于 createfile返回值 的文章

 

随机推荐