1

我正在为游戏制作聊天服务,

我正在使用 TCP 侦听器和客户端获取帐户信息,某种登录服务。我想知道我是否可以让客户端连接到服务器,检查他是否仍然在线,如果他有新消息,请继续向他发送消息。

我已经尝试为登录队列制作一个套接字列表,但是一旦我接受了一个新的套接字,它就会断开前一个套接字与服务器的连接。

byte[] usernameByte = new byte[100];
int usernameRecieved = s.Receive(usernameByte);
//guiController.setText(System.DateTime.Now + " Recieved Login...");

byte[] passByte = new byte[100];
int passRecieved = s.Receive(passByte);
//guiController.setText(System.DateTime.Now + " Recieved Password...");

string username = "";
string password = "";

for (int i = 0; i < usernameRecieved; i++)
     username += (Convert.ToChar(usernameByte[i]));

for (int i = 0; i < passRecieved; i++)
     password += (Convert.ToChar(passByte[i]));

if (DomainController.getInstance().checkAccount(username, password))
{
    ASCIIEncoding asen = new ASCIIEncoding();
    s.Send(asen.GetBytes("true"));
    s.Send(asen.GetBytes("U are succesfully logged in, press enter to continue"));
    guiController.setText(serverName,System.DateTime.Now+"");
    guiController.setText(serverName, "Sent Acknowledgement - Logged in");
}
else
{
    ASCIIEncoding asen = new ASCIIEncoding();
    s.Send(asen.GetBytes("false"));
    s.Send(asen.GetBytes("U are NOT logged in, press enter to continue"));
    guiController.setText(serverName, System.DateTime.Now + "");
    guiController.setText(serverName, "\nSent Acknowledgement - Not logged in");
}

这是我目前用来检查用户发送给我的帐户信息的代码。在我发送这个之后,用户断开了连接,我继续下一个。

我曾尝试制作 1 个单独的套接字列表并一个一个地处理它们,但由于前一个套接字的连接断开,因此失败了,即使它是 2 台不同的机器尝试连接。

有没有人有解决方案/保存套接字的方法,我可以用它来使程序保持所有连接处于活动状态?所以我可以从用户 1 向用户 2 发送消息,并且只使用他们连接的套接字?还是每次他们建立连接时我都需要添加一个ID?

编辑

客户端代码:(这只是一个测试客户端)

while (true)
{
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
tcpclnt.Connect("xx.xxx.xxx.xx", 26862);
// use the ipaddress as in the server program

while(!(checkResponse(tcpclnt.GetStream())))
{
     Thread.Sleep(1000);
}

Console.WriteLine("Connected");

Console.Write("Enter the string to be transmitted : ");

String str = Console.ReadLine();
if (str == "")
{
    str = " ";
}
                    
Stream stm = tcpclnt.GetStream();

ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);


Console.WriteLine("Transmitting.....");

stm.Write(ba, 0, ba.Length);

Console.Write("Enter the string to be transmitted : ");

String str2 = Console.ReadLine();
if (str2 == "")
{
   str2 = " ";
}

Stream stm2 = tcpclnt.GetStream();

ASCIIEncoding asen2 = new ASCIIEncoding();
byte[] ba2 = asen2.GetBytes(str2);


Console.WriteLine("Transmitting.....");

stm.Write(ba2, 0, ba2.Length);
if (str == "false")
{
    blijvenWerken = false;
}
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);

for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(bb[i]));

byte[] bb2 = new byte[100];
int k2 = stm.Read(bb2, 0, 100);
Console.Write("\n");
for (int i = 0; i < k2; i++)
Console.Write(Convert.ToChar(bb2[i]));

Console.WriteLine("\n");

tcpclnt.Close();
Thread.Sleep(1000);
}

服务器获取套接字:这段代码在登录服务器上,因为我每次只能接受 1 个套接字以保持连接处于活动状态,所以我将 queueCount 设置为最大 1。我希望能够列出我接受添加到用户帐户的套接字。

while (loginServerOn)
{
    if (queueCount < 1)
    {
        if (loginServer.getLoginListener().Pending())
        {
            loginQueue.Add(loginServer.getSocket());
            ASCIIEncoding asen = new ASCIIEncoding();
            Socket s = loginQueue.First();
            try
            {
                s.Send(asen.GetBytes("true"));
                queueCount++;
            }
            catch
            {
                loginQueue.Remove(s);
            }
        }
    }
}

返回接受的套接字的函数。

public Socket getSocket()
{
    return myList.AcceptSocket();
}

编辑:问题的本质

我想将收到的 socked 或客户端添加到我的 Account 对象,因此每个连接都有一个与其链接的 Account,当我想向某个帐户发送消息时,它应该向绑定到该帐户的 socked 或客户端发送消息,你能帮助/告诉我如何实现这一目标吗?

4

2 回答 2

1

这仍然是 c# 和套接字,但我的方法与你的不同。

我提出了“connectedCleint”的概念,其目的与您所说的帐户类似。

我有一个名为 ServerTerminal 的类,它负责接受和顶级管理套接字连接。在这我有:

 public Dictionary<long, ConnectedClient> DictConnectedClients =
        new Dictionary<long, ConnectedClient>();

所以这是我的套接字句柄索引的连接客户端列表。

要接受连接,我有一个例程:

public void StartListen(int port)
    {
        socketClosed = false;
        IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);

        listenSocket = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        //bind to local IP Address...
        //if ip address is allready being used write to log
        try
        {
            listenSocket.Bind(ipLocal);
        }
        catch (Exception excpt)
        {
            // Deal with this.. write your own log code here ?
            socketClosed = true;

            return;
        }
        //start listening...

        listenSocket.Listen(100); // Max 100 connections for my app

        // create the call back for any client connections...
        listenSocket.BeginAccept(new AsyncCallback(OnClientConnection), null);

    }

因此,当客户端连接时,它会触发:

private void OnClientConnection(IAsyncResult asyn)
    {
        if (socketClosed)
        {
            return;
        }

        try
        {
            Socket clientSocket = listenSocket.EndAccept(asyn);

            ConnectedClient connectedClient = new ConnectedClient(clientSocket, this, _ServerTerminalReceiveMode);

            //connectedClient.MessageReceived += OnMessageReceived;
            connectedClient.Disconnected += OnDisconnection;
            connectedClient.dbMessageReceived += OndbMessageReceived;

            connectedClient.ccSocketFaulted += ccSocketFaulted;

            connectedClient.StartListening();

            long key = clientSocket.Handle.ToInt64();
            if (DictConnectedClients.ContainsKey(connectedClient.SocketHandleInt64))
            {
                // Already here - use your own error reporting..
            }

            lock (DictConnectedClients)
            {
                DictConnectedClients[key] = connectedClient;
            }

            // create the call back for any client connections...
            listenSocket.BeginAccept(new AsyncCallback(OnClientConnection), null);

        }
        catch (ObjectDisposedException excpt)
        {
            // Your own code here..
        }
        catch (Exception excpt)
        {
            // Your own code here...
        }

    }

这对您来说至关重要的部分是:

            // create the call back for any client connections...
            listenSocket.BeginAccept(new AsyncCallback(OnClientConnection), null);

这将设置服务器终端以接收新连接。

编辑:减少我的连接客户端的版本:

public class ConnectedClient
{
    private Socket mySocket;
    private SocketIO mySocketIO;

    private long _mySocketHandleInt64 = 0;


    // These events are pass through; ConnectedClient offers them but really
    // they are from SocketIO

    public event TCPTerminal_ConnectDel Connected
    {
        add
        {
            mySocketIO.Connected += value;
        }
        remove
        {
            mySocketIO.Connected -= value;
        }
    }

    public event TCPTerminal_DisconnectDel Disconnected
    {
        add
        {
            mySocketIO.Disconnected += value;
        }
        remove
        {
            mySocketIO.Disconnected -= value;
        }
    }

    // Own Events

    public event TCPTerminal_TxMessagePublished TxMessageReceived;

    public delegate void SocketFaulted(ConnectedClient cc);
    public event SocketFaulted ccSocketFaulted;

    private void OnTxMessageReceived(Socket socket, TxMessage myTxMessage)
    {
     // process your message
    }



    private void OnMessageSent(int MessageNumber, int MessageType)
    {
    // successful send, do what you want..
    }





    public ConnectedClient(Socket clientSocket, ServerTerminal ParentST)
    {
        Init(clientSocket, ParentST, ReceiveMode.Handler);
    }

    public ConnectedClient(Socket clientSocket, ServerTerminal ParentST, ReceiveMode RecMode)
    {
        Init(clientSocket, ParentST, RecMode);
    }

    private void Init(Socket clientSocket, ServerTerminal ParentST, ReceiveMode RecMode)
    {
        ParentServerTerminal = ParentST;
        _myReceiveMode = RecMode;

        _FirstConnected = DateTime.Now;
        mySocket = clientSocket;
        _mySocketHandleInt64 = mySocket.Handle.ToInt64();
        mySocketIO = new SocketIO(clientSocket, RecMode);

        // Register for events
        mySocketIO.TxMessageReceived += OnTxMessageReceived;
        mySocketIO.MessageSent += OnMessageSent;
        mySocketIO.dbMessageReceived += OndbMessageReceived;

    }


    public void StartListening()
    {
        mySocketIO.StartReceiving();
    }

    public void Close()
    {
        if (mySocketIO != null)
        {
            mySocketIO.Close();
            mySocketIO = null;
        }

        try
        {
            mySocket.Close();
        }
        catch
        {
            // We're closing.. don't worry about it
        }
    }

    public void SendMessage(int MessageNumber, int MessageType, string Message)
    {
        if (mySocket != null && mySocketIO != null)
        {
            try
            {
                mySocketIO.SendMessage(MessageNumber, MessageType, Message);
            }
            catch
            {
                // mySocketIO disposed inbetween check and call
            }
        }
        else
        {
            // Raise socket faulted event
            if (ccSocketFaulted != null)
                ccSocketFaulted(this);
        }
    }


}

}

一些有用的链接:

这是我开始的地方:http: //vadmyst.blogspot.com.au/2008/01/how-to-transfer-fixed-sized-data-with.html

http://vadmyst.blogspot.com.au/2008/03/part-2-how-to-transfer-fixed-sized-data.html

和..

C# 套接字和多线程

导致连接的套接字在 .BeginReceive 之后立即接受新消息?

http://nitoprograms.blogspot.com.au/2009/04/tcpip-net-sockets-faq.html

http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod

我现在不能发布我的整个解决方案;我需要调试的服务器代码存在缺陷;另外,有些部分我的雇主可能不想发表。但是我的代码基于 Vadym 对可变长度消息的处理。

于 2013-01-16T00:50:13.083 回答
0

当服务器准备好接受 TCP 连接时,它会创建一个新的 TCP 套接字,将其 Bind() 绑定到端口并使用 Listen() 方法。当连接请求进来时,Listen() 方法返回一个新的套接字,服务器和客户端使用该套接字进行通信。此时,服务器和客户端可以使用 Send() 和 Receive() 来回传递数据。如果客户端断开连接,则服务器的 Receive() 以 0 字节数据终止。

如果您想在接受第一个连接后等待另一个连接请求(即,当您与第一个客户端交互时),可以这样做。此时,您需要使用诸如线程或异步方法之类的东西,以便您可以处理多个连接。基本上,您将能够从您的侦听套接字接受()连接请求。

麦克风

于 2013-01-15T22:37:57.453 回答