【问题标题】:Get notified when new connection is established with server与服务器建立新连接时收到通知
【发布时间】:2014-05-01 22:37:58
【问题描述】:

我使用Lidgren library 来通过网络传递字符串抛出计算机。当客户端与服务器断开连接时我可以收到通知,但当客户端连接时我不能。我尝试在不同的线程中运行这段代码:

 static void connectionCheck()
    {
        if (server.ConnectionsCount != con)
        {

            for (int i = con; i < s_server.Connections.Count; i++)
            {
                Console.WriteLine(server.Connections[i].Peer.Configuration.LocalAddress.ToString() + " connected");
            }
            con = server.ConnectionsCount;
        }
        Thread.Sleep(1);
        connectionCheck();
    }

其中con 是旧的连接数。在新连接的客户端发送消息后,我得到的唯一输出是0.0.0.0 connected。为什么这不起作用?在建立新连接时如何获得通知?

【问题讨论】:

    标签: c# networking network-programming lidgren


    【解决方案1】:

    在 s_server 上调用 ReadMessage();当您收到 StatusChanged 类型的消息时,检查第一个字节,它包含一个 NetConnectionStatus 枚举 - 如果它是“已连接”,那么远程对等方刚刚建立了与该对等方的连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2014-02-11
      相关资源
      最近更新 更多