469,638 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,638 developers. It's quick & easy.

Asynchronous Socket Server Advice

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 what
they were doing could take a look at my code and tell me where and why
I'm going wrong.

Any suggestions of groups or forums?
Nov 18 '05 #1
7 2128
I'll just post some code here in case anyone fancies a look:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections;
using System.Diagnostics;
// State object for reading client data asynchronously
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
// the group this socket belongs to
public int group;
}
public class AsynchronousSocketListener
{
public static int currentGroup = 0;
public static ArrayList connectedClients = new ArrayList();
public static ArrayList connectedClientGroups = new ArrayList();
public static string grp = string.Empty;
// Incoming data from client.
public static string data = null;

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

public AsynchronousSocketListener()
{
}

public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];

// Establish the local endpoint for the socket.
// That's the computer running the server btw
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

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

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

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

// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
// (method, object)
listener.BeginAccept(new AsyncCallback(AcceptCallback),
listener);

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

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

Console.WriteLine("\nPress ENTER to continue...");
Console.Read();

}

public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();

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

// Add this newly connected client socket to our arraylist

// Create the state object - CUSTOM OBJECT!
StateObject state = new StateObject();
state.workSocket = handler;

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

// this is basically constantly called
public static void ReadCallback(IAsyncResult ar)
{

Console.WriteLine("ReadCallBack");
String content = String.Empty;

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

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

if (bytesRead > 0)
{

// There might be more data, so store the data received so
far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer,0,bytesRead));

// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
state.sb.Remove(0,state.sb.Length);
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket. \n Data
: {1}",
content.Length, content );

// Echo the data back to the connected clients.
// send the group which this client belongs to - that's
the only people that need to see this data
sendToClients(state.group.ToString(), content);
}
// initial string will be the group this client is attached to
// this will be the first to be called.
else if (content.IndexOf("<GRP>") > -1)
{
state.sb.Remove(0,state.sb.Length);
Debug.WriteLine("Received group " +
content.Substring(0,1));
state.group = Convert.ToUInt16(content.Substring(0,1));
connectedClients.Add(state);

handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
else
{
Console.WriteLine("received something");
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}

// Iterate through the connectedClients arraylist and send
// the data to clients who are in the right group
private static void sendToClients(string grp, string data)
{

try
{
foreach(StateObject s in connectedClients)
{
//StateObject so = (StateObject)connectedClients[i];
Debug.WriteLine("SendToClients: foreaching - " + grp +
" / " + s.group.ToString());
if(s.group.ToString() == grp)
{
Debug.WriteLine("IF");

//StateObject state = new StateObject();
//state.workSocket = s.workSocket;

byte[] byteData = Encoding.ASCII.GetBytes("test");

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

Debug.WriteLine("end if");
}
}
}
catch (Exception myExc)
{
Debug.WriteLine(myExc.Message);
}
}

private static void SendCallback(IAsyncResult ar)
{
Debug.WriteLine("SendCallBack called.");
try
{
StateObject state = (StateObject) ar.AsyncState;
Socket handler = state.workSocket;

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

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

byte[] byteData = Encoding.ASCII.GetBytes("");

handler.BeginReceive(byteData, 0, byteData.Length, 0,
new AsyncCallback(ReadCallback), state);

Debug.WriteLine("Called ReadCallBack after send");

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

public static int Main(String[] args)
{
StartListening();
return 0;
}
}

I know this is a lot of code, but I'm hoping someone could wade through
there and see if they can help me. Based on the MSDN example for an
asynchronous socket server, I've tried to make the following app:

* users connect to the socket server in groups, specified when they send
an initial "n<GRP>" string, with n being the number of the group.
* when a user sends a string (ending in "<EOF>") to the server, it will
be bounced back to the other users in their group, no-one else.

It should be quite straightforward but I am just out of my depth. Please
help!
Nov 18 '05 #2
If you are not stuck to async - there is a TCP Server demo here that you can
easily build on:

http://www.atozed.com/indy/Demos/Indy10.iwp

Zip Code server
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 18 '05 #3
Chad Z. Hower aka Kudzu wrote:
If you are not stuck to async - there is a TCP Server demo here that you can
easily build on:

http://www.atozed.com/indy/Demos/Indy10.iwp

Zip Code server
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Thanks. Part of the problem is that I don't know enough about this. I
just know what I want to create but not the underlying tech I need. So -
I don't know if I'm stuck to async, it's just what I stumbled upon.

I'll take a look at the demo, thanks.
Nov 18 '05 #4
Colin Ramsay <co*********@blueyonder.co.uk> wrote in
news:58*************@news-binary.blueyonder.co.uk:
Thanks. Part of the problem is that I don't know enough about this. I
just know what I want to create but not the underlying tech I need. So -
I don't know if I'm stuck to async, it's just what I stumbled upon.

I'll take a look at the demo, thanks.


Sync sockets are a LOT easier to work with. If you have any questions on the
demo let me know here.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
Nov 18 '05 #5
This is a very cool socket demo:
http://www.mentalis.org/soft/projects/proxy/

--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.

"Colin" <co*********@blueyonder.co.uk> wrote in message
news:3b**************************@posting.google.c om...
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 what
they were doing could take a look at my code and tell me where and why
I'm going wrong.

Any suggestions of groups or forums?

Nov 18 '05 #6
Eric Lawrence [MSFT] wrote:
This is a very cool socket demo:
http://www.mentalis.org/soft/projects/proxy/


Reading through it now. Seems quite extensive. Wish I could find
something to guide me through this subject, but I will have to make do.

Thanks!
Nov 18 '05 #7
Feel free to chat with me off thread. Fiddler (www.fiddlertool.com) was
written in C# and makes extensive use of Sockets, so I may be able to help.

-Eric

"Colin Ramsay" <co*********@blueyonder.co.uk> wrote in message
news:lW****************@news-binary.blueyonder.co.uk...
Eric Lawrence [MSFT] wrote:
This is a very cool socket demo:
http://www.mentalis.org/soft/projects/proxy/


Reading through it now. Seems quite extensive. Wish I could find
something to guide me through this subject, but I will have to make do.

Thanks!

Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Michael Lindsey | last post: by
4 posts views Thread by Macca | last post: by
1 post views Thread by Macca | last post: by
reply views Thread by Macca | last post: by
4 posts views Thread by Engineerik | last post: by
2 posts views Thread by Nicolas Le Gland | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.