$socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); //设置超时时间为1秒 socket_set_option ($socket, SOL_SOCKET, SO_RCVTIMEO, array ("sec" => 1, "usec" => 0 ) ); $connection = socket_connect ($socket, '127.0.0.1', 10008); if ($connection < 0) { echo "can't connect"; return; } $msg = iconv ("utf-8", "gbk", "测试测试");; socket_write ($socket, $msg, strlen ($msg) ); $result = socket_read ($socket, 2048, PHP_NORMAL_READ); socket_close ($socket);