当前位置:编程学习 > C#/ASP.NET >>

.net实现TCP通信监听

哪个大哥能帮我写个监听程序...对方用IP加端口号进行联接.我只需要监听到有IP在连接。然后就提示哪个IP对我的主机进行了联接就行了..有哪个好心的大哥帮帮忙.谢谢      --------------------编程问答-------------------- UPUPUPPUPUPUPPPUPUU --------------------编程问答-------------------- System.Diagnostics.Process里
使用 netstat -an看看把 --------------------编程问答--------------------  public void StartListen(System.Windows.Forms.Control owner, Action<IPEndPoint, string> ProcessTcpCommand)
        {
            _owner = owner;

            GetTcpMessage = ProcessTcpCommand;
            tcpListenerThread = new Thread(TcpListenHandler);
            tcpListenerThread.IsBackground = true;
            tcpListenerThread.Start();
        } 

private void TcpListenHandler()
        {
            tcpListener = new TcpListener(IPAddress.Any, global.TcpPort);
            IPEndPoint iep;
            tcpListener.Start();

            while (isListening)
            {
                //异步方式接受
                //tcpListener.BeginAcceptTcpClient();
                
                System.Threading.Thread.Sleep(200);
                if (tcpListener.Pending())
                {
                    TcpClient tcpRecivceClient = tcpListener.AcceptTcpClient();

                    NetworkStream netStream = tcpRecivceClient.GetStream();
                    byte[] buffer = new byte[1024];
                    if (!netStream.DataAvailable)
                        continue;
                    List<byte> bufferTotal = new List<byte>();
                    while (netStream.DataAvailable)
                    {
                        netStream.Read(buffer, 0, 1024);
                        bufferTotal.AddRange(buffer);
                    }
                    iep = (IPEndPoint)tcpRecivceClient.Client.RemoteEndPoint;

                    tcpRecivceClient.Close();
                    netStream.Close();
                    string receive = System.Text.Encoding.UTF8.GetString(bufferTotal.ToArray());
                    _owner.Invoke(GetTcpMessage, new object[] { iep, receive });
                }
            }
            tcpListener.Stop();
        } --------------------编程问答-------------------- try
引用 1 楼 hqs19821108 的回复:
UPUPUPPUPUPUPPPUPUU
--------------------编程问答--------------------
引用 2 楼 zgke 的回复:
System.Diagnostics.Process里
使用 netstat -an看看把

try 点错了。。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,