如何从服务器程序中识别特定的客户端?

时间:2019-04-28 13:54:13

标签: c# tcp tcpclient tcplistener

让我们看看以下程序:

//Server
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyServerProgram
{
    class Program
    {
        static void Main(string[] args)
        {            
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            int port = 2000;
            TcpListener listener = new TcpListener(ip, port);
            listener.Start();

            try
            {
                bool keepRunning = true;

                while (keepRunning)
                {
                    TcpClient client = listener.AcceptTcpClient();
                    Console.WriteLine("Connected " + ((IPEndPoint)client.Client.RemoteEndPoint).Address);

                    Thread thread = new Thread(delegate() 
                        {
                            Program.ServiceMethod(client);
                        });
                    thread.Start();
                }
            }
            catch (Exception ex)
            {
                var inner = ex.InnerException as SocketException;
                if (inner != null && inner.SocketErrorCode == SocketError.ConnectionReset)
                    Console.WriteLine("Disconnected");
                else
                    Console.WriteLine(ex.Message);

                listener.Stop();
            }
        }

        static void ServiceMethod(TcpClient client)
        {
            NetworkStream netStream = client.GetStream();
            BinaryReader br = new BinaryReader(netStream);

            try
            {
                while (client.Client.Connected)
                {
                    string str = br.ReadString();

                    Console.WriteLine(str);
                }
            }
            catch (Exception ex)
            {
                br.Close();
                netStream.Close();
                client.Close();
            }
        }
    }
}

//Client
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MyClientProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 2000;
            TcpClient client = new TcpClient("localhost", port);
            NetworkStream netStream = client.GetStream();
            BinaryWriter br = new BinaryWriter(netStream);

            try
            {
                int i=1;
                while (client.Client.Connected)
                {
                    br.Write(i.ToString());
                    br.Flush();
                    i++;

                    int milliseconds = 2000;
                    System.Threading.Thread.Sleep(milliseconds);
                }
            }
            catch
            {
                br.Close();
                netStream.Close();
                client.Close();
            }
        }
    }
}

他们工作正常。

现在,我需要唯一地标识每个客户端程序,以便可以将消息发送到特定客户端。

我知道可以使用Guid来识别客户。

如何使用它来达到我想要的效果?

0 个答案:

没有答案