日期:2014-05-18 浏览次数:21025 次
$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);
}
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);    
    }
}   
string receive=System.Text.Encoding.UTF8.GetString(str);