繁体   English   中英

php中的tcp客户端连接

[英]tcp client connection in php

我有一个连接到服务器并以xml或json返回结果的Web服务。 它是用asp.net编写的。 我想在php中使用相同的代码。

using (TcpClient tcpClient = new TcpClient()) 
    { 
        tcpClient.Connect("1.1.1.1", port: 42011);
        string search = "search^10|5|2|1^gagl^55592a782ce899d2" + 
System.Environment.NewLine; 
        using (NetworkStream ns = tcpClient.GetStream()) 
        { 
            byte[] bytes = ASCIIEncoding.ASCII.GetBytes(search); 
            ns.Write(bytes, 0, bytes.Length); 
            string result = ReadBuffer(ns); 

          // [.. consume result XML string ..] 

        } 
        tcpClient.Close(); 
    }
        }
protected string ReadBuffer(NetworkStream ns) 
{ 
    byte[] lenghtBuffer = new byte[4]; 
    int bytesRead = 0; 
    while (bytesRead < 4) 
        bytesRead += ns.Read(lenghtBuffer, bytesRead, 4 - bytesRead); 
    if (BitConverter.IsLittleEndian) 
        Array.Reverse(lenghtBuffer); 
    int responseLength = BitConverter.ToInt32(lenghtBuffer, 0); 
    byte[] buffer = new byte[responseLength]; 
    bytesRead = 0; 
    while (bytesRead < responseLength) 
        bytesRead += ns.Read(buffer, bytesRead, responseLength - bytesRead); 
    string result = ASCIIEncoding.ASCII.GetString(buffer, 0, bytesRead); 
    return result; 
} 

上面的代码返回.net中的值。 如何使用相同的值在php中获得结果。

您可以使用fsockopen( http://php.net/manual/fr/function.fsockopen.php )打开TCP连接并检索结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM