<?php
set_time_limit(0);
$address
=
"127.0.0.1"
;
$port
= 2048;
$sock
= socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
or
die
(
"socket_create() 失敗的原因是:"
. socket_strerror(socket_last_error()) .
"/n"
);
socket_set_block(
$sock
)
or
die
(
"socket_set_block() 失敗的原因是:"
. socket_strerror(socket_last_error()) .
"/n"
);
$result
= socket_bind(
$sock
,
$address
,
$port
)
or
die
(
"socket_bind() 失敗的原因是:"
. socket_strerror(socket_last_error()) .
"/n"
);
$result
= socket_listen(
$sock
, 4)
or
die
(
"socket_listen() 失敗的原因是:"
. socket_strerror(socket_last_error()) .
"/n"
);
echo
"OK\nBinding the socket on $address:$port ... "
;
echo
"OK\nNow ready to accept connections.\nListening on the socket ... \n"
;
do
{
$msgsock
= socket_accept(
$sock
)
or
die
(
"socket_accept() failed: reason: "
. socket_strerror(socket_last_error()) .
"/n"
);
echo
"Read client data \n"
;
$buf
= socket_read(
$msgsock
, 8192);
echo
"Received msg: $buf \n"
;
$msg
=
"welcome \n"
;
socket_write(
$msgsock
,
$msg
,
strlen
(
$msg
))
or
die
(
"socket_write() failed: reason: "
. socket_strerror(socket_last_error()) .
"/n"
);
socket_close(
$msgsock
);
}
while
(true);
socket_close(
$sock
);