我试图做一个简单的聊天服务器。PHP试图使用PHP测试我的本地服务器,本地服务器从不接受连接
我使用的是HTML格式来测试它。现在我想通过创建一些PHP脚本并且同时运行一堆PHP脚本来确定它是否真正起作用。
我正在使用卷发来发帖。当我尝试发送邮件到我的聊天服务器时,接受不会消失。但是如果我提交表格,它就会看到它。
服务器代码
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>
您在端口1079上创建一个服务器套接字,您将其注释为端口62并打印该服务器正在端口1030上侦听。什么? – halfdan
仅供参考如果您可以更改[/ url]的代码,将POST 的数据更改为[http_build_query](http://www.php.net/http_build_query)':)' – nickb