PHP Socket

Programing menggunakan php socket.

Apa sih php socket?
PHP socket memungkin kita untuk melaukan komunikasi lewat port-port tertentu. Sebenarnya prosessnya itu gampang di phpnya kita anggap sebagai clientnya. Kan untuk sitiap prom di client pasti ada input ma output nah di php nanti setelah bisa terkoneksi kita juga tinggal input aja pakek perintah fputs

Ini contoh-contohnya aq ambil dari php manual

//untuk koneksi ke port 80 dan bisa buat ngabil source htmlnya
$fp = fsockopen(“www.example.com”, 80, $errno, $errstr, 30);
if (!$fp) {
echo “$errstr ($errno)
\n”;
} else {
$out = “GET / HTTP/1.1\r\n”;
$out .= “Host: http://www.example.com\r\n”;
$out .= “Connection: Close\r\n\r\n”;

fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}

//untuk koneksi di port UDP
$fp = fsockopen(“udp://127.0.0.1”, 13, $errno, $errstr);
if (!$fp) {
echo “ERROR: $errno – $errstr
\n”;
} else {
fwrite($fp, “\n”);
echo fread($fp, 26);
fclose($fp);
}

//ini contoh untuk komunikasi menggunakan telnet, kemarin sudah berhasil butuh waktu 2hari untuk experimen🙂 makanya gak bikin artikel akhir2 ini hehe, harap maklum yak.

# This is the difficult part, the Telnet header <— mumet (bahasa jowo)
$header1=chr(0xFF).chr(0xFB).chr(0x1F).chr(0xFF).chr(0xFB).
chr(0x20).chr(0xFF).chr(0xFB).chr(0x18).chr(0xFF).chr(0xFB).
chr(0x27).chr(0xFF).chr(0xFD).chr(0x01).chr(0xFF).chr(0xFB).
chr(0x03).chr(0xFF).chr(0xFD).chr(0x03).chr(0xFF).chr(0xFC).
chr(0x23).chr(0xFF).chr(0xFC).chr(0x24).chr(0xFF).chr(0xFA).
chr(0x1F).chr(0x00).chr(0x50).chr(0x00).chr(0x18).chr(0xFF).
chr(0xF0).chr(0xFF).chr(0xFA).chr(0x20).chr(0x00).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0x2C).chr(0x33).
chr(0x38).chr(0x34).chr(0x30).chr(0x30).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x27).chr(0x00).chr(0xFF).chr(0xF0).
chr(0xFF).chr(0xFA).chr(0x18).chr(0x00).chr(0x58).chr(0x54).
chr(0x45).chr(0x52).chr(0x4D).chr(0xFF).chr(0xF0);
$header2=chr(0xFF).chr(0xFC).chr(0x01).chr(0xFF).chr(0xFC).
chr(0x22).chr(0xFF).chr(0xFE).chr(0x05).chr(0xFF).chr(0xFC).chr(0x21);

# connecting
$fp=fsockopen(“127.0.0.1″,23);

# sending the Telnet header
fputs($fp,$header1);
usleep(125000);
fputs($fp,$header2);
usleep(125000);

/*
# login
fputs($fp,”user\r”);
usleep(125000);
fputs($fp,”users.pass\r”);
usleep(125000);
*/

fputs($fp,”n\r”);
usleep(125000);
fputs($fp,”telonet\r”);
usleep(125000);
fputs($fp,”t3l0n3t\r”);
usleep(125000);

# some tests
//fputs($fp,”ifconfig\r”);
fputs($fp,”echo year telnet php connect works|wall\r”);

# we had to wait
usleep(125000);

# show the output
do
{
$output.=fread($fp, 1000); // read line by line, or at least small chunks
$stat=socket_get_status($fp);
}
while($stat[“unread_bytes”]);

$output = str_replace(“\n”, ”
“, $output);
fclose($fp);
echo $output;
?>

Selamat mencoba..!, Coding is my life

Satu Tanggapan

  1. thanks bgt y mas..kbetulan lagi nyari nih..
    mo skalian nanya dah..itu kan buat yg ngirim ya via telnet..klo stelah dikirim kemudian servernya ngebales..cara kita nerimanya gmna ya??bisa kasi contoh ga mas..
    thanx alot before…maybe for you “Coding is your life”…for me..”No Coding No Makan”..wkwkwkwkwkwk

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: