.Net C# Socket通信

  IPAddress ipAddress = IPAddress.Parse(this.textBox_Ip.Text);
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, int.Parse(this.textBox_Port.Text));
            socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

 try
            {
                socketwatch.Bind(localEndPoint);
                socketwatch.Listen(1000);


                //创建一个监听线程  
                threadwatch = new Thread(watchconnecting);
                //将窗体线程设置为与后台同步,随着主线程结束而结束  
                threadwatch.IsBackground = true;
                //启动线程     
                threadwatch.Start();
                //启动线程后 textBox3文本框显示相应提示  
                textBox_code.AppendText("开始监听客户端传来的信息!" + "\r\n");


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
  private void watchconnecting()
        {
            Socket connection = null;

            while (true)  //持续不断监听客户端发来的请求     
            {
                try
                {
                  

                    connection = socketwatch.Accept();
                 

                    //获取客户端的IP和端口号  
                    IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address;
                    int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port;



                    RemoteEndPoint = connection.RemoteEndPoint.ToString(); //客户端网络结点号  
                    textBox_code.AppendText("成功与" + RemoteEndPoint + "客户端建立连接!\t\n");     //显示与客户端连接情况  


                      
                    IPEndPoint netpoint = connection.RemoteEndPoint as IPEndPoint;


                    if (!myThread.ContainsKey(connection))
                    {
                        //创建一个通信线程      
                        Worker MyWorker = new Worker();
                        MyWorker.IsRun = true;
                        MyWorker.Cus_Minute = textBox_Save_Minute;
                        MyWorker.MyLog = textBox_code;
                        Thread thread = new Thread(MyWorker.StartWork);
                        myThread.Add(connection, MyWorker);

                        thread.IsBackground = true;//设置为后台线程,随着主线程退出而退出     
                                                   //启动线程     
                        thread.Start(connection);
                    }
                }
                catch (Exception ex)
                {

                    this.textBox_code.AppendText(ex.Message + " watchconnecting"); //提示套接字监听异常  
                                                          
                    connection.Close();//关闭之前accept出来的和客户端进行通信的套接字 
                    connection.Dispose();

                }
            }
            
        }
  public void StartWork(object socketclientpar)
        {
            ///     
            /// 接收客户端发来的信息      
            ///     
            ///客户端套接字对象    

            Socket socketServer = socketclientpar as Socket;

           

            while (isrun)
            {
                try
                {
                    //
                    //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度  
                    byte[] arrServerRecMsg = new byte[1024];
                    int length = socketServer.Receive(arrServerRecMsg);

                    //将机器接受到的字节数组转换为人可以读懂的字符串 
                    //string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
                    string SixMessage = BitConverter.ToString(arrServerRecMsg, 0, length).Replace("-", " ");

                    string[] SisMessArr = SixMessage.Split(' ');

                    int mingling_length = SisMessArr.Length;
                }
            }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值