C#Tcp Ip识别客户端

时间:2018-03-12 12:17:35

标签: c# client-server tcp-ip

我有一个Tcp Ip客户端/服务器连接。 我希望服务器能够通过Id和字符串识别客户端。 为了做到这一点并扩展this我创建了

public class MySocket
{
    private static int Counter = 0;

    public int UniqueId
    {
        get;
        private set;
    }

    public string StrIdentificator;

    protected Socket socket
    {
        get;
        private set;
    }

    #region functions
    ....
    #endregion


    public MySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, string strIdentification)
    {
        socket = new Socket(socketType, protocolType);

        if (socket == null)
        {
            throw new ArgumentNullException("tcpClient null");
        }
        this.socket = socket;
        this.UniqueId = ++MySocket.Counter;
        this.StrIdentificator = strIdentification;
    }
}

因此,我将此作为对象传递:

IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
int port = 11000;
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

// Create a TCP/IP Mysocket.<----------here is where I store more data
var MyClient = new MySocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, "BBBB");

// Connect to the remote endpoint.
MyClient.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), MyClient);
connectDone.WaitOne();

所以,不是传递一个Socket,而是传递一个包含更多数据的MySocket。

该部分适用于客户端。 对于服务器部分,我必须检索该对象

// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);

现在问题就出现了。有没有办法检索MySocket而不是套接字?

提前谢谢你 帕特里克

0 个答案:

没有答案