Example #1
0
    private void Broadcaster_DataReceived(object sender, AsyncSocketEventArgs e)
    {
        LogTool.Log(e.msg);
        //switch (e.msg) {
        //    case Command.EXIT:
        //        broadcaster.Close(e.state);
        //        break;
        //}
        if (e.msg.StartsWith(NetworkCommand.LOGIN) && clientDic.Count <= maxCount)
        {
            string[] result = e.msg.Split(';');
            LogTool.Log("Login……");
            int        id            = int.Parse(result[1].Split(':')[1]);
            string     userName      = result[2].Split(':')[1];
            string     password      = result[3].Split(':')[1];
            IPEndPoint remotePoint   = e.state.remote;
            string     remoteAddress = remotePoint.Address.ToString();

            if (!clientDic.ContainsKey(remoteAddress))
            {
                clientDic.Add(remoteAddress, userName);
            }
            Loom.DispatchToMainThread(() =>
            {
                if (!studentDic.ContainsKey(userName))
                {
                    //StudentItemView item = CreateStudent(studentCopy);
                    //item.IPAddress = remoteAddress;
                    //item.studentState = e.state;
                    //item.UserName = userName;
                    //studentDic.Add(userName, item);
                }
            });
            LogTool.Log("客户端:" + userName + "IP:" + remoteAddress);

            SaveDataController.CreateUser(id, userName, password);
            SaveDataController.SetServerPort(ipAddress, tcpPort);
            udpServer.sendMessage(NetworkCommand.ISCONNECTED);
            StartServer();
        }
    }
Example #2
0
 private void UdpClient_DataReceived(object sender, AsyncSocketEventArgs e)
 {
     LogTool.Log(e.msg);
     if (e.msg == NetworkCommand.ISCONNECTED)
     {
         serverPoint = new IPEndPoint(e.state.remote.Address, serverTcpPort);
         LogTool.Log(serverPoint);
         isConnected = true;
         startTCP();
         SaveDataController.CreateUser(id, userName, password);
         SaveDataController.SetServerPort(serverPoint.Address.ToString(), serverTcpPort);
         //closeUDP();
     }
     else if (e.msg.StartsWith(NetworkCommand.CONNECT) && !isConnected)
     {
         serverTcpPort = int.Parse(e.msg.Split(':')[1]);
         udpClient.AsynSend(NetworkCommand.LOGIN + ";id:" + id + ";userName:"******";psd:" + password);//连接中,发送用户名密码
     }
     else if (e.msg == NetworkCommand.EXIT)
     {
         CloseClient();
         LogTool.Log("被服务器踢出");
     }
 }