Click here to Skip to main content
15,883,603 members
Articles / Programming Languages / C#
Article

Sending Binary Data using Sockets

Rate me:
Please Sign up or sign in to vote.
3.12/5 (18 votes)
25 Jan 20051 min read 80.8K   1.1K   52   7
An article on file transfer using sockets.

Screenshot

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:

C#
private INPClient _client;

// in the initialisation
_client = new INPClient("127.0.0.1",11000);

// to send a file do
_client.SendFile("FILENAME");

Creating a server to receive a file, is quite simple.

C#
private INPServer _server;

// in the initialisation

_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);

// and in the body of the code you chould implement thes functions aswell eg:

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() +".";
}

// to start the server
_server.Start();

// to stop the server
_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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General[Message Deleted] Pin
it.ragester2-Apr-09 21:50
it.ragester2-Apr-09 21:50 
QuestionAt server side how to fetch the file name [modified] Pin
amit801210-Aug-08 19:57
amit801210-Aug-08 19:57 
QuestionSome clarification please! Pin
Jim Weiler3-Jul-07 7:35
Jim Weiler3-Jul-07 7:35 
Questionhow can i control the saving of the sended file in server side? Pin
fcis200722-Dec-06 6:44
fcis200722-Dec-06 6:44 
AnswerRe: how can i control the saving of the sended file in server side? Pin
Fouad Mahmoud22-Dec-06 15:02
Fouad Mahmoud22-Dec-06 15:02 
GeneralGreat idea, but needs work Pin
Will Gray1-Jun-05 7:14
Will Gray1-Jun-05 7:14 
GeneralDo write an article on protocol implemetation Pin
David Nissimoff26-Jan-05 11:31
David Nissimoff26-Jan-05 11:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.