Home Home Archives About red box
Username:
Password:
 
rssPopular Content

Filed Under Programming
digg Network Socket Programming
visits: 113 | score: 0 
posted by sysrpl on Monday May 1, 2006 4:20 AM

Years ago I wrote a C# socket class to make socket programming easier. I believe I got the programming model perfect on the first attempt. Here is a small zip file of core files that contain ListenerSocket and ClientSocket classes in the Modeler.Net namespace.

Both objects support async sockets, and use framework's the built in thread pool.

Use as follows:

Create the socket passing the default send and receive buffer size and option async notifier interface (if using winforms, the form class supports this interface)..

ClientSocket s = new ClientSocket(102400, this);

Set your host, port, and event handlers (you can optionally set an error event handler).

s.HostName = "www.google.com";
s.Port = 80;
s.Receive += new SocketReceiveEventHandler(ClientReceive);
s.StateChange += new SocketStateChangeEventHandler(ClientStateChange);

Call connect and write handler code.

s.Connect();
...

public void ClientReceive(object sender,
    SocketReceiveEventArgs e)
{
    edit.Text += e.Text;
}

public void ClientStateChange(object sender,
  SocketStateChangeEventArgs e)
{
    switch (e.CurrentState)
    {
        case SocketState.Connecting:
            this.Text = "Connecting";
            break;
        case SocketState.Connected:
            edit.Text = "";
            this.Text = "Connected";
            s.Send("GET / HTTP/1.1" + Environment.NewLine +
                "Host: www.google.com" + Environment.NewLine +
                "Connection: close" + Environment.NewLine +
                Environment.NewLine);
            break;
        case SocketState.Disconnected:
            this.Text = "Diconnected";
            break;
        case SocketState.Resolving:
            this.Text = "Resolving";
            break;
        default:
            break;
    }
}

print send topic Rate this article  

Title:

image link indent align right align middle align left quote underline bold code quote
Comment:

page generated in 0.125 seconds | last modified 1/10/2017 3:48 AM
none  none