Login.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace SocketChat.Client
  12. {
  13. public partial class Login : Form
  14. {
  15. #region 用户定义
  16. public Socket ClientSocket { get; set; }
  17. public User p_user { get; set; }
  18. #endregion
  19. #region 用户函数
  20. public Login()
  21. {
  22. InitializeComponent();
  23. }
  24. #endregion
  25. #region 用户函数
  26. #region 暂时不用
  27. /// <summary>
  28. /// 客户端接收数据
  29. /// </summary>
  30. /// <param name="socket"></param>
  31. public void fnReceiveData(object socket)
  32. {
  33. var __clientSocket = socket as Socket;
  34. byte[] __data = new byte[1024 * 1024];
  35. while (true)
  36. {
  37. int __iLen = 0;
  38. try
  39. {
  40. __iLen = __clientSocket.Receive(__data, 0, __data.Length, SocketFlags.None);
  41. }
  42. catch (Exception)
  43. {
  44. this.StopConnect();
  45. return;
  46. }
  47. }
  48. }
  49. private void StopConnect()
  50. {
  51. if (ClientSocket.Connected)
  52. {
  53. ClientSocket.Shutdown(SocketShutdown.Both);
  54. ClientSocket.Close(100);
  55. }
  56. }
  57. #endregion
  58. #endregion
  59. #region 窗体事件
  60. private void btnLogin_Click(object sender, EventArgs e)
  61. {
  62. try
  63. {
  64. User __user = new User();
  65. __user.p_serverIP = this.txtIP.Text;
  66. __user.p_userName = this.txtLoginName.Text;
  67. __user.p_serverPort = this.txtPort.Text;
  68. this.p_user = __user;
  69. this.DialogResult = DialogResult.OK;
  70. }
  71. catch (Exception ex)
  72. {
  73. throw ex;
  74. }
  75. }
  76. #endregion
  77. }
  78. }