Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

100 rindas
2.3 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. namespace StuMgmClient
  6. {
  7. class Comm
  8. {
  9. #region 基本函数
  10. const int bufSize = 1024 * 1024;
  11. static IPEndPoint m_ipEndpoint;
  12. static Socket m_socket;
  13. static byte[] m_buf;
  14. void Init(string ip,int port)
  15. {
  16. IPAddress ipAdress = IPAddress.Parse(ip);
  17. m_ipEndpoint = new IPEndPoint(ipAdress, port);
  18. m_buf = new byte[bufSize];
  19. }
  20. void Connect()
  21. {
  22. try
  23. {
  24. m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  25. m_socket.Connect(m_ipEndpoint);
  26. }
  27. catch (Exception ex)
  28. {
  29. Debug.Print(ex.Message);
  30. }
  31. }
  32. bool Send(byte[] data)
  33. {
  34. try
  35. {
  36. m_socket.Send(data);
  37. return true;
  38. }
  39. catch(Exception e)
  40. {
  41. Debug.Print(e.Message);
  42. return false;
  43. }
  44. }
  45. bool Receive()
  46. {
  47. try
  48. {
  49. Array.Clear(m_buf, 0, m_buf.Length);
  50. m_socket.Receive(m_buf);
  51. return true;
  52. }
  53. catch (Exception e)
  54. {
  55. Debug.Print(e.Message);
  56. return false;
  57. }
  58. }
  59. void DisConnect()
  60. {
  61. client.Close();
  62. }
  63. #endregion
  64. internal ErrCode VerifLogin(string userName,string pawssword,out Roles role)
  65. {
  66. role = Roles.Error;
  67. Connect();
  68. UserInfo cs = new UserInfo(userName,pawssword);
  69. byte[] sendBuf;
  70. if(Utility.BinSerialize(cs,out sendBuf ))
  71. return ErrCode.FailSerial;
  72. if (Send(sendBuf))
  73. return ErrCode.FailSend;
  74. if (Receive())
  75. return ErrCode.FailReceive;
  76. object o;
  77. if (Utility.BinDeserialize(m_buf, out o))
  78. return ErrCode.FailDeserial;
  79. if (!(o is UserInfo))
  80. return ErrCode.ErrData;
  81. DisConnect();
  82. role = ((UserInfo)o).UserRole;
  83. return ErrCode.Success;
  84. }
  85. }
  86. }