当前位置: 首页>>代码示例>>C#>>正文


C# Connection.SendMessage方法代码示例

本文整理汇总了C#中System.Net.Connection.SendMessage方法的典型用法代码示例。如果您正苦于以下问题:C# Connection.SendMessage方法的具体用法?C# Connection.SendMessage怎么用?C# Connection.SendMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Connection的用法示例。


在下文中一共展示了Connection.SendMessage方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: button1_Click

 private void button1_Click(object sender, EventArgs e)
 {
     if (!int.TryParse(textPort.Text, out port))
     {
         MessageBox.Show("Please enter a number in the port section.");
         DialogResult = DialogResult.None;
         return;
     }
     Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     try
     {
         socket.Connect(Hostname, port);
     }
     catch
     {
         MessageBox.Show("Unable to connect to chosen server on specified port.");
         DialogResult = DialogResult.None;
         return;
     }
     try
     {
         Connection con = new Connection(socket, ParentShard);
         player.Name = textUsername.Text;
         SHA256 hasher = SHA256.Create();
         player.PasswordHash = hasher.ComputeHash(Encoding.Unicode.GetBytes(textPassword.Text));
         player.Connection = con;
         con.Player = player;
         con.Start();
         HelloResponse res = (HelloResponse)con.SendMessage(new HelloMessage());
         if (!res.Success)
         {
             MessageBox.Show("Client version incompatible with selected server.");
             DialogResult = DialogResult.None;
             return;
         }
         byte[] publicKeyData = res.PublicKeyData;
         byte[] nonce = res.Nonce;
         Message response = con.SendMessage(new LoginPlayerMessage(player.Name, player.PasswordHash, publicKeyData, nonce));
         LoginPlayerResponseMessage realResponse = response as LoginPlayerResponseMessage;
         if (realResponse != null)
         {
             if (!realResponse.Success)
             {
                 if (MessageBox.Show("Username does not exist or password wrong, should I attempt to create a new player?", "Login failure.", MessageBoxButtons.YesNo) == DialogResult.Yes)
                 {
                     // TODO - confirm password.
                     response = con.SendMessage(new CreatePlayerMessage(player.Name, player.PasswordHash, publicKeyData, nonce));
                     CreatePlayerResponseMessage realResponse2 = response as CreatePlayerResponseMessage;
                     if (realResponse2 != null)
                     {
                         if (realResponse2.Success)
                         {
                             MessageBox.Show("Player successfully created.");
                             return;
                         }
                         else
                         {
                             MessageBox.Show("Name already exists, choose another.");
                             DialogResult = DialogResult.None;
                             return;
                         }
                     }
                     else
                         throw new NotSupportedException("Unexpected response type from server.");
                 }
                 else
                 {
                     DialogResult = DialogResult.None;
                     return;
                 }
             }
         }
         else
             throw new NotSupportedException("Unexpected response received from server.");
     }
     catch
     {
         MessageBox.Show("Unexpected error occured while logging in.");
         DialogResult = DialogResult.None;
         return;
     }
 }
开发者ID:Tilps,项目名称:LoopDeLoop,代码行数:82,代码来源:ConnectForm.cs


注:本文中的System.Net.Connection.SendMessage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。