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

发布于 2024-12-18 21:43:50 字数 2307 浏览 1 评论 0原文

我正在尝试制作一个简单的聊天服务器。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三人与歌 2024-12-25 21:43:50

根据之前评论中指出的差异,我不得不相信您只是简单地复制粘贴了此内容(为什么您还要做其他事情?)。因此,在您的 PHP 脚本中,存在一个错误:

'username'=>urlencod("tedpottel")

您打算编写 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:

'username'=>urlencod("tedpottel")

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文