PHP代码如下:$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#代码如下: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#端也能弹出“发送通知”来?
$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#代码如下: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);