Send File from Server to Client using C# Socket Programming 1/6

Hello Friends, after long day’s gap I am writing some blog for you. During this time many things has changed in technological world like earlier I write code with VS 2008 and now VS 2012 has released. Also lots of changes in our life too. I believe this time I can write my blog with better English than earlier, which will help you to read this. Ok lets read this post about to learn new thing which is about to send small file from server to client. This is just opposite of sending file from client to server. This is the basic of large file transfer, later I shall use the same thing to send large file from server to client and finally will use both (client to server and server to client) large file transfer code client to client. I shall come to that point step by step.

To send file from server to client there must be two applications that is Server application and client application. In code I have mentioned these two parts individually. In below section I am describing Server action means server application is working and you need to check server code, for client action need to check client code. Handshaking of these two socket programming applications should be following:

1) Server Action: First need to run server application, this server application will open an endpoint with predefined IP address and port number and will remain in listen mode to accept new socket connection request from client.

It’s just like some one is waiting at some fixed position to reply on some ones request.
This below section of code from server application is doing exactly same thing:
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 5656);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

sock.Bind(ipEnd);

sock.Listen(100);
Here line no 1 is creating an ipEnd point with port number 5656 and IP is local machine IP address. Port number can be anything except well known port number (port number should be more than 1024).
Next two lines are creating a socket object and binding with previously created IPEnd point.
Last line is sending newly created socket object to listen mode to accept new connection request from client.
So you need to run server application first and then client application.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill the blank please: *