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

C#如何接收通过socket发送到本机某个端口的二进制信息?
比如说,用PHP发送一个字节00100000到本机的2012端口,如何在本机通过C#监听2012端口并接收、识别这个字节?

------解决方案--------------------
监听2010,然后reciv啊

socket使用方法,google “c# socket”
------解决方案--------------------
C# code

IPAddress ip = IPAddress.Parse("127.0.0.1");
            IPEndPoint iep = new IPEndPoint(ip, 2012);
            Socket mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            mySocket.Bind(iep);
            mySocket.Listen(10);
            Socket myClient = mySocket.Accept();
            myClient.Send(System.Text.Encoding.Unicode.GetBytes("连接成功"));
            byte[] buf = new byte[1024];
            int k = myClient.Receive(buf);
            System.Console.WriteLine(System.Text.Encoding.Unicode.GetString(buf, 0, k));
            myClient.Send(System.Text.Encoding.Unicode.GetBytes("客户你好"));
            myClient.Shutdown(SocketShutdown.Both);
            myClient.Close();
            mySocket.Close();