TCP通信问题,我的TCP服务器貌似无法开启监听

来源:互联网 发布:医疗软件销售怎么样 编辑:程序博客网 时间:2024/06/01 09:56

代码贴出来,求帮助


using ECG_System.Dao;

using ECG_System.Mode;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Script.Serialization;


namespace ECG_System.DALL
{
    public class TcpClienter
    {
        private static TcpClient client = null;
        private static Thread[] td;//线程声明
        private static string BloodO_ip = ConfigurationManager.AppSettings["BloodO_ip"];
        private static string Ecg_ip = ConfigurationManager.AppSettings["Ecg_ip"];
        private static string bloodP_ip = ConfigurationManager.AppSettings["bloodP_ip"];


        private static string BloodO_port = ConfigurationManager.AppSettings["BloodO_port"];
        private static string Ecg_port = ConfigurationManager.AppSettings["Ecg_port"];
        private static string bloodP_port = ConfigurationManager.AppSettings["bloodP_port"];


        public static string pid { get; set; }
        public static bool isrun { get; set; }
        public static bool isruning { get; set; }






        public static void run()
        {
            List<IpAddr> list = new List<IpAddr>();   //list 存IP
            var BloodOip = new IpAddr(BloodO_ip, BloodO_port);
            var Ecgip = new IpAddr(Ecg_ip, Ecg_port);
            var bloodPip = new IpAddr(bloodP_ip, bloodP_port);
            list.Add(BloodOip);
            list.Add(Ecgip);
            list.Add(bloodPip);


            if (isruning == false)
            {
                td = new Thread[3];
                td[0] = new Thread(StartListen);
                td[1] = new Thread(StartListen);
                td[2] = new Thread(StartListen);
                for (int i = 0; i < td.Length; i++)
                {
                    td[i].Start(list[i]);
                }


            }


        }
        public static void stop()
        {
            if (td.Length > 0)
            {
                for (int i = 0; i < td.Length; i++)
                {
                    td[i].Abort();
                }
            }
            if (client.Connected)
            {
                client.Close();
                isruning = false;
            }


        }
        /// <summary>
        /// 数据监听
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="isrun"></param>
        public static void StartListen(object adder)
        {
            var message = "";
            try
            {
              
                IpAddr ia = (IpAddr)adder;
                using (client = new TcpClient(ia.ip, ia.port))
                {


                    while (isrun)
                    {
                        isruning = true;
                        //接受连接请求
                        NetworkStream nstream = client.GetStream();        //获取数据流
                        byte[] mbyte = new byte[1024];                      //建立缓存
                        int i = nstream.Read(mbyte, 0, mbyte.Length);       //将数据流写入缓存
                        message = Encoding.Default.GetString(mbyte, 0, i);//将数据流转换为字符串
                        if (!string.IsNullOrEmpty(message))
                        {


                            var arr = message.Split(',');
                            pid = GetCard(message);
                            WebCache.Add("PID", pid);
                            if (arr.Length > 6)
                            {
                                Edata d = new Edata();
                                if (arr[6] == "0x0A")
                                {
                                    d.BloodO = ConverToInt.toInt(arr[7]);
                                }
                                else if (arr[6] == "0x0B")
                                {
                                    d.Ecg = ConverToInt.toInt(arr[7]);
                                }
                                else
                                {
                                    d.bloodP = ConverToInt.toInt(arr[7]);
                                }
                                d.Pid = pid;
                                d.CreateDate = DateTime.Now;   //本地时间
                                new EdataDao().Add(d);
                            }
                           


                        }
                        Thread.Sleep(1000);
                    }
                }
            }
            catch (Exception)
            {
                stop();


            }






        }
        private static string GetCard(string pid)
        {
            string result = "";
            var arr = pid.Split(','); //把带逗号的字符串转换成字符串数组
            if (arr.Length >= 6)
            {
                for (int i = 0; i < 6; i++)
                {
                    result += arr[i] + ",";
                }
            }
            return result.TrimEnd(',');//删除最后一个逗号




        }




    }
    public class IpAddr
    {
        public IpAddr(string ip, string port)
        {
                        this.ip = ip;
                        this.port = int.Parse(port);
        }
        public string ip { get; set; }
        public int port { get; set; }


    }

}



0 0
原创粉丝点击