日期:2014-05-17 浏览次数:20497 次
<?php
// 设置一些基本的变量
global $lat,$lnt;
$host = "192.168.1.110";
$port = 8080;
// 设置超时时间
set_time_limit(0);
// 创建一个Socket
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
//绑定Socket到端口
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$ret = socket_listen($socket, 5);
if($ret)
{
while (true)
{
$spawn = socket_accept($socket);
if(!$spawn) break;
$output = "congratulations,you have connected server:" . $host . "!";
socket_write($spawn, $output, strlen ($output)) or die("error\n");
$input = socket_read($socket, 100000);
socket_write($spawn, $input, strlen ($input)) or die("error\n");
socket_close($spawn);
}
}
socket_close();
?>
<?php
// 设置一些基本的变量
$host = "192.168.1.110";
$port = 8080;
// 设置超时时间
set_time_limit(0);
// 创建一个Socket
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
if(!$socket) echo "can't creat socket";
//绑定Socket到端口
$result = socket_connect($socket,$host,$port);
if($result)
{
$output="abc";
socket_write($socket, $output, strlen ($output));
$input = socket_read($socket, 1024);
}
echo $input;
?>
// Client
// 设置错误处理
error_reporting (E_ALL);
// 设置处理时间
set_time_limit (0);
$ip = ""; // IP 地址
$port = 1000; // 端口号
$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // 创建一个SOCKET
if ($socket)
echo "socket_create() 成功!<br>\n";
else
echo "socket_create() 失败:".socket_strerror ($socket)."