日期:2014-05-18  浏览次数:20853 次

PHP通过套接字向C#发送中文字符,C#接收到的却是数字字符
PHP代码如下:
PHP code

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);   
$connection = socket_connect($socket, $IP, 2012); 
if($connection==true)
{    
    $notice="发送通知";
    $length = strlen($notice);
    while(true)
    {
    $sent=socket_write($socket,$notice,$length);
    if($sent==false) break;
    if($sent<$length) 
        {
        $st = substr($notice, $sent);
        $length -= $sent;
        }  
    else
    {                                
           $write='failed'; 
       break;
     }
     }
    echo 'ok';
    socket_shutdown($socket,2);
    socket_close($socket);
}


C#代码如下:
C# code

public  void ReadCallback(IAsyncResult ar)
{
    StateObject state = (StateObject)ar.AsyncState;
    Socket clientSocket = state.workSocket;//从异步状态对象中获得State对象和客户套接字
    int bytesRead = clientSocket.EndReceive(ar);//从客户套接字读取数据
    if (bytesRead > 0)
    {
        byte[] str = state.buffer;
        receive = Convert.ToString(str[0]);
        MessageBox.Show(receive);    
    }
}   


这样弹出的总是数字。是不是转码的问题?我该如何在C#端也能弹出“发送通知”来?

------解决方案--------------------
都没经过编码..
string receive=System.Text.Encoding.UTF8.GetString(str);
MessageBox.Show(receive);
------解决方案--------------------
探讨

都没经过编码..
string receive=System.Text.Encoding.UTF8.GetString(str);
MessageBox.Show(receive);

------解决方案--------------------
C# code

string receive=System.Text.Encoding.UTF8.GetString(str);