How to broadcast a message to all clients connected to Socket server - c#

I am trying to make a program which can send commands to all clients from 1 server. I am stuck on sending commands to all clients. I currently do have this code:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
private static byte[] _buffer = new byte[1024];
private static List<Socket> _clientSockets = new List<Socket>();
private static int SERVERPORT = 5555;
private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
SetupServer();
while (!Console.ReadKey().Equals("]")) //Stops the Console for closing after I type something in the console
{
}
}
private static void SetupServer()
{
Console.WriteLine("Setting up server...");
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, SERVERPORT));
_serverSocket.Listen(100);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void AcceptCallback(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockets.Add(socket);
Console.WriteLine("Client conntected");
string response = string.Empty;
response = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void ReceiveCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
//int received = socket.EndReceive(AR);
//byte[] dataBuf = new byte[received];
//Array.Copy(_buffer, dataBuf, received);
//string text = Encoding.ASCII.GetString(dataBuf);
string response = string.Empty;
response = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
}
private static void SendCallback(IAsyncResult AR)
{
try {
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
catch {
}
//It gets alot of NullReferenceExceptions. It does work this way but I dont trust it.
}
public static void sendMessage(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
string response = string.Empty;
response = Console.ReadLine();
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), null);
}
}
}
I am able to send commands to each client when its 'their turn'. E.g. I start the server. I start client1 first and client2 second. When I type something in the console its client1's turn first so he receives the command. When I type something again client2 receives the command and so over and over.
I was wondering if there is a way to send it to all clients at the same time. Thanks!

Related

how to connect to my local server from external network computer or within the same network using c# socket

I have set-up a server running on my local host that uses sockets. Also I have created a client application that connects to the server using socket. I have successfully connected to the server from the same computer but what I am trying to do is that to connect to the server from different computer from within the network. What I did is that I ran the server on the local host and on the client side I provided the local IP address of the computer that runs on the server and I set up the port on 90 so the server listen for connection in this port. The problem is that I can't connect to the server and the application keeps trying to connect and attempts with no result. How can I fix this. Also a point to mention is that I have disabled firewall and antivirus to allow for connection but still I get no result. can someone help.
Code for server :
class Program
{
private static IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
private static Socket _serverSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
private static List<Socket> _clientSockerts = new List<Socket>();
private static byte[] _buffer = new byte[1024];
static void Main(string[] args)
{
SetupServer();
Console.ReadLine();
}
private static void SetupServer()
{
Console.WriteLine("Setting UP Server");
_serverSocket.Bind(new IPEndPoint(ipAddress, 90));
_serverSocket.Listen(1);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
}
private static void AcceptCallBack(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockerts.Add(socket);
Console.WriteLine("Client Connects");
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
}
private static void ReceiveCallBack(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int recived = socket.EndReceive(AR);
byte[] dataBuf = new byte[recived];
Array.Copy(_buffer, dataBuf, recived);
string text = Encoding.ASCII.GetString(dataBuf);
Console.WriteLine("text recived: " + text);
string response = string.Empty;
if (text.ToLower() != "get time")
{
response = "Invaild Request";
}
else
{
response = DateTime.Now.ToLongTimeString();
}
byte[] data = Encoding.ASCII.GetBytes(text);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(sendCallBack), socket);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
}
private static void sendCallBack(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
}
Code for client:
class Program
{
private static IPAddress ipAddress = Dns.GetHostEntry("192.168.1.3").AddressList[0];
private static Socket _clientSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
LoopConnect();
sendLoop();
Console.ReadLine();
}
private static void sendLoop()
{
while(true)
{
Thread.Sleep(500);
Console.WriteLine("Enter a request: ");
string req = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(req);
_clientSocket.Send(buffer);
byte[] reciveBuf = new byte[1024];
int rec = _clientSocket.Receive(reciveBuf);
byte[] data = new byte[rec];
Array.Copy(reciveBuf,data,rec);
Console.WriteLine("Recived: " + Encoding.ASCII.GetString(data));
}
}
private static void LoopConnect()
{
int attempts = 0;
while (!_clientSocket.Connected)
{
try
{
attempts++;
_clientSocket.Connect("192.168.1.3", 90);
}
catch (SocketException)
{
Console.Clear();
Console.WriteLine("Connection attempts: " + attempts.ToString());
}
}
Console.Clear();
Console.WriteLine("Connected");
}
}

System.Net.Sockets.SocketException: 'An existing connection was forcibly closed by the remote host'

I create a TCP connection between a server and a client, but after few requests I have this error message :
System.Net.Sockets.SocketException: 'An existing connection was forcibly closed by the remote host'
On this line :
_clientSocket.Send(buffer,0,buffer.Length,SocketFlags.None);
How can I resolve this error ?
Server code
private static Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static List<Socket> _clientSockets = new List<Socket>();
private static byte[] _buffer = new byte[1024];
protected override void OnStart(string[] args)
{
SetupServer();
}
private static void SetupServer()
{
Log.writeEventLog("Setting up server...");
_serverSocket.Bind(new IPEndPoint(IPAddress.Any, 100));
_serverSocket.Listen(1);// max listen client
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void AcceptCallback(IAsyncResult AR)
{
Socket socket = _serverSocket.EndAccept(AR);
_clientSockets.Add(socket);
Log.writeEventLog("Client connected");
socket.BeginReceive(_buffer,0,_buffer.Length,SocketFlags.None,new AsyncCallback(ReceiveCallback),socket);
_serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
private static void ReceiveCallback(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
int received = socket.EndReceive(AR);
byte[] dataBuf = new byte[received];
Array.Copy(_buffer,dataBuf,received);
string text = Encoding.ASCII.GetString(dataBuf);
Log.writeEventLog("TEXT RECEIVED: " + text);
string response = string.Empty;
if(text.ToLower() != "get authentication key")
{
response = "Invalid Request";
}
else
{
response = "TEST";
}
byte[] data = Encoding.ASCII.GetBytes(response);
socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallBack), null);
socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
}
private static void SendCallBack(IAsyncResult AR)
{
Socket socket = (Socket)AR.AsyncState;
socket.EndSend(AR);
}
Client code
private static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
LoopConnect();
SendLoop();
Console.ReadLine();
Console.ReadKey();
}
private static void SendLoop()
{
while (true)
{
Console.WriteLine("Enter a request: ");
string req = Console.ReadLine();
byte[] buffer = Encoding.ASCII.GetBytes(req);
_clientSocket.Send(buffer,0,buffer.Length,SocketFlags.None);
var receivedBuffer = new byte[2048];
int rec = _clientSocket.Receive(receivedBuffer,SocketFlags.None);
byte[] data = new byte[rec];
Array.Copy(receivedBuffer, data, rec);
Console.WriteLine("Received : " + Encoding.ASCII.GetString(data));
}
}
private static void LoopConnect()
{
int attempts = 0;
while (!_clientSocket.Connected)
{
try
{
attempts++;
_clientSocket.Connect(IPAddress.Loopback, 100);
}
catch (SocketException) {
Console.Clear();
Console.WriteLine("Connection attenpts: "+ attempts.ToString());
}
}
Console.Clear();
Console.WriteLine("Connected");
}
Update:
socket.BeginSend(data, 0, data.Length, SocketFlags.None,
new AsyncCallback(SendCallBack), socket);

C# Asynchronous Chat Server

I'm trying to build an Asynchronous Chat Server and this is what I got so far:
Server
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousSocketListener
{
public static ManualResetEvent allDone = new ManualResetEvent(false);
public AsynchronousSocketListener()
{
}
public static void StartListening()
{
byte[] bytes = new Byte[1024];
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
allDone.Reset();
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress ENTER to continue...");
Console.Read();
}
public static void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadCallback), state);
}
public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",content.Length, content);
Send(handler, content);
}
else
{
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadCallback), state);
}
}
}
private static void Send(Socket handler, String data)
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
handler.BeginSend(byteData, 0, byteData.Length, 0,new AsyncCallback(SendCallback), handler);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
Socket handler = (Socket)ar.AsyncState;
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
StartListening();
return 0;
}
}
Client
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 256;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
public class AsynchronousClient
{
private const int port = 11000;
private static ManualResetEvent connectDone =new ManualResetEvent(false);
private static ManualResetEvent sendDone =new ManualResetEvent(false);
private static ManualResetEvent receiveDone =new ManualResetEvent(false);
private static String response = String.Empty;
static String username = "";
static int a = 1;
private static void StartClient()
{
try
{
Console.WriteLine("Username: ");
username = Console.ReadLine();
while (a == 1)
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
Console.WriteLine("Receiver: ");
String receiver = Console.ReadLine();
Console.WriteLine("Message: ");
String message = Console.ReadLine();
String Message = username + "[" + receiver + "[" + message + "<EOF>";
Send(client, Message);
sendDone.WaitOne();
Receive(client);
receiveDone.WaitOne();
Console.WriteLine("Response received : {0}", response);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReceiveCallback), state);
}
else
{
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Send(Socket client, String data)
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
client.BeginSend(byteData, 0, byteData.Length, 0,new AsyncCallback(SendCallback), client);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static int Main(String[] args)
{
StartClient();
return 0;
}
}
I'm able to send messages to the server and get back an answer, but the connection is cut every time and I have to reconnect it and like this I wouldn't be able to get messages after I send one. I'm looking for a way to connect my client to the server, send messages in both ways and stop the connection manualy.
Furthermore I'm looking for a way to send messages from one client to another one and to send a message to all clients, who are connected to the server.
Another Question I have, how can I set up multiple ports and let the server listen to all ports? I wanted to open one port for the login and one port for the messages.
I'm looking for a way to connect my client to the server, send messages in both ways and stop the connection manualy.
Closing the connection could be done by adding a disconnect call after the loop: client.Disconnect(false). After some exit condition you simply quit the loop.
It would be preferable to create the client in a using statement though, this way it will be disposed automatically.
but the connection is cut every time
because your explicitly close it in SendCallback
Socket handler = (Socket) ar.AsyncState;
handler.Shutdown(SocketShutdown.Both);
handler.Close();
You may call handler.BeginReceive instead

Using sockets to send information from server to client

I've setup a server and a client, trying to send a string from my server to my client.
As it appears, my client does not received the message, and as I'm new to networking I can't seem to find the error.
My server:
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.Threading;
namespace Server
{
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
class Program
{
public static ManualResetEvent allDone = new ManualResetEvent(false);
public static ManualResetEvent sendDone = new ManualResetEvent(false);
public static void StartListening()
{
byte[] bytes = new byte[1024];
IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostEntry.AddressList[0];
IPEndPoint localEndPointOut =
new IPEndPoint(ipAddress, 22222);
Console.WriteLine(ipAddress);
Socket listenerOut = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
StateObject state = new StateObject();
Socket handler = state.workSocket;
try
{
listenerOut.Bind(localEndPointOut);
listenerOut.Listen(100);
while (true)
{
allDone.Reset();
listenerOut.BeginAccept(new AsyncCallback(outputCallback), listenerOut);
allDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("\nPress enter to continue...");
Console.Read();
}
public static void outputCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(outputStream), state);
}
public static void outputStream(IAsyncResult ar)
{
String content = String.Empty;
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
byte[] byteData = Encoding.ASCII.GetBytes("test");
while (true)
{
try
{
handler.Send(byteData, SocketFlags.None);
}
catch
{
Console.WriteLine("Client forcebly closed.");
}
}
}
static void Main(string[] args)
{
StartListening();
Console.ReadKey();
}
}
}
And my client:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace OutputClient
{
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 256;
public byte[] buffer = new byte[BufferSize];
public StringBuilder sb = new StringBuilder();
}
class Program
{
private const int port = 22222;
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
private static String response = String.Empty;
private static void StartClient()
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
Socket client = new Socket(ipAddress.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
Receive(client);
receiveDone.WaitOne();
Console.WriteLine(response);
}
private static void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void Receive(Socket client)
{
try
{
StateObject state = new StateObject();
state.workSocket = client;
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static void Main(string[] args)
{
StartClient();
Console.ReadKey();
}
}
}
Currently I'm using the Socket.Send method on my server, I also tried using Socket.BeginSend, but with same result.
I don't get any errors, there just seem to be data being send. The server registers the client however.
Your server calls BeginReceive and in its handler (outputCallback) it attempts to send data. This means that the server won't send data until it has received something from the client first.
The client however never sends any data; only when you close the client your server will receive a 0-byte message and start sending data (which of course isn't picked up by the client anymore).

c# Buffer conflict on concurrent TCP clients in BeginReceive

i wrote a TCP server (Async Socket method in c# language) that can be get username and password from the clients, when one client connect and supply username and password everything will be work correctly and next users can store their username and password in a list, but when one client connect and then second client connect concurrently the second connected user's password take the place but with correct digit number of first client password;
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
}
as you see in above the first connected password is in correct size but with secondly connected clients password (which enter password earlier)
my codes is :
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("\r\nEnter 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;
}
}
}
}
}

Categories

Resources