美文网首页
C#用多线程避开TCP多次连接的问题

C#用多线程避开TCP多次连接的问题

作者: 我的小猫不见了 | 来源:发表于2020-04-01 17:22 被阅读0次

    1.在做TCP客户端经常遇到开始连可以,后面就会卡起来 , 我的思路是新开一个线程,用来监听连接请求,另一个线程作为主线程去应付用户的操作.

    using System;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Security.Cryptography;
    using System.Threading;
    
    namespace ConsoleApp3
    {
        public class Message_01
        {
            public static int user_id;
            public static string user_name="";
            public static string nneed_send = "";
            public static string receive_recent="";
            public static Boolean status = true;
        }
        class Program
        {
           
            public static void connect()
            {
                IPAddress myIP = IPAddress.Parse("192.168.1.7");
                //构造一个TcpClient类对象,TCP客户端
                TcpClient client = new TcpClient();
                //与TCP服务器连接
                client.Connect(myIP, 7777);
                //创建网络流,获取数据流
                NetworkStream stream = client.GetStream();
                //读数据流对象
                StreamReader sr = new StreamReader(stream);
                //写数据流对象
                StreamWriter sw = new StreamWriter(stream);
                while (true)
                {
                    if (Message_01.nneed_send.Equals(""))
                    {
                        System.Threading.Thread.Sleep(15);
                    }
                    else
                    {
                        try
                        {
                            string input0 = Message_01.nneed_send;
                            string[] sArray = Regex.Split(input0, " ", RegexOptions.IgnoreCase);
                            if (sArray[0].Equals("login"))
                            {
                                input0 = sArray[0] + " " + Md5_Tools("Apache" + sArray[1]);
                            }
                            Console.WriteLine(input0);
                            string input = String2Unicode(input0);
                            char[] chars = input.ToCharArray();
                            byte[] bytes = new byte[chars.Length];
                            for (int i = 0; i < chars.Length; i++)
                            {
                                int c = (int)chars[i];
                                bytes[i] = (byte)c;
                            }
                            Encoding Encoding_GB2312 = Encoding.GetEncoding("GB2312");
                            string str_converted = Encoding_GB2312.GetString(bytes);
                            sw.WriteLine(str_converted);
                            sw.Flush();             //刷新流
                            Message_01.receive_recent=sr.ReadLine();
                            Message_01.nneed_send = "";
                            
    
                        }
                        catch (IOException e)
                        {
                            /*无论如何都要将send清零*/
                            Message_01.nneed_send = "";
                            Message_01.status = false;/*判断执行是否出现错误*/
                        }
                    }
                }            
            }
            public static string String2Unicode(string source)
            {
                byte[] bytes = Encoding.Unicode.GetBytes(source);
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i += 2)
                {
                    stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
                }
                return stringBuilder.ToString();
            }
    
            public static string Md5_Tools(string source)
            {
    
                //创建MD5实例
                MD5 md5 = MD5.Create();
                //计算哈希值
                byte[] result = md5.ComputeHash(Encoding.Default.GetBytes(source));
                string re = "";
                //历遍result的值
                for (int i = 0; i < result.Length; i++)
                {
                    re += result[i].ToString("X2");
                }
                return re;
    
            }
        }
        public class ChatTestt
        {
            static void Main(string[] args)
            {
    
                Thread thread = new Thread(new ThreadStart(Program.connect));
                thread.Start();
                while (true)
                {
                    string a = Console.ReadLine();
                    Message_01.nneed_send = a;
                    System.Threading.Thread.Sleep(120);
                    var b = Message_01.receive_recent;
                    
                    Console.WriteLine(b);
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:C#用多线程避开TCP多次连接的问题

          本文链接:https://www.haomeiwen.com/subject/tvvauhtx.html