2011-11-29 169 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服務器。它運行良好。是的,我不能拼寫。 –

相關問題