PHP 尝试使用 PHP 测试我的本地服务器,本地服务器从不接受连接
我正在尝试制作一个简单的聊天服务器。
我正在使用 html 表单来测试它。现在我想通过创建一些 PHP 脚本并让它们同时运行来看看它是否真的有效。
我正在使用curl 来毁掉一个帖子。当我尝试向我的聊天服务器发送帖子消息时,接受永远不会消失。但如果我提交表单,它就会看到它。
服务器代码
server = new ServerSocket(1079); // port 62
// Loop forever
while(true)
{
///////////////////////////////////////////////////
// get a new connection
///////////////////////////////////////////////////
System.out.println("Aceepting connections on port 1030 \r");
try{
// Get New Connection
// wait for ever on accepting new connections
server.setSoTimeout(0);
connection = server.accept();
cConnection thread = new cConnection("thread3", connection);
PHP 脚本:
extract($_POST);
//set POST variables
$url = 'http://localhost:1079/enter';
$fields = array(
'username'=>urlencod("tedpottel"),
'password'=>urlencode("oreo8157"),
'comment'=>urlencode("new comment"),
);
//url-ify the data for the POST
foreach($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
print $result;
//close connection
curl_close($ch);
HTML 表单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="http://localhost:1079/enter">
<input name="username" type="text" value="tedpottel" />
<input name="password" type="text" value="oreo8157" />
<input name="comment" type="text" value="Hi There" />
<input type="submit" />
</form>
</body>
</html>
I'm trying to make a simple chat server.
I was using html forms to test it. Now I want to see if it really works by creating some PHP scrips and have a bunch of them running at the same time.
I'm using curl to immolate a post. When I try to send a post message to my chat server the accept never goes off. But if I submit my form, it sees it.
server code
server = new ServerSocket(1079); // port 62
// Loop forever
while(true)
{
///////////////////////////////////////////////////
// get a new connection
///////////////////////////////////////////////////
System.out.println("Aceepting connections on port 1030 \r");
try{
// Get New Connection
// wait for ever on accepting new connections
server.setSoTimeout(0);
connection = server.accept();
cConnection thread = new cConnection("thread3", connection);
PHP Script:
extract($_POST);
//set POST variables
$url = 'http://localhost:1079/enter';
$fields = array(
'username'=>urlencod("tedpottel"),
'password'=>urlencode("oreo8157"),
'comment'=>urlencode("new comment"),
);
//url-ify the data for the POST
foreach($fields as $key => $value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
print $result;
//close connection
curl_close($ch);
HTML Form
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="http://localhost:1079/enter">
<input name="username" type="text" value="tedpottel" />
<input name="password" type="text" value="oreo8157" />
<input name="comment" type="text" value="Hi There" />
<input type="submit" />
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据之前评论中指出的差异,我不得不相信您只是简单地复制粘贴了此内容(为什么您还要做其他事情?)。因此,在您的 PHP 脚本中,存在一个错误:
您打算编写 urlencode。但我不认为这会导致 server.accept() 不响应。
当你的意思是“模仿”时,我对你使用“牺牲”感到好笑。
Based on the discrepancies pointed out in previous comment, I have to believe that you did simply copy-paste this (and why would you do anything else?). Therefore in your PHP script, here is a mistake:
You meant to write urlencode. But I don't think this would cause the server.accept() to not respond.
I'm amused by your use of "immolate" when you meant "imitate".