TCP Connection


TCP Client (Active Connection)

Active connection means sending a TCP connection request packet to a TCP server and this host is called TCP client. To perform TCP client, pid_bind and pid_connect function are required.

pid_bind($pid, "", 0);
pid_connect($pid, $addr, $port);

Argument $addr is an IP address of a TCP server and $port is a port number.

example of TCP client

<?php
$pid = pid_open("/mmap/tcp0");   // open TCP
$addr = "10.1.0.2";              // IP address of TCP server
$port = 1470;                    // TCP port
pid_bind($pid, "", 0);           // binding
pid_connect($pid, $addr, $port); // active TCP connection
sleep(25);
pid_close($pid);
?>

TCP Server (Passive Connection)

Passive connection means listening a TCP connection request packet from a TCP client and this host is called TCP server. To perform TCP server, pid_bind and pid_listen function are required.

pid_bind($pid, "", $port);
pid_listen($pid[, $backlog]);

Argument $port is a TCP port number.

example of TCP Server

<?php
$pid = pid_open("/mmap/tcp0");  // open TCP
$port = 1470;                   // TCP port number
pid_bind($pid, "", $port);      // bind with the port number
pid_listen($pid);               // passive TCP connection
sleep(25);
pid_close($pid);
?>