尝试使用PHP测试我的本地服务器,本地服务器从不接受连接


PHP Trying to test my local server using PHP, the local server never accepts the connection

我想做一个简单的聊天服务器。

我使用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>

基于前面评论中指出的差异,我不得不相信你只是简单地复制粘贴了这个(为什么你要做其他事情呢?)因此,在您的PHP脚本中,这里有一个错误:

'username'=>urlencod("tedpottel")

你想写urlencode。但是我不认为这会导致server.accept()不响应。

你想说"模仿",却用了"牺牲",我觉得很好笑。