Example: Working with IPV4/6 stream sockets
The follwing socket functions can be used. Complexer examples with TLS you will find under ssl-client and webserver.
Example: Connect to a hostname/port and send/receive data.
<?v1
hostname = "myhostname";
socket = fsockopen (hostname, 1000);
if (socket) {
print ("Connected to IP: ".fsockip (socket)." Port: ".fsockport (socket));
// Send a string
fwrite (socket, "TEST");
// Try to read 1000 bytes
// To read line by line use: while (freadln (socket, data)) { ... }
data = "";
bytesRead = fread (socket, data, 1000);
if (bytesRead!==false) {
print (bytesRead, " bytes received.");
print (data);
}
else {
print ("Connection closed or timeout.");
}
// Close socket
fclose (socket);
}
?>