Introduction
I'll demonstrate and provide some very basic classes that can be used as a base for some client/server applications.
Background
When searching the internet for some code that can send files over the net, I was disappointed. All I could find where some chat like programs and very few articles on file transfer. Well, here's my attempt to change that.
Using the code
I created very basic classes which are very similar to the ones you can find on the net, but mine are more elegant (at least I think so). I created some events which make the usage of these classes very easy and straightforward.
To create a client, just create it like any other object:
private INPClient _client;
_client = new INPClient("127.0.0.1",11000);
_client.SendFile("FILENAME");
Creating a server to receive a file, is quite simple.
private INPServer _server;
_server = new INPServer("127.0.0.1",11000);
_server.ClientConnected+=new NetworkEventHandler(_server_ClientConnected);
_server.ClientDisConnected +=
new NetworkEventHandler(_server_ClientDisConnected);
_server.DataRecieved+=new NetworkEventHandler(_server_DataRecieved);
_server.RecieveCompleted+=new EventHandler(_server_RecieveCompleted);
private void _server_ClientConnected(object sender, NetworkEventArgs ne)
{
txtData.Text+= ne.Info.Socket.RemoteEndPoint.ToString()
+ " connected.\r\n";
}
private void _server_ClientDisConnected(object sender, NetworkEventArgs ne)
{
txtData.Text+= ne.Info.Socket.RemoteEndPoint.ToString()
+ " disconnected.\r\n";
}
private void _server_DataRecieved(object sender, NetworkEventArgs ne)
{
txtData.Text += "\r\nRECIEVING DATA FROM "
+ ne.Info.Socket.RemoteEndPoint.ToString() +".";
}
private void _server_RecieveCompleted(object sender, EventArgs e)
{
txtData.Text += "\r\nRECIEVING DATA COMPLETED FROM "
+ ne.Info.Socket.RemoteEndPoint.ToString() +".";
}
_server.Start();
_server.Stop();
If you want to use this code to create a server, you should inherit from the serverbase
class and implement the protocol in a new class. The INPServer
is a sample of this.
When creating software nowadays, it's not so important if you can do it, it is how you do it that is important. When solving a problem, it is important to not just find "a solution", it is important to find "the right solution".
Points of Interest
I would like comment on this article since it is my first one but I would like to post some more. I am an expert on component / control development. Maybe there are some cool things you'd like to see in an article. Please let me know if you find spelling errors. Please post about them or let me know until the buy.
History
I created this project in January 2005. If I find the time, I might add a second article on protocol implementation.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.