2012-01-23 15 views
6

Veriyi PHP soketine gönderip almaya çalışıyorum.Veri göndermek ve almak için PHP soketini kullanın

Evrything tamam ama veri göndermeye çalıştığımda PHP hiçbir şey göndermiyor (Wireshark bana gönderilen veri uzunluğunun 0 olduğunu söyledi).

<?php 
$address = 'example.com'; 
$port = 1234; 

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
$sockconnect = socket_connect($sock, $address, $port); 

$c = 0; 
do { 
    $c++; 
    $msg = '<test>Xml data</test>'; 

    socket_write($sock, $msg, strlen($msg)); 

    echo socket_read($sock, 1024 * 100, PHP_NORMAL_READ); 

} while ($c < 3); 

socket_close($sock); 

Herkes bana yardımcı olabilir:

Bu kodu kullanıyorum? Sorumu okumak için teşekkürler.

cevap

7

Hataları bir kez bile kontrol etmiyorsanız her şeyin yolunda olduğunu nasıl anlayabilirsiniz? Sen Gethostbyname işlevini kullanmıyorsanız

<?php 
error_reporting(E_ALL); 

/* Get the port for the WWW service. */ 
$service_port = getservbyname('www', 'tcp'); 

/* Get the IP address for the target host. */ 
$address = gethostbyname('www.example.com'); 

/* Create a TCP/IP socket. */ 
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
if ($socket === false) { 
    echo "socket_create() failed: reason: " . 
     socket_strerror(socket_last_error()) . "\n"; 
} 

echo "Attempting to connect to '$address' on port '$service_port'..."; 
$result = socket_connect($socket, $address, $service_port); 
if ($result === false) { 
    echo "socket_connect() failed.\nReason: ($result) " . 
      socket_strerror(socket_last_error($socket)) . "\n"; 
} 

$in = "HEAD/HTTP/1.1\r\n"; 
$in .= "Host: www.example.com\r\n"; 
$in .= "Connection: Close\r\n\r\n"; 
$out = ''; 

echo "Sending HTTP HEAD request..."; 
socket_write($socket, $in, strlen($in)); 
echo "OK.\n"; 

echo "Reading response:\n\n"; 
while ($out = socket_read($socket, 2048)) { 
    echo $out; 
} 

socket_close($socket); 
?> 
+0

bağlanmaya çalıştığında, böylece, ama uzak ana yapar:

example from the manual aşağıdaki düşünün Bana bir daha cevap vermeyin. SocketTest v3.0 aracılığıyla veri gönderip doğru şekilde çalışıyor – ucha

+0

"HEAD" isteği örneğim sisteminizde çalışır mı? – sanmai

+0

Evet, diğer ana bilgisayarlarda çalışıyor, ancak yalnızca xml verilerini göndermem gerekiyor çünkü ana bilgisayarım, 1980 dışındaki herhangi bir bağlantı noktasından http başı isteklerini yanıtlamıyor. XML gönderirken bana yanıt vermiyor (örneğinizi kullanarak bile) veri. – ucha

1

varolmayan bir ip adresine hiçbir herhangi bir hata yoktur

<?php 
$address= gethostbyname('yourdata.com'); 
İlgili konular