473,389 Members | 1,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

Asynchronous Socket Server data

Hi,

I am writing an asychronous socket server to handle 20+ simulataneous
connections.
I have used the example in MSDN as a base. The code is shown at end of
question.

Each connection has a number of different types of data coming in. I have a
databuffer for each type of data coming in.

The socket server knows what type of data it expects due to the interface
protocol I have implemented.

The main part of this protocol is that the client sends a 5 byte message
which indicates the type of data to follow.
The client then sends the data in response to the server acking the 5 byte
message.

This data will be split up into packets. Each packet is checked and a valid
reply from the server will prompt the client
to send the next packet until the whole data has been been receiveed.

The socket server knows what type of data to expect and will therefore put
it into the appropriate sized data buffer.

My question is, rather than have 20 copies of the data buffer for datatype1,
20 copies for datatype2 etc is there anyway
I can have 1 databuffer only for each datatype to handle multiple connections?

As each instance of my app could have a different number of connections to
it, the above would stop the need for adding
or removing buffers for each instance of my app if the number of different
connections differs form instance to instance.

I'd appreciate any suggesstions,

Thanks In Advance
Macca

public sealed class AsynchronousSocketServer
{
// ReadCallback variables. Will declaring these variables here
enables multiple connections
// to use them?

static byte[] messagePacketArray = new
byte[SysConstants.MessagePacketSize];
static byte[] standbyMessageArray = new
byte[SysConstants.StandbyMessageSize];
static byte[] roiPacketArray = new byte[SysConstants.RoiPacketSize];
static byte[] diagnosticPacketArray = new
byte[SysConstants.DiagnosticPacketSize];
static byte[] roomStatusArray = new
byte[SysConstants.roomStatusPacketSize];
static byte[] readyToReceiveArray = new
byte[SysConstants.readyToReceiveSetupPacketSize];

static int packetIndex;

// Sensor Ack/Nack buffers
static byte[] ackArray = new byte[1] { SysConstants.Ack };
static byte[] nackArray = new byte[1] { SysConstants.Nack };
// Reply Message Buffers
static byte[] standbyArray = new byte[3] { SysConstants.Standby, 0,
0 };

static ushort diagnosticPacketCounter;

// Thread signal.
private static ST.ManualResetEvent allDone = new
ST.ManualResetEvent(false);

/// <summary>
/// Default Constructor
/// </summary>
private AsynchronousSocketServer()
{
}

/// <summary>
/// Listen for incoming connections from the Sensor Clients
/// </summary>
public static void StartListening()
{
// Establish the local endpoint for the socket.

string hostname = "";
SN.IPHostEntry ipHostInfo = SN.Dns.GetHostEntry(hostname);
SN.IPAddress ipAddress = ipHostInfo.AddressList[0];
SN.IPEndPoint localEndPoint = new SN.IPEndPoint(ipAddress,
SysConstants.TcpPortNumber);

// Create a TCP/IP socket.
SNS.Socket listener = new
SNS.Socket(SNS.AddressFamily.InterNetwork,
SNS.SocketType.Stream, SNS.ProtocolType.Tcp);

// Bind the socket to the local endpoint and listen for incoming
connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(SysConstants.ClientConnectionsMaxB ufferSize);

while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();

// Start an asynchronous socket to listen for connections.
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);

// Wait until a connection is made before continuing.
allDone.WaitOne();
}

}
catch (SNS.SocketException e)
{
Console.WriteLine(e.ToString());
}

}
/// <summary>
/// Callback Method that handles client connections
/// </summary>
/// <param name="ar"> The Interface that contains Socket
Information</param>
public static void AcceptCallback(IAsyncResult ar)
{
if (ar == null)
{
throw new ArgumentNullException("ar");
}

// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
SNS.Socket listener = (SNS.Socket)ar.AsyncState;
SNS.Socket handler = listener.EndAccept(ar);

// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
state.dataType = SysConstants.WaitingForCommand;
state.dataSize = SysConstants.MessagePacketSize;

handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}

/// <summary>
/// Callback Method that processes data sent over socket connection
/// by the sensor.
/// </summary>
/// <param name="ar"></param>
public static void ReadCallback(IAsyncResult ar)
{
if (ar == null)
{
throw new ArgumentNullException("ar");
}

// Determine which sensor has sent data

// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
SNS.Socket handler = state.workSocket;

// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);

if (bytesRead > 0)
{
try
{
// Check what type of data has been received
switch (state.dataType)
{
// Build message if total bytes expected not reached yet
case SysConstants.Message:
Array.Copy(state.buffer, 0, messagePacketArray,
packetIndex, bytesRead);
break;

case SysConstants.WaitingForCommand:
Array.Copy(state.buffer, 0, standbyMessageArray,
packetIndex, bytesRead);
break;

case SysConstants.RoiProcessed:
Array.Copy(state.buffer, 0, roiPacketArray,
packetIndex, bytesRead);
break;

case SysConstants.DiagnosticImage:
Array.Copy(state.buffer, 0,
diagnosticPacketArray, packetIndex, bytesRead);
break;

case SysConstants.RoomStatusData:
Array.Copy(state.buffer, 0, roomStatusArray,
packetIndex, bytesRead);
break;

case SysConstants.ReadyToReceiveSetupData:
Array.Copy(state.buffer, 0, readyToReceiveArray,
packetIndex, bytesRead);
break;

}
}
catch (System.ArgumentException e)
{
Console.WriteLine("{0} : error ", e.Message);

}
state.totalBytesRead += bytesRead; // Used to indicate when
full packet is received
packetIndex += bytesRead;
if (state.totalBytesRead == state.dataSize)
{
// Full Packet has been received
packetIndex = 0;
state.totalBytesRead = 0;
switch (state.dataType)
{
case SysConstants.WaitingForCommand:
//
// Do Something
//
break;

case SysConstants.Message:
//
// Do Something
//
break;

case SysConstants.RoiProcessed:
//
// Do Something
//
break;

case SysConstants.DiagnosticImage:
//
// Do something
//
break;

case SysConstants.RoomStatusData:
//
// Do Something
//
break;

case SysConstants.ReadyToReceiveSetupData:
//
// Do Something
//
break;
}
// Check Socket for more data
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);

}
else
{
// Full packet has not been received yet
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
else
{
}
}

private static void Send(SNS.Socket handler, byte[] byteData)
{
// Generate the CRC for data
Int16 crc = CrcGenerator.GenerateCrc(byteData, byteData.Length);

// Add CRC to end Message Packet
byteData[byteData.Length-1] = (byte)(crc >> 8);
byteData[byteData.Length-2] = (byte)crc;

// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
//handler.BeginSend(byteData, 0, 1, 0,
new AsyncCallback(SendCallback), handler);
}

private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
SNS.Socket handler = (SNS.Socket)ar.AsyncState;

// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);

//handler.Shutdown(SNS.SocketShutdown.Both);
//handler.Close();

}
catch (System.ArgumentException e)
{
Console.WriteLine(e.ToString());
}
catch (SNS.SocketException e)
{
Console.WriteLine(e.ToString());
}
}
}

/// <summary>
/// State object for reading client data asynchronously
/// </summary>
public class StateObject
{
// Client socket.
internal SNS.Socket workSocket;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
internal byte[] buffer = new byte[BufferSize];
// identify if message or data packet
//internal bool messagePacket;
internal byte dataType;

// Total Bytes read
internal Int32 totalBytesRead;
// Size of Data Packet expected
internal Int32 dataSize;
}
Apr 5 '06 #1
0 4636

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
7
by: Colin | last post by:
I'm writing a little console socket server but I'm having some difficulty. Can I ask your advice - where is the best place to get some help on that topic? It would be nice if some people who knew...
4
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. ...
4
by: Macca | last post by:
I am writing an application that uses asynchronous sockets to get data over ethernet from embedded devices, up to 30 concurrent devices.(These devices are written in C). My application...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
1
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a TCP socket. I found an example code on the MSDN...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.