UDP with multiple sender and one single receiver - c#

In a project I have multiple senders of UDP data, but only a single receiver.
If traffic gets higher, the receiver "forgets" lots of packets. I know that this is a problem inherent to UDP, but I want to minimize the number of lost packets.
I have read this SO question before!
Here is my code (Please excuse the massive code blocks, but I wanted the example to be self contained)
Sender side:
public class UdpSender
{
private static int portNumber = 15000;
public void Send(string data)
{
var udpServer = new UdpClient();
var ipEndPoint = new IPEndPoint(IPAddress.Broadcast, portNumber);
byte[] bytes = Encoding.ASCII.GetBytes(data);
udpServer.Send(bytes, bytes.Length, ipEndPoint);
udpServer.Close();
}
}
class Program
{
static void Main(string[] args)
{
var sender = new UdpSender();
var count = 100000;
for (var i = 0; i < count; ++i)
{
sender.Send(i.ToString());
}
Console.ReadKey();
}
}
Receiver side:
public class UDPListener
{
private static int portNumber = 15000;
private readonly UdpClient udp = new UdpClient(portNumber);
public volatile int CountOfReceived = 0;
public void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult ar)
{
Interlocked.Increment(ref CountOfReceived);
IPEndPoint ip = new IPEndPoint(IPAddress.Any, portNumber);
byte[] bytes = udp.EndReceive(ar, ref ip);
string message = Encoding.ASCII.GetString(bytes);
StartListening();
}
}
class Program
{
static void Main(string[] args)
{
var listener = new UDPListener();
listener.StartListening();
while (true)
{
Console.WriteLine(listener.CountOfReceived.ToString("000,000,000"));
}
}
}
Right now if I start 5 sender applications in parallel, only about a quarter of the messages is received. This is only on my local machine.
What can I do to minimize packets lost?

thinking about the speed you're sending your packets on their trip i would say that the sinks buffer might be filled in a bit of time - increasing the buffer size (as shown here) could solve this issue, i guess.

Related

C# TCP Socket callback not returning bytes for first sent message

I have a console app that listens for client socket connections and the packets being sent. Once a packet is sent, it outputs the length of the packet. I am trying to create a bit more organization to my application and keep some of these actions within their own objects. However, I have run into a small issue where the first message comes back as 0, but the rest of them work just fine.
I have an object that handles the data being received:
public class Receiver : IDisposable
{
private byte[] _buffer;
public Receiver(byte[] buffer)
{
this._buffer = buffer;
}
public void DoWork(IAsyncResult result)
{
Socket clientSocket = (Socket)result.AsyncState;
int bufferSize = clientSocket.EndReceive(result);
byte[] packet = new byte[bufferSize];
Array.Copy(_buffer, packet, packet.Length);
// Handle the packet
ushort packetLength = BitConverter.ToUInt16(packet, 0);
Console.WriteLine("Packet received... Length: {0}", packetLength);
_buffer = new byte[1024];
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, this.DoWork, clientSocket);
}
public void Dispose()
{
}
}
An object that handles incoming connections:
public class Connector : IDisposable
{
private Socket _socket;
private Receiver _receiver;
private byte[] _buffer;
public Connector(Socket socket, Receiver receiver, byte[] buffer)
{
this._socket = socket;
this._receiver = receiver;
this._buffer = buffer;
}
public void DoWork()
{
_socket.BeginAccept(this.HandleAcceptedCallback,null);
}
public void Dispose()
{
}
private void HandleAcceptedCallback(IAsyncResult result)
{
Socket clientSocket = _socket.EndAccept(result);
_buffer = new byte[1024];
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, _receiver.DoWork, clientSocket);
this.DoWork();
}
}
And then my console object:
static void Main(string[] args)
{
byte[] buffer = new byte[1024];
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
using (Receiver receiver = new Receiver(buffer))
using (Connector connector = new Connector(socket, receiver, buffer))
{
socket.Bind(11000);
socket.Listen(500);
connector.DoWork();
while (true)
Console.ReadLine();
}
}
I have been trying to play around with different approaches and have found out that if I move the DoWork method from the Receiver object into the Connector object and wire the callbacks up there, it all works fine.
I was wondering if anyone would be able to tell me where I am going wrong, or why this is not practical? I am new to using sockets and apologize for any misunderstandings.

Getting multicast messages with UDPClient

I've got a product plugged directly into the ethernet port of my computer, sending multicast packets to 224.224.10.10 and UDP port 24588. I've set up my UDPclient in the code below, and I feel like I have it set up correctly, but I still don't receive any packets. I always catch an exception saying that I timed out waiting for a response. Any thoughts or glaring mistakes?
I looked at a lot of questions before posting this, but I couldn't get a resolution, and I couldn't find anyone who had the same type of setup that I have.
public class ReceiverClass
{
private UdpClient m_UDPClient = null;
private Thread m_UDPReceiverThread = null;
private bool m_ContinueReceiving = false;
private readonly object m_sync = new object();
private const int UDP_PORT = 24588;
public ReceiverClass()
{
m_ContinueReceiving = true;
m_UDPClient = new UdpClient(UDP_PORT);
m_UDPClient.Client.ReceiveTimeout = 20000;
m_UDPReceiverThread = new Thread(ReceiveData) { IsBackground = true };
m_UDPReceiverThread.Start();
}
private void ReceiveData()
{
bool Continue;
byte[] ReceiveBuffer;
IPEndPoint defaultIP = new IPEndPoint(IPAddress.Any, 0);
m_UDPClient.JoinMulticastGroup(IPAddress.Parse("224.224.10.10"));
m_UDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
lock (m_sync)
{
Continue = m_ContinueReceiving;
}
while (Continue)
{
try
{
ReceiveBuffer = m_UDPClient.Receive(ref defaultIP);
if (null != ReceiveBuffer)
{
// Do stuff with received...
}
}
catch (Exception e)
{
// ooo eee kill stream
Dispose(false);
return;
}
finally
{
lock (m_sync)
{
Continue = m_ContinueReceiving;
}
}
}
}
}
I can't see anything immediately wrong with your code so I don't have a proper answer for you. However, here's a piece of code I'm using to get UDP broadcast messages. Hopefully it will work for you, or give you some new ideas:
class UdpHandler {
public UdpHandler(int portNo) {
Thread t = new Thread(ListenThread);
t.IsBackground = true;
t.Start(portNo);
}
public void ListenThread(object portNo) {
UdpClient client = new UdpClient { ExclusiveAddressUse = false };
IPEndPoint localEp = new IPEndPoint(IPAddress.Any, (int)port);
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.ExclusiveAddressUse = false;
client.Client.Bind(localEp);
while (true) {
byte[] data = client.Receive(ref localEp);
DataReceived(data);
}
}
private void DataReceived(byte[] rawData) {
// Handle the received data
}
}
What I ended up doing was going to the Sockets class and looking at the raw packets and picking out what I needed. I bind the socket to my lan interface, and sniff stuff out. I had to run it as an administrator in order to get it to work, but thats fine. I went this route because I could see the packets in wireshark, but they weren't getting to my udpclient. This ended up being the fastest way to get what I wanted.
Socket ReceiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
EndPoint DefaultIPEndpoint = new IPEndPoint(IPAddress.Parse("10.0.2.0"), 0);
ReceiveSocket.ReceiveTimeout = 5000;
ReceiveSocket.Bind(DefaultIPEndpoint);
ReceiveSocket.IOControl(IOControlCode.ReceiveAll, new byte[4] { 1, 0, 0, 0 }, null);
while (true)
{
byte[] ReceiveBuffer = new byte[512];
int ByteCount = 0;
ByteCount = ReceiveSocket.ReceiveFrom(ReceiveBuffer, ref DefaultIPEndpoint);
// Handle packets ...
}

C# Async Sockets - Get Data

I'm building a simple server, that contains some information and that will be using Async Sockets to get N Connections (I don't really know how many, they can range from 50 to 1000).
The main purpose of this server is to 'verify' the file integrity and version of clients connecting, and if needed send them the updated files so they can patch.
Right now, I'm on the part of getting my sockets running, I can display the data in the console, and everything is fine, problem is, my most important variables are on my 'DownloadServer' class, while my socket functions are on a class named 'ServerSocket'.
My question is, how can I access the data in DownloadServer(that contains an instance of ServerSocket) when said info is requested to my socket. I can't return the data received with Async Callbacks (because they must be void), so what would be the best way to go about this?
Here's my socket code, it only receives a client and prints data sent. What I want to do is acess the received info (likewise, a command for example) and return to the client the request info, for example, if the message received is "GetFileVersion", the server should reply back with the value of the variable it contains (it's a global variable named FILEVERSION), being the problem that I can't seem to find a way to acces that variable.
class ServerSocket
{
private Socket _socket;
private byte[] _buffer;
public ServerSocket()
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void bind(int port)
{
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
}
public void Listen(int backlog)
{
_socket.Listen(backlog);
}
public void Accept()
{
_socket.BeginAccept(AcceptCallBack, null);
}
private void AcceptCallBack(IAsyncResult ar)
{
Socket clientSocket = _socket.EndAccept(ar);
Accept();
IPEndPoint clientEndPoint = (IPEndPoint)clientSocket.RemoteEndPoint;
Console.WriteLine("Client Connected: {0}",clientEndPoint.Address.ToString());
_buffer = new byte[2048];
clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, ReceivedCallback, clientSocket);
}
private void ReceivedCallback(IAsyncResult ar)
{
try
{
Socket clientSocket = ar.AsyncState as Socket;
int size = clientSocket.EndReceive(ar);
byte[] packet = new byte[size];
Array.Copy(_buffer, packet, packet.Length);
String data = System.Text.Encoding.ASCII.GetString(packet, 0, packet.Length);
}
catch (Exception ex)
{
;
}
}
}
}
Thank you in advance!
EDIT:
What I'd like to do is something like String commandReceived = socket.Receive() (like you would do with synchronous sockets), or some way to acess that command received via that socket.
class DownloadServer
{
private static String UPDATEPATH="";
private static int FILEVERSION;
private static string ROOTPATH = "";
private String SQLUsername = "";
private String SQLPassword = "";
private String AccountDB = "";
private String ServerName = "";
private SqlConnection sqlConn;
//some logic methods
static void Main(string[] args){
ServerSecurity serverSecurity = new ServerSecurity();
ServerSocket serverSocket = new ServerSocket();
serv.Initialize();
serverSecurity.Initialize();
serverSocket.bind(15779);
serverSocket.Listen(1000);
serverSocket.Accept();
while (true)
{
Console.WriteLine("Input the command:");
var Input = Console.ReadLine();
if (Input == "Update")
{
serv.prepareUpdate();
}
else if (Input == "Exit")
{
Environment.Exit(0);
}
else if (Input == "TestEncryption")
{
Console.WriteLine("Enter the text to be encrypted:");
String plainText = Console.ReadLine();
Console.WriteLine("Original Text = {0}", plainText);
Console.WriteLine("Encrypted Text = {0}", serverSecurity.Encrypt(plainText));
Console.WriteLine("Decrypted Text = {0}", serverSecurity.Decrypt(serverSecurity.Encrypt(plainText)));
}
}
}
Add in your DownloadServer Class :
Socket NewSocket;
byte[] buffer = new buffer [1024];
NewSocket = serverSocket.Accept();
Then begin Receive from this NewSocket :
void Receive()
{
NewSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, r);
}
private void ReceiveCallback(IAsyncResult ar)
{
Socket CurrentClient = (Socket)ar.AsyncState;
int Received;
try { Received = CurrentClient.Receive(buffer); }
catch
{
return;
}
byte[] _buffer = new byte[Received];
Array.Copy(buffer, _buffer, Received);
string _Text = Encoding.Unicode.GetString(_buffer);
if(_Text == "GetVersion")
byte[] infoVersion = Encoding.Encoding.Default.GetBytes(FILEVERSION);
CurrentClient .Send(infoVersion )
}
This looks like a classic design problem where you are attempting to give a class too many responsibilities. In SOLID design, each class should have a single responsibility. The SocketServer would then only be responsible for creating the socket and sending/receiving data. Your DownloadServer would be responsible for creating data to be sent. Another class say ValidationServer, would be responsible for requesting data through the SocketServer and parsing it. A third class, a controller if you will, would be responsible for coordinating calls and passing data between these classes. In this way your SocketServer and DownloadServer would be decoupled and not require variables from each other.

C# UDP Socket client and server

My first question here. I am new to this kind of programming, and i've only programmed .NET web sites and forms.
Now, the company I work at, asks me to make an ActiveX component, that listens to UDP messages, and turns them into events.
The UDP msgs are send from Avaya system, so i was told that to test my ActiveX, at first I need to create an app, that only sends UDP (only one button that sends pre-defined UDP string). And then create listener socket, ordinary C# app, that will get those transmitted UDP string from the tests app. Both apps will work on the same machine.
Later, when i get this working, i need to make the listener an ActiveX component, but first things first.
I need to know if there are any good tutorials about this, and any idea on how to start? I am sorry for my ignorance, but i am really new on this and i don't really have any time to learn this since it has to be done in 2 weeks.
Thanks in advance.
edit: I managed to create 2 simple console applications, and was sending UDP messages between them successfully. The sender will be only for testing, and now I need to re-make my receiver to get the UDP message and 'translate' it to events. And lastly, to make it an ActiveX control...
Simple server and client:
public struct Received
{
public IPEndPoint Sender;
public string Message;
}
abstract class UdpBase
{
protected UdpClient Client;
protected UdpBase()
{
Client = new UdpClient();
}
public async Task<Received> Receive()
{
var result = await Client.ReceiveAsync();
return new Received()
{
Message = Encoding.ASCII.GetString(result.Buffer, 0, result.Buffer.Length),
Sender = result.RemoteEndPoint
};
}
}
//Server
class UdpListener : UdpBase
{
private IPEndPoint _listenOn;
public UdpListener() : this(new IPEndPoint(IPAddress.Any,32123))
{
}
public UdpListener(IPEndPoint endpoint)
{
_listenOn = endpoint;
Client = new UdpClient(_listenOn);
}
public void Reply(string message,IPEndPoint endpoint)
{
var datagram = Encoding.ASCII.GetBytes(message);
Client.Send(datagram, datagram.Length,endpoint);
}
}
//Client
class UdpUser : UdpBase
{
private UdpUser(){}
public static UdpUser ConnectTo(string hostname, int port)
{
var connection = new UdpUser();
connection.Client.Connect(hostname, port);
return connection;
}
public void Send(string message)
{
var datagram = Encoding.ASCII.GetBytes(message);
Client.Send(datagram, datagram.Length);
}
}
class Program
{
static void Main(string[] args)
{
//create a new server
var server = new UdpListener();
//start listening for messages and copy the messages back to the client
Task.Factory.StartNew(async () => {
while (true)
{
var received = await server.Receive();
server.Reply("copy " + received.Message, received.Sender);
if (received.Message == "quit")
break;
}
});
//create a new client
var client = UdpUser.ConnectTo("127.0.0.1", 32123);
//wait for reply messages from server and send them to console
Task.Factory.StartNew(async () => {
while (true)
{
try
{
var received = await client.Receive();
Console.WriteLine(received.Message);
if (received.Message.Contains("quit"))
break;
}
catch (Exception ex)
{
Debug.Write(ex);
}
}
});
//type ahead :-)
string read;
do
{
read = Console.ReadLine();
client.Send(read);
} while (read != "quit");
}
}
Simple server and client:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
class Program
{
static void Main(string[] args)
{
// Create UDP client
int receiverPort = 20000;
UdpClient receiver = new UdpClient(receiverPort);
// Display some information
Console.WriteLine("Starting Upd receiving on port: " + receiverPort);
Console.WriteLine("Press any key to quit.");
Console.WriteLine("-------------------------------\n");
// Start async receiving
receiver.BeginReceive(DataReceived, receiver);
// Send some test messages
using (UdpClient sender1 = new UdpClient(19999))
sender1.Send(Encoding.ASCII.GetBytes("Hi!"), 3, "localhost", receiverPort);
using (UdpClient sender2 = new UdpClient(20001))
sender2.Send(Encoding.ASCII.GetBytes("Hi!"), 3, "localhost", receiverPort);
// Wait for any key to terminate application
Console.ReadKey();
}
private static void DataReceived(IAsyncResult ar)
{
UdpClient c = (UdpClient)ar.AsyncState;
IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receivedBytes = c.EndReceive(ar, ref receivedIpEndPoint);
// Convert data to ASCII and print in console
string receivedText = ASCIIEncoding.ASCII.GetString(receivedBytes);
Console.Write(receivedIpEndPoint + ": " + receivedText + Environment.NewLine);
// Restart listening for udp data packages
c.BeginReceive(DataReceived, ar.AsyncState);
}
}
Server
public void serverThread()
{
UdpClient udpClient = new UdpClient(8080);
while(true)
{
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
lbConnections.Items.Add(RemoteIpEndPoint.Address.ToString()
+ ":" + returnData.ToString());
}
}
And initialize the thread
private void Form1_Load(object sender, System.EventArgs e)
{
Thread thdUDPServer = new Thread(new ThreadStart(serverThread));
thdUDPServer.Start();
}
Client
private void button1_Click(object sender, System.EventArgs e)
{
UdpClient udpClient = new UdpClient();
udpClient.Connect(txtbHost.Text, 8080);
Byte[] senddata = Encoding.ASCII.GetBytes("Hello World");
udpClient.Send(senddata, senddata.Length);
}
Insert it to button command.
Source: http://technotif.com/creating-simple-udp-server-client-transfer-data-using-c-vb-net/

C# Socket Server - Issue with Concurrent Connections

hey all,
I have made a socket server in C# for a flash game that I am developing, I got the code from somewhere and I am a beginner in c# and .net development . It works fine in practice when connections are made and the server functions correctly. Get 2 concurrent connections at the same time and we have a problem.
here is the basic aspects of the socket server below: (alot taken out for obvious reasons)
how can I alter this so that it can handle concurrent connections? Should I be threading each response?
Thanks
class TcpSock
{
int tcpIndx = 0;
int tcpByte = 0;
byte[] tcpRecv = new byte[1024];
////////////////////////////////////////
public Socket tcpSock;
////////////////////////////////////////
public int Recv(ref string tcpRead)
{
tcpByte = tcpSock.Available;
if (tcpByte > tcpRecv.Length - tcpIndx)
tcpByte = tcpRecv.Length - tcpIndx;
tcpByte = tcpSock.Receive(tcpRecv, tcpIndx, tcpByte,
SocketFlags.Partial);
tcpRead = Encoding.ASCII.GetString
(tcpRecv, tcpIndx, tcpByte);
tcpIndx += tcpByte;
return tcpRead.Length;
}
public int RecvLn(ref string tcpRead)
{
tcpRead = Encoding.ASCII.GetString
(tcpRecv, 0, tcpIndx);
tcpIndx = 0;
return tcpRead.Length;
}
public int Send(string tcpWrite)
{
return tcpSock.Send(Encoding.ASCII.GetBytes(tcpWrite));
}
public int SendLn(string tcpWrite)
{
return tcpSock.Send(Encoding.ASCII.GetBytes(tcpWrite + "\r\n"));
}
}
[STAThread]
static void Main()
{
Thread Server1 = new Thread(RunServer);
Server1.Start();
}
static void RunServer()
{
///class IPHostEntry : Stores information about the Host and is required
///for IPEndPoint.
///class IPEndPoint : Stores information about the Host IP Address and
///the Port number.
///class TcpSock : Invokes the constructor and creates an instance.
///class ArrayList : Stores a dynamic array of Client TcpSock objects.
IPHostEntry Iphe = Dns.Resolve(Dns.GetHostName());
IPEndPoint Ipep = new IPEndPoint(Iphe.AddressList[0], 4444);
Socket Server = new Socket(Ipep.Address.AddressFamily,SocketType.Stream, ProtocolType.Tcp);
///Initialize
///Capacity : Maximux number of clients able to connect.
///Blocking : Determines if the Server TcpSock will stop code execution
///to receive data from the Client TcpSock.
///Bind : Binds the Server TcpSock to the Host IP Address and the Port Number.
///Listen : Begin listening to the Port; it is now ready to accept connections.
ArrayList Client = new ArrayList();
string[,] Users = new string[1000,9];
string rln = null;
string[] Data;
Client.Capacity = 1000;
Server.Blocking = false;
Server.Bind(Ipep);
Server.Listen(32);
Console.WriteLine("Server 1 {0}: listening to port {1}", Dns.GetHostName(), Ipep.Port);
////////////////////////////////////////////////////////////////////////////////////////////
///Main loop
///1. Poll the Server TcpSock; if true then accept the new connection.
///2. Poll the Client TcpSock; if true then receive data from Clients.
while (true)
{
//Accept - new connection
#region new connection
if (Server.Poll(0, SelectMode.SelectRead))
{
int i = Client.Add(new TcpSock());
((TcpSock)Client[i]).tcpSock = Server.Accept();
Console.WriteLine("Client " + i + " connected.");
Users[i, 0] = i.ToString();
}
#endregion
for (int i = 0; i < Client.Count; i++)
{
//check for incoming data
if (((TcpSock)Client[i]).tcpSock.Poll(0, SelectMode.SelectRead))
{
//receive incoming data
if (((TcpSock)Client[i]).Recv(ref rln) > 0)
{
Console.WriteLine(rln.ToString());
Data = rln.Split('|');
// 1) initial connection
#region InitialConnection
if (Data[0] == "0000")
{
}
}
}
}
}
}
You will need to not use synchronous functions but asynchrounus functions like Socket.BeginReceive
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
public static partial class TcpServer
{
public static void Main()
{
// Setup listener on "localhost" port 12000
IPAddress ipAddr = Dns.GetHostEntry("localhost").AddressList[0];
TcpListener server = new TcpListener(ipAddr, 12000);
server.Start(); // Network driver can now allow incoming requests
// Accept up to 1 client per CPU simultaneously
Int32 numConcurrentClients = Environment.ProcessorCount;
for (Int32 n = 0; n
private static Byte[] ProcessData(Byte[] inputData)
{
String inputString = Encoding.UTF8.GetString(inputData, 1, inputData[0]);
String outputString = inputString.ToUpperInvariant();
Console.WriteLine("Input={0}", inputString);
Console.WriteLine(" Output={0}", outputString);
Console.WriteLine();
Byte[] outputStringBytes = Encoding.UTF8.GetBytes(outputString);
Byte[] outputData = new Byte[1 + outputStringBytes.Length];
outputData[0] = (Byte)outputStringBytes.Length;
Array.Copy(outputStringBytes, 0, outputData, 1, outputStringBytes.Length);
return outputData;
}
}
public static partial class TcpServer
{
private sealed class ClientConnectionApm
{
private TcpListener m_server;
private TcpClient m_client;
private Stream m_stream;
private Byte[] m_inputData = new Byte[1];
private Byte m_bytesReadSoFar = 0;
public ClientConnectionApm(TcpListener server)
{
m_server = server;
m_server.BeginAcceptTcpClient(AcceptCompleted, null);
}
private void AcceptCompleted(IAsyncResult ar)
{
// Connect to this client
m_client = m_server.EndAcceptTcpClient(ar);
// Accept another client
new ClientConnectionApm(m_server);
// Start processing this client
m_stream = m_client.GetStream();
// Read 1 byte from client which contains length of additional data
m_stream.BeginRead(m_inputData, 0, 1, ReadLengthCompleted, null);
}
private void ReadLengthCompleted(IAsyncResult result)
{
// If client closed connection; abandon this client request
if (m_stream.EndRead(result) == 0) { m_client.Close(); return; }
// Start to read 'length' bytes of data from client
Int32 dataLength = m_inputData[0];
Array.Resize(ref m_inputData, 1 + dataLength);
m_stream.BeginRead(m_inputData, 1, dataLength, ReadDataCompleted, null);
}
private void ReadDataCompleted(IAsyncResult ar)
{
// Get number of bytes read from client
Int32 numBytesReadThisTime = m_stream.EndRead(ar);
// If client closed connection; abandon this client request
if (numBytesReadThisTime == 0) { m_client.Close(); return; }
// Continue to read bytes from client until all bytes are in
m_bytesReadSoFar += (Byte)numBytesReadThisTime;
if (m_bytesReadSoFar
private void WriteDataCompleted(IAsyncResult ar)
{
// After result is written to client, close the connection
m_stream.EndWrite(ar);
m_client.Close();
}
}
}
First of all: Stop using non-blocking sockets. In .NET you should either stick to the synchronous methods Receive/Send or asynchronous methods BeginReceive/BeginSend.
You should only stick with sync methods if you will have only a handful of clients. Then launch each new client in a new thread. This is the easiest option to get everthing running.
Simply do like this:
public void AcceptClients()
{
TcpListener listener = new TcpListener(IPAddress.Any, 5566);
listener.Start();
while (_serverRunning)
{
var socket = listener.AcceptSocket();
new Thread(ClientFunc).Start(socket);
}
}
public void ClientFun(object state)
{
var clientSocket = (Socket)state;
var buffer = new byte[65535];
while (_serverRunning)
{
//blocking read.
clientSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
//check packet.
// handle packet
// send respons.
clientSocket.Send(alalalal);
}
}
You should refactor the methods so that they follow SRP. The code is just a small guide to get you going.

Categories

Resources