Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. the sequence events of environment
  2.  
  3. client1 connect;
  4. client2 connect;
  5.  
  6. client1 enter username; (for example JOHN)
  7. client2 enter username;(for example JACK)
  8. client2 enter password;(for example 123456)
  9. client1 enter password;(for example abc)
  10.  
  11. in the list is see;
  12. list{
  13. [0]:
  14. username : JOHN
  15. password : 123
  16. [1]
  17. username : JACK
  18. password : 123456
  19. }
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Threading.Tasks;
  26. using System.Net;
  27. using System.Net.Sockets;
  28. using System.Collections.Concurrent;
  29.  
  30. namespace MyTestServer
  31. {
  32. class Program
  33. {
  34. public byte[] _buffer;
  35. public byte[] _PASSBUFF;
  36. private static Socket _ServerSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  37. private List<SpecificClient> _ClientDirectory = new List<SpecificClient>();
  38. static void Main(string[] args)
  39. {
  40. Console.Title = "Server";
  41. Program SS = new Program();
  42. SS.SetupServer();
  43. //SetupServer();
  44. Console.ReadLine();
  45. }
  46. private void SetupServer()
  47. {
  48. Console.WriteLine("Setting up server ...");
  49. _ServerSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
  50. _ServerSocket.Listen(5);
  51. _ServerSocket.BeginAccept(new AsyncCallback(AcceptCallBack), _ServerSocket);
  52. }
  53. private void AcceptCallBack(IAsyncResult AR)
  54. {
  55.  
  56. Socket socket = _ServerSocket.EndAccept(AR);
  57. _ServerSocket.BeginAccept(new AsyncCallback(AcceptCallBack), _ServerSocket);
  58. Console.WriteLine("Client Connected");
  59. string RequestForUsername = "Enter your username: ";
  60. byte[] RFU_byte = Encoding.ASCII.GetBytes(RequestForUsername);
  61. socket.BeginSend(RFU_byte, 0, RFU_byte.Length, SocketFlags.None, new AsyncCallback(onCompleteRequestForUsername), socket);
  62.  
  63. }
  64.  
  65. private void onCompleteRequestForUsername(IAsyncResult AR)
  66. {
  67. Socket socket = (Socket)AR.AsyncState;
  68. socket.EndSend(AR);
  69. _buffer = new byte[1024];
  70. socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(onUsernameReceived), socket);
  71.  
  72. }
  73.  
  74.  
  75.  
  76. private void onUsernameReceived(IAsyncResult AR)
  77. {
  78. Socket socket = (Socket)AR.AsyncState;
  79. int RcvFromClient = socket.EndReceive(AR);
  80. byte[] buffer = new byte[RcvFromClient];
  81. Array.Copy(_buffer, buffer, buffer.Length);
  82. string RcvUsername = Encoding.ASCII.GetString(buffer);
  83. SpecificClient NewUser = new SpecificClient();
  84. NewUser._username = RcvUsername;
  85. NewUser._UserSocket = socket;
  86. _ClientDirectory.Add(NewUser);
  87.  
  88. buffer = new byte[1024];
  89. buffer = Encoding.ASCII.GetBytes("rnEnter your password: ");
  90. socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(onSendPropmtForPass), socket);
  91.  
  92.  
  93.  
  94. }
  95. private void onSendPropmtForPass(IAsyncResult AR)
  96. {
  97. Socket socket = (Socket)AR.AsyncState;
  98. socket.EndSend(AR);
  99. _PASSBUFF = new byte[1024];
  100. socket.BeginReceive(_PASSBUFF, 0, _PASSBUFF.Length, SocketFlags.None, new AsyncCallback(onReceivePassword), socket);
  101. }
  102.  
  103. private void onReceivePassword(IAsyncResult AR)
  104. {
  105. Socket socket = (Socket)AR.AsyncState;
  106. int ByteCountRcvPass = socket.EndReceive(AR);
  107. byte[] PassBuff = new byte[ByteCountRcvPass];
  108. Array.Copy(_PASSBUFF, PassBuff, PassBuff.Length);
  109. string RcvPassword = Encoding.ASCII.GetString(PassBuff);
  110.  
  111. foreach ( SpecificClient Wanted in _ClientDirectory)
  112. {
  113. if (Wanted._UserSocket == socket)
  114. {
  115. Wanted._password = RcvPassword;
  116. break;
  117. }
  118. }
  119.  
  120. }
  121.  
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement