This project is read-only.


Apr 13, 2010 at 7:15 PM

Ok... I tried to use the NetSockets in a project and now I have several questions.


  1. If I was to make seperate client and server applications (unlike your example) in the server app how could I tell how many clients were connected and could I itterate through them to get say the IP address of each.
  2. When the OnClientConnected event is raised, how do I find out what client is connected?
  3. When the server receives data from a client, how does the server know what the data is?
  4. How do I use the Accepted and/or rejected properties and or events?
  5. How can I Reject a client?


I'm sure I've got more questions.... I just can't think of them at the momemt.

Apr 13, 2010 at 8:22 PM

Thanks for your interest in NetSockets,

Please download the latest changeset now available in "Source Code". I have made some small improvements that should make client enumeration easier, and server events more "logical".

The general client-server interaction can be described as follows:

  1. A client connects to the server
  2. The server creates the client stream
  3. The server raises the OnClientConnected event.
  4. The server decides whether to accept the client based on the client count and "Reject" property in the OnClientConnected event arg.
  5. The server either raises OnClientAccepted or OnClientRejected based on the above decision.

I hope the below explanations and examples answer your questions:

  1. The server class contains a property named "ClientCount" that tells you how many clients are connected. I have made it easier for iterating through clients in the latest changeset. The code snippet below shows how to enumerate all the client streams, and get the client's endpoint.
    foreach (Guid guid in server.Clients)
         string ip = server.ClientStreams[guid].EndPoint.ToString();

  2. I have improved the events in the server in the latest changeset. When the OnClientConnected event is raised, the client's guid is available in the event args:
    void server_OnClientConnected(object sender, NetClientConnectedEventArgs e)
        var client = server.ClientStreams[e.Guid];

  3. When data is received, the data is available in the event args:
    void client_OnReceived(object sender, NetReceivedEventArgs<NetObject> e)
        var data = e.Data;
    void server_OnReceived(object sender, NetClientReceivedEventArgs<NetObject> e) { var data = e.Data; }

  4. The Accepted and Rejected events are raised when a client is accepted or rejected. Clients are automatically accepted unless rejected in the OnClientConnected event.
    void server_OnClientConnected(object sender, NetClientConnectedEventArgs e)
        e.Reject = true;

  5. See above.
Apr 13, 2010 at 11:34 PM

Thank you!

Apr 25, 2010 at 8:16 PM

I must be a little slow this week. 

I'm having problems sending text from the client to the server.

I get that the data to be send is a byte array, but I don't see an easy way to get my text to the byte array to send.

Also on the other end, once I receive the data how to I get it to a useable text string?


Apr 26, 2010 at 8:38 PM
Edited Apr 26, 2010 at 8:48 PM


String str = "some string";
byte[] bytes = System.Text.Encoding.Default.GetBytes(str)

byte[] bytes;
String str = System.Text.Encoding.Default.GetString(bytes);

PS: I just checked in helper client/server classes for sending/receiving strings easily. Check out NetStringClient/Server.

Apr 27, 2010 at 8:03 PM
Edited Apr 27, 2010 at 8:03 PM

I like it.

On a different but similar issue.  In the NetBox app, I added the following code to the server_OnClientConnected

e.Reject = true;

and I got the following error:

Cannot access a disposed object.

I try again and the error comes up:

The stream does not support reading.