Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- the sequence events of environment
- client1 connect;
- client2 connect;
- client1 enter username; (for example JOHN)
- client2 enter username;(for example JACK)
- client2 enter password;(for example 123456)
- client1 enter password;(for example abc)
- in the list is see;
- list{
- [0]:
- username : JOHN
- password : 123
- [1]
- username : JACK
- password : 123456
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net;
- using System.Net.Sockets;
- using System.Collections.Concurrent;
- namespace MyTestServer
- {
- class Program
- {
- public byte[] _buffer;
- public byte[] _PASSBUFF;
- private static Socket _ServerSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- private List<SpecificClient> _ClientDirectory = new List<SpecificClient>();
- static void Main(string[] args)
- {
- Console.Title = "Server";
- Program SS = new Program();
- SS.SetupServer();
- //SetupServer();
- Console.ReadLine();
- }
- private void SetupServer()
- {
- Console.WriteLine("Setting up server ...");
- _ServerSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
- _ServerSocket.Listen(5);
- _ServerSocket.BeginAccept(new AsyncCallback(AcceptCallBack), _ServerSocket);
- }
- private void AcceptCallBack(IAsyncResult AR)
- {
- Socket socket = _ServerSocket.EndAccept(AR);
- _ServerSocket.BeginAccept(new AsyncCallback(AcceptCallBack), _ServerSocket);
- Console.WriteLine("Client Connected");
- string RequestForUsername = "Enter your username: ";
- byte[] RFU_byte = Encoding.ASCII.GetBytes(RequestForUsername);
- socket.BeginSend(RFU_byte, 0, RFU_byte.Length, SocketFlags.None, new AsyncCallback(onCompleteRequestForUsername), socket);
- }
- private void onCompleteRequestForUsername(IAsyncResult AR)
- {
- Socket socket = (Socket)AR.AsyncState;
- socket.EndSend(AR);
- _buffer = new byte[1024];
- socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(onUsernameReceived), socket);
- }
- private void onUsernameReceived(IAsyncResult AR)
- {
- Socket socket = (Socket)AR.AsyncState;
- int RcvFromClient = socket.EndReceive(AR);
- byte[] buffer = new byte[RcvFromClient];
- Array.Copy(_buffer, buffer, buffer.Length);
- string RcvUsername = Encoding.ASCII.GetString(buffer);
- SpecificClient NewUser = new SpecificClient();
- NewUser._username = RcvUsername;
- NewUser._UserSocket = socket;
- _ClientDirectory.Add(NewUser);
- buffer = new byte[1024];
- buffer = Encoding.ASCII.GetBytes("rnEnter your password: ");
- socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(onSendPropmtForPass), socket);
- }
- private void onSendPropmtForPass(IAsyncResult AR)
- {
- Socket socket = (Socket)AR.AsyncState;
- socket.EndSend(AR);
- _PASSBUFF = new byte[1024];
- socket.BeginReceive(_PASSBUFF, 0, _PASSBUFF.Length, SocketFlags.None, new AsyncCallback(onReceivePassword), socket);
- }
- private void onReceivePassword(IAsyncResult AR)
- {
- Socket socket = (Socket)AR.AsyncState;
- int ByteCountRcvPass = socket.EndReceive(AR);
- byte[] PassBuff = new byte[ByteCountRcvPass];
- Array.Copy(_PASSBUFF, PassBuff, PassBuff.Length);
- string RcvPassword = Encoding.ASCII.GetString(PassBuff);
- foreach ( SpecificClient Wanted in _ClientDirectory)
- {
- if (Wanted._UserSocket == socket)
- {
- Wanted._password = RcvPassword;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement