2013-09-16 60 views

回答

0

以下函數將使用端口號在PHP中創建一個套接字。根據你的欺騙行爲,你需要修改'信息'。在下面的例子中是明文形式,但是應用程序經常以二進制信息的形式傳輸這些信息,這些信息需要進行預留設計以欺騙它。

function sendUDP($host, $msg, $timeout = 1) { 
/* ICMP ping packet with a pre-calculated checksum */ 
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); 

socket_connect($socket, $host, 8000); //Set up to send to port 8000, change to any port 

socket_send($socket, $msg, strLen($msg), 0); 

$buf = ""; 
$from = ""; 
$port = 0; 

@socket_recvfrom($socket , $buf , 24 , 0 , $from , $port); 
socket_close($socket); 

return $buf; 
} 
0

剛讀那些代碼: server.php

<?php 

//error_reporting(E_ALL); 
set_time_limit(0); 
ob_implicit_flush(); 
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
if ($socket === false) { 
    echo "socket_create() failed:reason:" . socket_strerror(socket_last_error()) . "\n"; 
} 
$ok = socket_bind($socket, '202.85.218.133', 11109); 
if ($ok === false) { 
    echo "socket_bind() failed:reason:" . socket_strerror(socket_last_error($socket)); 
} 
while (true) { 
    $from = ""; 
    $port = 0; 
    socket_recvfrom($socket, $buf,1024, 0, $from, $port); 
    echo $buf; 
    usleep(1000); 
} 
?> 

client.php

<?php 
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
$msg = 'hello'; 
$len = strlen($msg); 
socket_sendto($sock, $msg, $len, 0, '202.85.218.133', 11109); 
socket_close($sock); 
?>