Scrolling through sockets and finding an available one...
Posted: Tue May 10, 2005 11:30 pm
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?
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);
}