2011-11-29 167 views
0

我试图做一个简单的聊天服务器。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> 
+1

您在端口1079上创建一个服务器套接字,您将其注释为端口62并打印该服务器正在端口1030上侦听。什么? – halfdan

+0

仅供参考如果您可以更改[/ url]的代码,将POST 的数据更改为[http_build_query](http://www.php.net/http_build_query)':)' – nickb

回答

0

基础上的差异在之前的评论中指出,我不得不相信,你没有简单地复制粘贴这个(为什么你会做别的?)。因此,在你的PHP脚本,这里是一个错误:

'username'=>urlencod("tedpottel") 

你的意思是写进行urlencode。但我不认为这会导致server.accept()不响应。

当你的意思是“模仿”时,你对“immluent”的使用感到很开心。

+0

的简单调用谢谢为了花时间回答我的问题,是的代码被复制,但我确实更改了网址以指向我的Web服务器。它运行良好。是的,我不能拼写。 –

相关问题