在WinForms c#中使用套接字连接在服务器端显示多个客户端名称

本文关键字:显示 客户端 服务器端 连接 WinForms 套接字 | 更新日期: 2023-09-27 18:02:31

Windows-Form Application c# 中,我已经使用套接字连接完成了多个客户端到单个服务器的连接,并且我可以从多个客户端向单个服务器发送数字。但问题是,在服务器端,我无法区分哪个客户端发送哪个数字,所以我需要在服务器端应用程序中显示不同的Client_1, Client_2, Client_3等。请让我知道怎样做会更好?

SERVER(在OnDataReceived()方法中如何更改?)

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Socket_Server
{
    public partial class Form1 : Form
    {
        const int MAX_CLIENTS = 30;
        public AsyncCallback pfnWorkerCallBack;
        private Socket m_mainSocket;
        private Socket[] m_workerSocket = new Socket[30];
        private int m_clientCount = 0;

        public StreamWriter STW;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            string PortNumber = "1755";
            try
            {
                string portStr = PortNumber;
                int port = System.Convert.ToInt32(portStr);
                m_mainSocket = new Socket(AddressFamily.InterNetwork,
                                           SocketType.Stream,
                                           ProtocolType.Tcp);
                IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
                m_mainSocket.Bind(ipLocal);
                m_mainSocket.Listen(4);
                m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
                this.BackgroundWorker = new Thread(new ThreadStart(Startinfiniteloop));
                this.BackgroundWorker.Start();

            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
        public void Startinfiniteloop()
        {
            for (int initial_value = 0; ; initial_value++)
            {

                try
                {

                    Dictionary<string, int> d2 = new Dictionary<string, int>();
                    d2["c2"] = initial_value;


                    this.BeginInvoke((MethodInvoker)(() => Storing_Number.Items.Clear()));
                    this.BeginInvoke((MethodInvoker)(() => Storing_Number.Items.Add(d2["c2"])));
                    //Delay  Thread             
                    Thread.Sleep(1000);
                    NumberSend(  d2["c2"] + "'n");
                    this.BeginInvoke((MethodInvoker)(() => Sending_Box.Items.Add("Sending_To_CLIENT:     " + d2["c2"] + "'n")));
                    this.BeginInvoke((MethodInvoker)(() => Sending_Box.TopIndex = Sending_Box.Items.Count - 1));
                   //  this.BackgroundWorker = new Thread(new ThreadStart(NumberSend));
              //  this.BackgroundWorker.Start();

                           // STW = new StreamWriter(Sending_Box.Items[initial_value].ToString());
                          //  STW.WriteLine(Sending_Box.Items[initial_value]);
                           // STW.AutoFlush = true;
                }
                catch (Exception x)
                {
                }
            }

        }

        public void NumberSend(string mesg)
        {
            try
            {

                Object objData = mesg;
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                for (int i = 0; i < m_clientCount; i++)
                {
                    if (m_workerSocket[i] != null)
                    {
                        if (m_workerSocket[i].Connected)
                        {
                            m_workerSocket[i].Send(byData);
                        }
                    }
                }
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }

        public void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                m_workerSocket[m_clientCount] = m_mainSocket.EndAccept(asyn);

                WaitForData(m_workerSocket[m_clientCount]);
                ++m_clientCount;


                String str = String.Format("Client # {0} connected", m_clientCount);
                Invoke(new Action(() => textBoxMsg.Clear()));
                Invoke(new Action(() => textBoxMsg.AppendText(str)));
                m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "'n OnClientConnection: Socket has been closed'n");
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
        public class SocketPacket
        {
            public System.Net.Sockets.Socket m_currentSocket;
            public byte[] dataBuffer = new byte[1024];
        }

        public void WaitForData(System.Net.Sockets.Socket soc)
        {
            try
            {
                if (pfnWorkerCallBack == null)
                {
                    pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
                }
                SocketPacket theSocPkt = new SocketPacket();
                theSocPkt.m_currentSocket = soc;// could be this one!!!
                soc.BeginReceive(theSocPkt.dataBuffer, 0,
                                   theSocPkt.dataBuffer.Length,
                                   SocketFlags.None,
                                   pfnWorkerCallBack,
                                   theSocPkt);
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                SocketPacket socketData = (SocketPacket)asyn.AsyncState;
                int iRx = 0;
                iRx = socketData.m_currentSocket.EndReceive(asyn);
                char[] chars = new char[iRx + 1];
                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int charLen = d.GetChars(socketData.dataBuffer,
                                         0, iRx, chars, 0);
                System.String szData = new System.String(chars);
                Invoke(new Action(() => richTextBoxSendMsg.AppendText(szData)));
                Invoke(new Action(() => richTextBoxSendMsg.AppendText(Environment.NewLine)));
                WaitForData(socketData.m_currentSocket);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "'nOnDataReceived: Socket has been closed'n");
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            CloseSockets();
            Close();
        }
        String GetIP()
        {
            String strHostName = Dns.GetHostName();
            IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);
            String IPStr = "";
            foreach (IPAddress ipaddress in iphostentry.AddressList)
            {
                if (ipaddress.IsIPv6LinkLocal == false)
                {
                    IPStr = IPStr + ipaddress.ToString();
                    return IPStr;
                }
            }
            return IPStr;
        }

        void CloseSockets()
        {
            if (m_mainSocket != null)
            {
                m_mainSocket.Close();
            }
            for (int i = 0; i < m_clientCount; i++)
            {
                if (m_workerSocket[i] != null)
                {
                    m_workerSocket[i].Close();
                    m_workerSocket[i] = null;
                }
            }
        }


        public Thread BackgroundWorker { get; set; }
    }
}

在WinForms c#中使用套接字连接在服务器端显示多个客户端名称

OnClientConnect()中,IAsyncResult对象应该有关于客户端的信息。在执行++m_clientCount;时,您可以设置一个字典,其中包含来自每个客户机及其ID (IP,端口)的唯一信息。然后在OnDataReceived()中,您可以直接使用GetClientID(Socket client)

更多信息可在以下网站找到:https://msdn.microsoft.com/en-us/library/bbx2eya8 (v = vs.110) . aspx

请注意,在未来,请发布一个最简单的版本你的问题,这样别人就不必看完整的代码。