2009-09-08 214 views
2

是否有可能在PHP中複製此C#程序?PHP網絡套接字

 var chatsrv = new IPEndPoint(IPAddress.Parse(_charUrl), _chatPort); 
     _chatsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
     _chatsocket.Connect(chatsrv); 
     var ns = new NetworkStream(_chatsocket); 
     _nw = new BinaryWriter(new BufferedStream(ns)); 
     var nr = new BinaryReader(ns); 

     _nw.Write((byte)(0xff)); 
     _nw.Write(acc.AccountId); 
     _nw.WriteUtf8String(acc.AuthCookie); 
     _nw.Write((UInt32)2); 
     _nw.Flush(); 

     var response = nr.ReadBytes(nr.ReadInt32()); 

回答

1

也許這:


$chatsocket = fsockopen('IP address', $chatPort); 
if ($chatsocket == FALSE) die('connection failed'); 

fwrite($chatsocket, "\xff"); 
fwrite($chatsocket, $accountId); 
fwrite($chatsocket, utf8_encode($authCookie)); 
// memdump of 2 as little-endian 32-bits unsigned int (intel representation) 
fwrite($chatsocket, "\x02\x00\x00\x00"); 

$bytes = fread($chatsocket, 4); 

// convert little-endiang 32-bits unsigned int into PHP number 
$bytes = ord($bytes[0]) | (ord($bytes[1]) << 8) | (ord($bytes[2] << 16) | (ord($bytes[3] << 24); 


$response = ''; 
// wait for the entire block 
while (!feof($chatsocket) && (strlen($response) < $bytes)) 
$response .= fread($chatsocket, $bytes - strlen($response)); 

+0

這是如果有人想從PHP溝通,它使用前綴的郵件指定其消息的大小的應用程序的解決方案?對不起這個基本的問題,但我不是一個專業的開發人員... – Mike 2011-08-06 01:21:04