可以帮我看看,C#TCP服务端运行之后,为什么发信息会显示不在字典中 。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
      
        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ip = IPAddress.Parse(textBox1.Text);
            
            IPEndPoint point = new IPEndPoint(ip, int.Parse(textBox2.Text));
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
           
            {
                socket.Bind(point);
                socket.Listen(20);
                ShowMsg("开始监听");
                Thread thread = new Thread(AcceptInfo);
                thread.IsBackground = true;
                thread.Start(socket);
            }
            catch (Exception ex)
            {
                ShowMsg(ex.Message);
            }
        }
        Dictionary<string, Socket> dic = new Dictionary<string, Socket>();

        private Socket client; 
        void AcceptInfo(object a) {
            Socket socket = a as Socket;
            while (true)
            {
                try
                {
                    Socket tSocket = socket.Accept();
                    string point = tSocket.RemoteEndPoint.ToString();
                    

                    ShowMsg(point + "连接成功!");
                    cboIpPort.Items.Add(point);
                    dic.Add(point, tSocket);
                    Thread th = new Thread(ReceiveMsg);
                    th.IsBackground = true;
                    th.Start(tSocket);

                }
                catch (Exception ex)
                {
                    ShowMsg(ex.Message);
                    break;
                }
            }
        }
        void ReceiveMsg(object a) {
            Socket client = a as Socket;
            while (true)
            {
                try {
                    byte[] buffer = new byte[1024 * 1024];
                    int n = client.Receive(buffer);
                    
                    string words = Encoding.UTF8.GetString(buffer, 0, n);
                    ShowMsg(client.RemoteEndPoint.ToString() + ":" + words);
                    
                }
                catch(Exception ex){
                ShowMsg(ex.Message);
                    break;
                }
                
                }
            
            }
        void ShowMsg(string msg){
        txtLog.AppendText(msg+"\r\n");
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            try{
            ShowMsg(txtMsg.Text);
                string ip=cboIpPort.Text;
                byte [] buffer=Encoding.UTF8.GetBytes(txtMsg.Text);
                
               dic[ip].Send(buffer);
              
                
            }
            catch(Exception ex)
            {
            ShowMsg(ex.Message);
            }
        }
        
        }
    }

 

string point = tSocket.RemoteEndPoint.ToString();

打印一下point的值,是否唯一?

?什么意思?

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632