Scrolling through sockets and finding an available one...

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
deffe
Forum Newbie
Posts: 2
Joined: Tue May 10, 2005 11:05 pm

Scrolling through sockets and finding an available one...

Post by deffe »

The following code is what I am using to make a connection to a mainframe to extract data from an open socket. Everything works fine but I have a concern with the way I am handling the $int_socket_number; socket to connect to.

The mainframe allows X sockets open for a particular server and the only way I found of scrolling through each open socket it to use MySQL and a table to store which socket is available.

Does anyone know a better way to handle this without a db? sleep?

Code: Select all

// create a new socket
$socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket == false) {
    // cannot create socket
    $int_socket_error = 1;
}

// connect to the socket
$socket_connect = '';
if ($int_socket_error == 0) {
    $socket_connect = @socket_connect($socket, $str_address, $int_socket_number);
    if ($socket_connect == false) {
                                // cannot connect to socket
                                $int_socket_error = 1;
                        }
                }

$socket_write = '';
                if ($int_socket_error == 0) {
                        $socket_write = @socket_write($socket, $str_symconnect_message);
                        if ($socket_write == false) {
                                // cannot write to socket
                                $int_socket_error = 1;
                        }
                }$read = '';
                $pos = '';
                if ($int_socket_error == 0) {
                        do {
                                $read .= socket_read($socket, 8024);
                                $pos = strpos($read, chr(10));
                        } while ($pos === false);
}
Post Reply