File Transfer using C# .Net Socket Programming 1/3

File transfer using C# socket is Client-Server combined process and quite complex too. This process we can divide in three phases.

  • Preparation stage for Server and Client.
  • Establish communication channel to send-receive data between Client and Server.
  • Break the communication channel and release all resources.

Phase 1, Preparation: In that phase server and client both prepared to send some data.

At Server: Server makes an object of IPEndPoint class with some Port number with some IP address. Then creates a socket object with Internetwork family with protocol type IP(or IDP or else) to transfer Stream type data. At last bind this socket objects with that IPEndPoint object and place socket in listen mode to accept client request.
At Client: Client makes another IPEndPoint object with Server IP address and same port number. Then creates a socket object as same as Server socket object in same way. After completing these client reads a file and stores bytes data in a byte array object.
Phase 2, Establish communication and transfer data:Now client socket tries to connect to server socket which was in listen mode. When server socket get requests from client then it accept request and established connection between server and client by producing a new socket object. Next data transmission operation continues by that new socket object.
When new client socket request accepted by server and connection established then client send byte data by “socket-object. Send()” method to server and at server end these data received by “socket-object. Receive()” method. When data successfully received at server end, server saves these bytes in a file using byte data stream.
For large file, “these data read at client -send to server-receive at server end – save received data at server end” these steps are perform repeatedly until client reach at EOF (End Of File). But in this program only small file covered for this no need of looping, for this type of example read my “Send 2GB file using TCP socket programming” article.