Sockets, faild to connect

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sticksys
Forum Commoner
Posts: 33
Joined: Thu Aug 18, 2005 3:23 am
Location: Isreal
Contact:

Sockets, faild to connect

Post by sticksys »

Code: Select all

<?php
    $socket = @socket_create_listen("12345");

    if (!$socket) {
        print "Failed to create socket!\n";
        exit;
    }

    while (true) {
        $client = socket_accept($socket);
        $welcome = "\nWelcome to the Amazing ROT13 Machine.\nType '!close' to close this connection, or type '!halt' to halt the server.\n";
        socket_write($client, $welcome);

        while (true) {
            $input = trim(socket_read ($client, 256));
            if ($input == '!close') {
                break;
            }

            if ($input == '!halt') {
                socket_close ($client);
                break 2;
            }

            $output = str_rot13($input) . "\n";
            socket_write($client, $output);
            print "Them: $input, Us: $output\n";
        }

        socket_close ($client);
    }

    socket_close ($socket);
?>
it always says "Failed to create socket!".
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

have a look at the socket_create_listen function. I think it is supposed to accept int parameters not a string
sticksys
Forum Commoner
Posts: 33
Joined: Thu Aug 18, 2005 3:23 am
Location: Isreal
Contact:

Post by sticksys »

same error...
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

try $socket = @socket_create_listen(0);
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Meaby there is already something running/bind at port 12345?
Post Reply