How to C# Socket Programming using Visual Studio.Net

Learn step by step network programming using C#.Net socket, remoting, WCF and SignalR technology from lots of source code and description. All source code download link available at end of the blog post.

How to transfer Large File using C# Socket

Now I will discuss about, how we can transfer a large file (file size in GB) using Microsoft dot net socket programming using C# (C sharp) language. This code has written in Visual Studio 2005 (dot net 2).

Basic Knowledge
To cover this article you need to read my few articles, these are –
1. Asynchronous Socket Server Programming for Beginner
2. Asynchronous Socket Client Programming for Beginner
3. File Transfer using C# Socket Programming
4. Split and Assemble large file (around 2GB) in C# dot net Programming

The basic code has covered in these article and that article basically comes by assembled these four. To transfer a large file from Client to Server I’ve followed these basic steps-
1) First read the file (slice wise) and store a slice data in a buffer (byte array). How a file can read slice wise, it has covered in my 4th article (Split and Assemble large file (around 2GB) in C#) in file split section. It’s required because TCP buffer is limited so if we try to send a large data then we will get TCP buffer overflow error.
2) Send these buffered data to server using TCP socket object. How it can be done, has covered in 3rd article (File Transfer using C# Socket programming). And for basic idea you can read article 1 & 2.
3) In server side when we starts to receive sliced data from client then we write these data in a file. How this can be done has covered in article 4 (Split and Assemble large file (around 2GB) in ), in file assemble section.

To receive file from Server to Client same process is applicable, just then server sends data by sliced and receive by Client in same way.

How to use DLL
In my code I have made these two functionality (server and client) in two separate class library (DLL file). By calling these two libraries (DLL file) any one can transfer data very easily. To do it need to follow these steps -
Step 1: Create Object
To use these dll to transfer data you need to create one client and one server object.
For client object -
SynchronusClient_ByteArr objClient = new SynchronusClient_ByteArr ("","localhost", @"C:\Documents and Settings\Administrator\UserData");
First argument for client id, if you keep it blank string then also ok, but when you use dll for multiple client (sender) to transfer multiple file to multiple client (receiver) then it’s mandatory. Otherwise file may goes to all user or different user. Next argument for server IP, and next for a temporary back up path, it’s require at file receive time from server.
Here and one more constructor with more argument there you can assign port no etc. Here default port is 8000 and 8001.
For Server object –
SyncSocketServerMulClient servObj = new SyncSocketServerMulClient();
If you use this code then server will load it’s default configuration, like port 8000 and 8001 and it’s default path to C: Other wise you can call overloaded method.

Step 2: Method & Properties
For server and client has some method which you need to call.
Client methods and properties are –
Picture 1 Picture 2
Picture 1 for Method and events of client object and Picture 2 for method and property of client class.
Hope from pic 1 you can understand everything very easily and from pic 2 also clear from it’s name. For ‘status’, it holds current status of client like, slicing data, sending data, receiving data etc and ‘progress’ it holds current % of data transfer currently done.
Server methods and properties are –
Picture 3 Picture 4
For pic 3 hope everything clear to you. But my comments here is, when you call start server then server starts and stays active as long as you call stop server. After call start server you can’t call it again before stop server call.
From pic 4 ‘bufferSize’ is for declare byte array length, you can define it as your self, ‘maxClientReceived’ for how many client can send or receive data at a time.
By using this code I’ve developed few large application, like text chat with smiles, file transfer, picture transfer, online drawing etc. Hope you will get a great experience by that code.
Download link
You can download source code from these two links-

Server Code is here :

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ServerSockets.Synchronous.UsingByteArray

/// That server code will run to access multiple client requrest and response. Here will not any
/// Un-zip and Decript technology. Server will save all data in Encripted-Zipped form which will
/// sent by Client.

public class SyncSocketServerMulClient
public static bool isServerRunning=false;
public static int receivePort,sendPort, maxClientReceived, bufferSize;
//public static int progress=0 ;
public string outPath ;
public static string status="",presentOperation = "";
public string currentStatus = "";

public SyncSocketServerMulClient(int receivePort,int sendPort,int maxClient, string outPutPath)
SyncSocketServerMulClient.receivePort = receivePort;
SyncSocketServerMulClient.sendPort = sendPort;
SyncSocketServerMulClient.bufferSize = 10 * 1024;
SyncSocketServerMulClient.maxClientReceived = maxClient;

outPutPath = outPutPath.Replace("\\", "/");
if (outPutPath.Substring(outPutPath.Length - 1) != "/")
outPutPath += "/";

this.outPath = outPutPath;
SyncSocketServerMulClient.status = "";
//SyncSocketServerMulClient.progress = 0;
SyncSocketServerMulClient.presentOperation = "";

/// Default sets Receive Port:8080, Send Port:8081, Buffer Size:10KB, Max Client:100 and Out path: C:\

public SyncSocketServerMulClient()
SyncSocketServerMulClient.receivePort = 8080;
SyncSocketServerMulClient.sendPort = 8081;
SyncSocketServerMulClient.bufferSize = 10 * 1024;
SyncSocketServerMulClient.maxClientReceived = 100;
this.outPath = "c:/";
SyncSocketServerMulClient.status = "";
//SyncSocketServerMulClient.progress = 0;
SyncSocketServerMulClient.presentOperation = "";

Thread threadReceiveServer, threadSendServer;
private void StartReceiveServer()
SyncSocketServerMulClient.isServerRunning = true;
threadReceiveServer = new Thread(new ThreadStart(this.StartReceiveServerThread));
private void StopReceiveServer()
SyncSocketServerMulClient.isServerRunning = false;

IPAddress[] ipAddress = Dns.GetHostAddresses("localhost");
IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], SyncSocketServerMulClient.receivePort);
Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);

string strData = "END";
byte[] clientData = new byte[30];
clientData = Encoding.ASCII.GetBytes(strData);

//byte[] serverData = new byte[10];
//int len = clientSock.Receive(serverData);
//Console.WriteLine(Encoding.ASCII.GetString(serverData, 0, len));

catch (Exception ex)

Socket receiveSock,sendSock;
IPEndPoint ipEndReceive, ipEndSend;
private void StartReceiveServerThread()
ipEndReceive = new IPEndPoint(IPAddress.Any, SyncSocketServerMulClient.receivePort);
receiveSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
SyncSocketServerMulClient.isServerRunning = true;
Console.WriteLine("Waiting for new client connection");
while (SyncSocketServerMulClient.isServerRunning)
Socket clientSock;
clientSock = receiveSock.Accept();

SyncSocketServerMulClient serObj = new SyncSocketServerMulClient(SyncSocketServerMulClient.receivePort, SyncSocketServerMulClient.sendPort, SyncSocketServerMulClient.bufferSize, this.outPath);
Thread newClient = new Thread(serObj.ReadDataFromClient);

private void ReadDataFromClient(object clientObject)
Socket clientSock = null;
BinaryWriter bWriter=null;
string fileName = "";
SyncSocketServerMulClient.status = "";
SyncSocketServerMulClient.presentOperation = "";
clientSock = (Socket)clientObject;
bool flag = true;
Console.WriteLine("New connection estublished. Socket {0}", clientSock.GetHashCode());
int totalDataLen, receivedLen, fileNameLen, fileContentStartIndex;

byte[] data = new byte[bufferSize];

currentStatus =presentOperation = "Data Receiving";
int len = clientSock.Receive(data);
if (len == 0)
if (len == 3)
string clientData = Encoding.ASCII.GetString(data);
if (clientData.Substring(0, len) == "END")
totalDataLen = BitConverter.ToInt32(data, 0);
fileNameLen = BitConverter.ToInt32(data, 4);
fileName = Encoding.ASCII.GetString(data, 8, fileNameLen);
fileContentStartIndex = 4 + 4 + fileNameLen;
receivedLen = len - fileContentStartIndex;

if (File.Exists(outPath + fileName))
File.Delete(outPath + fileName);

bWriter = new BinaryWriter(File.Open(outPath + fileName, FileMode.Append));
bWriter.Write(data, 0, len);
while (true)
if (receivedLen < len =" clientSock.Receive(data);" currentstatus ="presentOperation" currentstatus =" presentOperation" clientinfodata =" new" clientinfodata =" Encoding.ASCII.GetBytes(" status = "Access Denied by server end." status = "Address Already In Use." status = "Address Not Available." status = "Connection aborted by server." status = "Connection refused by server." status = "Connection Reset." status = "Destination Address Required." status = "Disconnecting." status = "Target Host is Down." status = "Target Host Not Found." status = "Target Host Unreachable." status = "In Progress." status = "Interrupted." status = "Invalid Argument." status = "Network Down." status = "Network Reset." status = "Network Unreachable." status = "No Buffer Space Available." status = "No Data." status = "Not Connected." status = "Not Initialized." status = "Not Socket." status = "Operation Aborted." status = "Socket already Shutdown." status = "Too Many Open Sockets." status = "Too Many Open Sockets." status = "Have some unknown problem in Socket." isserverrunning =" true;" threadsendserver =" new" ipendsend =" new" sendsock =" new" isserverrunning =" true;" clientsock =" sendSock.Accept();" serobj =" new" newclient =" new" clientsocket="(Socket)clientObject;" filenamewithpath = "" breader =" null;" status = "" clientdata =" new" clientdatalen =" clientSocket.Receive(clientData);" clientdatalen ="=" clientdatalen ="=" clientdatastr =" Encoding.ASCII.GetString(clientData);" clientidlen =" BitConverter.ToInt32(clientData," filenamelen=" BitConverter.ToInt32(clientData," clientid =" Encoding.ASCII.GetString(clientData," filename =" Encoding.ASCII.GetString(clientData," copiedfilename =" clientId" filenamewithpath =" this.outPath;" filenamewithpath =" fileNameWithPath.Replace(" length ="=" currentstatus =" presentOperation" breader =" new" data =" new" sentdatasize =" (int)bReader.BaseStream.Length;" data =" new" sentdatasize =" bufferSize;" totalsentdataslot =" 1;" totaldatasize =" (int)bReader.BaseStream.Length;" progress =" (int)(((float)sentDataSize" progress =" (int)(((float)sentDataSize" enddata =" new" sentdatasize =" (int)bReader.BaseStream.Length;" progress =" (int)(((float)sentDataSize" currentstatus =" presentOperation" strfleerr = "ERROR" tempfleerrarr =" Encoding.ASCII.GetBytes(strFleErr);" currentstatus =" presentOperation" status = "Access Denied by server end." status = "Address Already In Use." status = "Address Not Available." status = "Connection aborted by server." status = "Connection refused by server." status = "Connection Reset." status = "Destination Address Required." status = "Disconnecting." status = "Target Host is Down." status = "Target Host Not Found." status = "Target Host Unreachable." status = "In Progress." status = "Interrupted." status = "Invalid Argument." status = "Network Down." status = "Network Reset." status = "Network Unreachable." status = "No Buffer Space Available." status = "No Data." status = "Not Connected." status = "Not Initialized." status = "Not Socket." status = "Operation Aborted." status = "Socket already Shutdown." status = "Too Many Open Sockets." status = "Too Many Open Sockets." status = "Have some unknown problem in Socket." status =" ex.Message;" isserverrunning =" false;" ipaddress =" Dns.GetHostAddresses(" ipend =" new" clientsock =" new" strdata = "END" clientdata =" new" clientdata =" Encoding.ASCII.GetBytes(strData);" serverdata =" new" len =" clientSock.Receive(serverData);" style="font-weight: bold; color: rgb(51, 51, 255);" size="4">Client Code is here:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace ClientSockets.Synchronous.UsingByteArray // SyncScoketClient_ByteArr_Dll

/// Client open a syncronous socket and send file to server, here asynchronous mode becomes by
/// Threading, and here uses Byte Array to transfer data.

public class SynchronusClient_ByteArr
public delegate void FileSendCompletedDelegate();
public event FileSendCompletedDelegate FileSendCompleted, FileReceiveCompleted;
public static int progress = 0, portSend, portReceive;
int bufferSize;
public static string status = "", presentOperation = "", ipAddress, outPathDefault = "";
private static string outPathUserSelected = "";
string clientId = "";
string fileNameWithPath;
public static bool isSaveToDefaultPath = false;
static int totalSentDataSlot;

public SynchronusClient_ByteArr(string clientId, string ipAddress, int sendPort, int receivePort, string DefaultPath)
this.clientId = clientId;
SynchronusClient_ByteArr.ipAddress = ipAddress;
SynchronusClient_ByteArr.portSend = sendPort;
SynchronusClient_ByteArr.portReceive = receivePort;
SynchronusClient_ByteArr.outPathDefault = DefaultPath;
this.bufferSize = 10 * 1024;

/// Conestructor of Client Object

public SynchronusClient_ByteArr(string clientId, string ipAddress, string DefaultPath)
this.clientId = clientId;
outPathDefault = DefaultPath;
SynchronusClient_ByteArr.ipAddress = ipAddress;
SynchronusClient_ByteArr.portSend = 8080;
SynchronusClient_ByteArr.portReceive = 8081;
this.bufferSize = 10 * 1024;
Thread startFileTransferThread, startFileReceiveThread;
public string SendFileToServer( string senderId, string receiverId)

string newFileName, onlyFileName, path;

//Select a File to transfer
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() != DialogResult.OK)
return "Cancelled by user";
string fileNameWithPath = ofd.FileName;
fileNameWithPath = fileNameWithPath.Replace("\\", "/");
onlyFileName = fileNameWithPath;
path = "";

while (onlyFileName.IndexOf("/") != -1)
path += onlyFileName.Substring(0, onlyFileName.IndexOf("/")) + "/";
onlyFileName = onlyFileName.Substring(onlyFileName.IndexOf("/") + 1);
newFileName = senderId + "#" + onlyFileName;
//File.Copy(fileNameWithPath, path + "/" + newFileName,true);
//File.SetAttributes(path + "/" + newFileName, FileAttributes.Hidden);

string bothName = fileNameWithPath + "?" + path + "/" + newFileName;
startFileTransferThread = new Thread(this.SendFileToServerByThread);

//Only file name
int k = newFileName.Length - 1;
while (newFileName.Substring(k, 1) != ".")
newFileName = newFileName.Substring(0, k);
newFileName = newFileName + ".sjb";

return newFileName;
private void SendFileToServerByThread(object fileNameWithPathObj)
Socket server = null;
BinaryReader bReader = null;
string newFileName = "";
string oldFileName, bothFileName;
bothFileName = fileNameWithPathObj.ToString();
oldFileName = bothFileName.Substring(0, bothFileName.IndexOf("?"));
newFileName = bothFileName.Substring(bothFileName.IndexOf("?") + 1);

string filePath = "", tempFileName = oldFileName;
while (tempFileName.IndexOf("/") != -1)
filePath += tempFileName.Substring(0, tempFileName.IndexOf("/") + 1);
tempFileName = tempFileName.Substring(tempFileName.IndexOf("/") + 1);
if (File.Exists(newFileName))

File.Copy(oldFileName, newFileName, true);
File.SetAttributes(newFileName, FileAttributes.Hidden);

status = "";
this.fileNameWithPath = newFileName;
//presentOperation = "Data Compressing";
//this.fileNameWithPath = ZipUnzip.LZO_Algorithm.LZO.Compress(this.fileNameWithPath);
//File.SetAttributes(filePath + this.fileNameWithPath, FileAttributes.Hidden);

if (this.fileNameWithPath.Length == 0)

this.fileNameWithPath = filePath + tempFileName;// this.fileNameWithPath;
presentOperation = "Data Sending";
IPAddress[] serverIp = Dns.GetHostAddresses(SynchronusClient_ByteArr.ipAddress);
IPEndPoint ipEnd = new IPEndPoint(serverIp[0], SynchronusClient_ByteArr.portSend);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);


int sentDataSize;
byte[] data;// = new byte[bufferSize];

bReader = new BinaryReader(File.Open(fileNameWithPath, FileMode.Open));
if (bReader.BaseStream.Length <= bufferSize) { data = new byte[bReader.BaseStream.Length]; bReader.Read(data, 0, (int)bReader.BaseStream.Length); sentDataSize = (int)bReader.BaseStream.Length; } else { data = new byte[bufferSize]; bReader.Read(data, 0, bufferSize); sentDataSize = bufferSize; } string onlyFileName; fileNameWithPath = fileNameWithPath.Replace("\\", "/"); onlyFileName = fileNameWithPath; while (onlyFileName.IndexOf("/") != -1) onlyFileName = onlyFileName.Substring(onlyFileName.IndexOf("/") + 1); //DATA FORMAT: [FILE SIZE LEN INFO[0-3]][FILE NAME LEN INFO[4-7]][FILE NAME DATA][FILE CONTENT] byte[] dataLenBytes = BitConverter.GetBytes((int)bReader.BaseStream.Length); byte[] fileNameLenBytes = BitConverter.GetBytes(onlyFileName.Length); byte[] fileNameBytes = Encoding.ASCII.GetBytes(onlyFileName); byte[] allData = new byte[8 + fileNameBytes.Length + data.Length]; //MAKE FIRST PACKET TO SERVER WITH FILE SIZE & FILE NAME dataLenBytes.CopyTo(allData, 0); fileNameLenBytes.CopyTo(allData, 4); fileNameBytes.CopyTo(allData, 8); data.CopyTo(allData, 8 + fileNameBytes.Length); //SEND FIRST PACKET TO SERVER server.Send(allData); totalSentDataSlot = 1; int totalDataSize = (int)bReader.BaseStream.Length; progress = (int)(((float)sentDataSize / totalDataSize) * 100); while (sentDataSize < progress =" (int)(((float)sentDataSize" enddata =" new" sentdatasize =" (int)bReader.BaseStream.Length;" progress =" (int)(((float)sentDataSize" receivedata =" new" recvlen =" server.Receive(receiveData);" presentoperation = "Data Sending Completed" status = "Access Denied by server end." status = "Address Already In Use." status = "Address Not Available." status = "Connection aborted by server." status = "Connection refused by server." status = "Connection Reset." status = "Destination Address Required." status = "Disconnecting." status = "Target Host is Down." status = "Target Host Not Found." status = "Target Host Unreachable." status = "In Progress." status = "Interrupted." status = "Invalid Argument." status = "Network Down." status = "Network Reset." status = "Network Unreachable." status = "No Buffer Space Available." status = "No Data." status = "Not Connected." status = "Not Initialized." status = "Not Socket." status = "Operation Aborted." status = "Socket already Shutdown." status = "Too Many Open Sockets." status = "Too Many Open Sockets." status = "Have some unknown problem in Socket." status =" ex.Message;" fbd =" new" outpathuserselected =" fbd.SelectedPath;" startfilereceivethread =" new" outpathuserselected =" ReceivePath;" startfilereceivethread =" new" server =" null;" bwriter =" null;" rcvfilename = "" filename = "" status = "" onlyfilename =" onlyFileNameObj.ToString();" presentoperation = "Data Compressing" length ="=" clientid =" DateTime.Now.Ticks.ToString();" clientidlenbytes =" BitConverter.GetBytes(clientId.Length);" clientidbytes =" Encoding.ASCII.GetBytes(clientId);" filenamelenbytes =" BitConverter.GetBytes(onlyFileName.Length);" filenamebytes =" Encoding.ASCII.GetBytes(onlyFileName);" alldata =" new" presentoperation = "Data Sending" serverip =" Dns.GetHostAddresses(SynchronusClient_ByteArr.ipAddress);" ipend =" new" server =" new" status = "" progress =" 0;" presentoperation = "" server =" (Socket)clientObject;" flag =" true;" data =" new" presentoperation = "Data Receiving" len =" server.Receive(data);" len ="=" strtemp =" Encoding.ASCII.GetString(data," strtemp ="=" status = "File yet not sent by Sender" presentoperation = "Data Receiving Not Started" progress =" 0;" totaldatalen =" BitConverter.ToInt32(data," filenamelen =" BitConverter.ToInt32(data," filename =" Encoding.ASCII.GetString(data," filecontentstartindex =" 4" receivedlen =" len;//" outpathdefault =" outPathDefault.Replace(" outpathuserselected =" outPathUserSelected.Replace(" fbd =" new" bwriter =" new" bwriter =" new" bwriter =" new" progress =" (int)(((float)receivedLen" len =" server.Receive(data);" val =" (float)receivedLen" progress =" (int)(((float)receivedLen" presentoperation = "Data Decompressing" rcvfilename =" ZipUnzip.LZO_Algorithm.LZO.Decompress(outPathDefault" rcvfilename =" ZipUnzip.LZO_Algorithm.LZO.Decompress(outPathUserSelected" orgfilename =" rcvFileName=" orgfilename =" orgFileName.Substring(orgFileName.IndexOf(" isserverrunning =" false;" presentoperation = "Data Receiving Completed" progress =" 100;" status = "Access Denied by server end." status = "Address Already In Use." status = "Address Not Available." status = "Connection aborted by server." status = "Connection refused by server." status = "Connection Reset." status = "Destination Address Required." status = "Disconnecting." status = "Target Host is Down." status = "Target Host Not Found." status = "Target Host Unreachable." status = "In Progress." status = "Interrupted." status = "Invalid Argument." status = "Network Down." status = "Network Reset." status = "Network Unreachable." status = "No Buffer Space Available." status = "No Data." status = "Not Connected." status = "Not Initialized." status = "Not Socket." status = "Operation Aborted." status = "Socket already Shutdown." status = "Too Many Open Sockets." status = "Too Many Open Sockets." status = "Have some unknown problem in Socket." status =" ex.Message;" style="">

Labels: , , , , , , , , ,

  1. Blogger jilani | March 2, 2009 at 8:21 PM |  

    Its Really very Usefull topic I will implement it

    Thank You


  2. Anonymous Anonymous | March 25, 2009 at 7:52 PM |  

    It's huge information to the socket programming and keep on the future also.


  3. Blogger Suman Biswas | March 25, 2009 at 8:07 PM |  

    Hiving here one chat session with last visitor, I will happy it would helps to other.

    RRaveen: hi
    RRaveen: S-Specific.M-Measureable.A-Achievable.R-Realistic.T-Time-bounded
    Suman: hi
    Sent at 2:03 PM on Wednesday
    RRaveen: hi maa
    how are you?
    Suman: yes fine...and u?
    RRaveen: first i would like to thank you
    i seem ur blog ur GURU in socket
    Suman: then I'm not maaman....i'm suman
    RRaveen: :)
    Suman: no...i know a little bit of socket but not guru at all
    RRaveen: ok
    Suman: then tell about your problem
    RRaveen: now i'm working in a project
    with socket to connect to yahoo server
    for i need create 65K client connections
    Suman: ok
    RRaveen: when i create and transfer data between server and client
    it fine
    until 10K connection
    when i try create 10001 client connection i'm getting buffer error and probs.
    i think my way of creation and handling not good
    then i try find solution in google, that time i got u like god
    r u tamil?
    Suman: first i have not worked with so many client so i can't give u practical idea...but i can help you by some concept
    no i'm bengali
    RRaveen: ok
    i'm sure you know better than me
    Suman: have you mention about max client no. is more than 10k?
    RRaveen: becuase of i look
    we can create 65K connection
    Suman: like code will be
    RRaveen: that;s server , yahoo will handle that
    we need create only clients
    Suman: oh!
    which thread u r using?
    RRaveen: now i'm thinking go to Async modeling
    Suman: which comes by .net it self?
    RRaveen: .net
    Suman: it's a drag and drop tool right?....vb or c# no matter
    RRaveen: internally
    i created not dragdrop
    Suman: for any thread .net keeps a thread pool like track
    i can't remember exact name
    it's can customized ... but quite problematic
    the pool can handle by default 25 thread
    RRaveen: i didn;t use any threadbool .
    now i give my steps
    1. we have yahoo users
    Suman: thrad pool no need to used by u
    it's handle by .net
    but it's limit 25
    RRaveen: 2. every user we need create a socketclient to connect server and transfer data
    Suman: so there have a chance to come problem from there
    RRaveen: 3. afted
    created all clients
    we need handle every socketclient data yahoo server sent to our client
    4. then do the processing and send again
    5.this the our goal operation
    say now we are created 10K connections and send authentication details to server
    now server return data to client
    now we need handle that data, here i'm using looping.
    i hope it not good idea to receive data by looping
    what is the good idea?
    Suman: for 10k user need 10k socket object at your machine by looping is it making 10k different object?
    RRaveen: ok course
    you are absolutelly correct
    Suman: see what happen actually between server and client
    first client requesting to connect to server by a socket
    server accepts it and create a socket for that client socket requiest
    and these two socket woks like two end of pipe
    by these two end data transffered like water transferred in pipe
    so for 10k user 10k like pipe created
    and transffed data parralley
    here in server side and client side opens atleast 10k thrad
    at yahoo side like 10k socket created by 10k thread for your 10k request
    and at your machine 10k same thing created
    here problem may comes from 1 ways
    1. you are using background worker which is a thread
    RRaveen: ohh really, go ahead i have question i will ask when u finished.
    Suman: it's limit is 25...which is microsoft site i have seen that one user was facing problem for around 4k user
    it may for u 10l
    so need to change this
    and need to built it by ray thread
    at my article see there has 2gb file transfer code there have used raw thared
    it's has no;s unlimited
    point 2
    problem from yahoo
    because y r trying to connect 10k times from same ip
    whihc yahoo can detect
    and can stop connecting more than 10k
    it may yahoo policy
    if it is then y nothing have to do
    RRaveen: ok
    Suman: if it exactly 10000 connecting is ok but 10001 is problem then much chance to yahoo
    RRaveen: ok
    Suman: but if it varies like 9999 some thimes 10000 or may 10001 thne problem from thread
    is it ok?
    RRaveen: soem times 10001 some times 12002
    just for example i said that position
    Suman: then i'm sure it from thread
    update your code
    use raw thread
    it's a small code
    Thread threadobject =new thread(object)
    RRaveen: and more when we are create thread for 100K connection
    it not moemory issues?
    i can't download ur code
    Suman: if your computer have 1gb ram then no problem i think..if less then also ok as i think
    RRaveen: 2gB
    Suman: but it may take much time to execute but problem/error will not occur
    yes you can download my code
    RRaveen: what ever async model for this?
    it giving error ur link suman
    Suman: goto my blog and visit 2gb file transfer code
    giving link
    Download source code from here
    Suman: then pls give a response in my blog... i will post whole chat at there
    RRaveen: when i clcik that
    it going wrong place
    can u send code my email
    Suman: see below of that article there has link
    for windows app with dll
    and 2
    RRaveen: hi do you have paypal account?
    Suman: for dll
    RRaveen: ok
    Suman: give a refponse with your name i want to put this chat session there
    it will help other
    RRaveen: sure
    how i can ?
    do that
    Suman: give a response and let me know
    RRaveen: sure
    can u do favour?
    Suman: go to bottom will get leave a resoponse link
    RRaveen: if i send my code can u modify?
    i will give some momey man, if you dont mind?
    Suman: it's not possible to me because i'm now in office
    RRaveen: not now
    Suman: no i don;t need money
    RRaveen: just
    Suman: i'm not helping u for money
    RRaveen: for friendship man
    oh sorry just for friendship
    Suman: then money not required try your self u will learn it
    RRaveen: ok
    Suman: if got any problem i will help u by some raw threading code
    RRaveen: i have download that code, but i can't see the code for your dll
    Suman: unzip u will get it
    i'm going to some other work try if you problem reply me...and leave a response soon
    RRaveen: no suman i got ur sample project
    code , but there is no code for the that dll
    ok ok
    thank you so much
    Suman: donload both everything is there
    RRaveen: ok
    thank you
    Suman: remember for you response
    RRaveen: sure of course
    thank you so much again i try then i will ask more more questiuon
    Sent at 2:48 PM on Wednesday

  4. Anonymous Muhammad Iftikhar | April 29, 2009 at 6:00 PM |  

    helo, thanks for your superb code. but can you please explain how to use the class in the c# form. becuase i am new in this technology. your help will be much appriated.

  5. Blogger Suman Biswas | April 29, 2009 at 6:10 PM |  

    Hi Muhammad ,
    Goto that link, here you will find a complete example about your requirement. Hope it will work for you fine.


  6. Blogger vinhshinichi | May 7, 2009 at 9:26 AM |  

    Hi Suman. The first i would like to say Thanks for your Post about send file socket.I am writing send file by socket. I have a problem. I want to load server's directories to list view of client but i don't know how to send server's directories to client. Can you help me. Thank you so much.

  7. Blogger Suman Biswas | May 7, 2009 at 12:40 PM |  

    Hi Vinhshinichi,
    You can send this by few number of ways. One most easy and common way is - make a CSV list and send it (in byte mode) to client by socket data transfer. Then from these data you can reconstruct your directories.
    Hope this will work.

  8. Blogger venki | May 25, 2009 at 12:28 PM |  

    Hi its realy usefull.
    i need some help from u .
    the code what u have written in client for receiving.
    can u use same code in server.
    My requirement is needs to transfer file to all clients.

    How can i do that ....
    needs help from u ...its urgent.

  9. Blogger Adeel | June 3, 2009 at 11:37 AM |  


    Thank you very much for sharing your code. I am using it. I found a bug in your code. It won't allow you to transfer exe file, i mean i does allow but the files get corrupted. Infact every transfer is corrupted only becuase you are writing extra information in file. The file header that you send to specify the filename and file lenght, you write that as when in file, which is not required. Becuase of that, the exe files cannt be exeecuted but other files are strong enough ;) to ignore that problem.

    In your receive file method, I did the following changes to make it work.

    byte[] data_NO_headers = new byte[bufferSize];
    for (int i=fileContentStartIndex,j=0;i< data.Length;i++,j++)
    data_NO_headers[j] = data[i];

    bWriter = new BinaryWriter(File.Open(outPath + fileName, FileMode.Append));
    bWriter.Write(data_NO_headers, 0, receivedLen);

    thanks heaps for sharing this code...

  10. Blogger Suman Biswas | June 3, 2009 at 2:28 PM |  

    Hi Adeel,
    Thanks for your reply.
    Same type of error reported by Muhammad, and I give solution him by email, but for laziness I've not updated it. Now I'm pasting all mail communication below.


  11. Anonymous Anonymous | June 9, 2009 at 1:12 PM |  

    I' ve run ur file which i got from ur gvn link .

    when i run this prog it give me error & error is
    a project with an output type of class library cannot be started directly ..

    i need ur help

  12. Blogger anasanjaria | June 9, 2009 at 1:18 PM |  

    I' ve run ur file which i got from ur gvn link .

    when i run this prog it give me error & error is
    a project with an output type of class library cannot be started directly ..

    i need ur help

  13. Blogger Suman Biswas | June 9, 2009 at 1:30 PM |  

    Hi Anasanjaria,
    Probably u r trying to run DLL file. U can't run it directly. U need to call it from a .exe file or u need to download from different link. That is .exe file by using that dll file.
    Try it again.

  14. Blogger anasanjaria | June 10, 2009 at 5:17 PM |  

    THANX 4 UR PREVIOUS REP .... how can i be add dll file in project

    i want to do file sharing in client/server application over LAn .. can u suggest me ... how can i be able to do it...

  15. Blogger anasanjaria | June 10, 2009 at 5:19 PM |  

    can u send me ur email so that i can contact u ...

  16. Blogger Suman Biswas | June 10, 2009 at 5:36 PM |  

    Hi Anasanjaria,
    For guide line to use DLL in c#, go to google, u will find a lot of document.
    For 2nd my email,
    plz describe about your self, like student, professional developer etc. etc. with your exact requirement.


  17. Anonymous nima | June 18, 2009 at 2:23 PM |  

    Hi Suman Biswas,

    At the first i say thanks you for this use full blog.

    I wrote a socket program to send a file from server to client.
    But when i want send a file from server and click send,, form be come hang.
    Can i send my application to you and you found my mistake pls?


  18. Blogger Suman Biswas | June 18, 2009 at 2:34 PM |  

    Hi Nima,
    Thanks for your interest about my blog.
    First of all we can't request to a client from server, server only gives reply to client's call. And for that your program is being hang.
    Your need to follow just few steps to send something from server to client.
    1. Try to connect from client to server.
    2. Server accept it and start a new socket connection, it will use to pass data in two way between server and client.
    3. Send data from server to client by that opened socket.

    If you follow it, you will able to send data from server to client.
    My code has same type of data transfer, to inform client about last received slice no. You may follow it.


  19. Anonymous nima | June 18, 2009 at 5:20 PM |  

    Dear Suman,

    Thanks alot for your soon reply.

    I studied before your program in this link:

    My program is like yours, both want send a file,of course your program sends to server and i want send to client.
    Before, in server side i did steps that you said in your post.
    At the first, server does listening,

    Then client connect,
    And then server accept (with new socket)

    After server accept, i seletc a file and click send, but server's form hang and dosen't work.

    I attached my program as below:
    I will grateful if you can help me.

    Thanks again,

  20. Blogger Suman Biswas | June 18, 2009 at 6:56 PM |  

    Hi Nima,
    I think there has some problem in server coding while accepting client request.
    Here after button click you have accepted client request, but there has no client request.
    See in general coding style client-server data transfer server wait in 'listen mode' for client request while it comes server 'accept' it. But here has not followed it. So it's not working properly. First make a code to just communicate server and client then enhance that code as your requirement. For basic code you may follow my article about beginner's socket programming etc.

    Hope if follow that way then you will get success.

    Good luck for your coding!!!


  21. Anonymous nima | June 22, 2009 at 12:25 AM |  

    Hi Suman,

    Can you help me pls, how i can open a audio file by Media Player?
    How i must call Media Player in my program?

    Thanks a lot,

  22. Blogger fps | June 22, 2009 at 6:33 AM |  

    please check out the file when transfer completed, it is not the origin file, I tried to send a simple text file from client to server, when complete you open the file and its contents has been modified by add some byte to the origin file, this will make the file broken. Please check out when you have free time guy.

  23. Anonymous Anonymous | July 3, 2009 at 9:37 PM |  

    This comment has been removed by a blog administrator.

  24. Blogger venki | July 23, 2009 at 8:56 AM |  

    hi ........i need some help form u.
    i want to create folder in server based in client(s) ip address.
    How can i do that ......

    its urgent

  25. Blogger venki | July 23, 2009 at 7:30 PM |  

    i found one bug in ur progtam.
    file is succesfully transferd to server. but the problem is once we open the file in had some additional txt.
    like in file staring its writing filename the

    Can u help me how to resolve this
    this artical realy super.......

  26. Blogger Suman Biswas | July 23, 2009 at 8:10 PM |  

    Hi All,
    In my code have a small but, that is when some file transferred then it modified with some extra text. To solve it you can follow below mail with "muhammad iftikhar", who found this bug first.

    Suman Biswas to muhammad May 21 2008

    There was a bug. Now it has solved!!!

    To update your code follow below line

    Find "bWriter.Write(data, 0, len )" in Server code and replace it with -

    "bWriter.Write(data, fileContentStartIndex, len - fileContentStartIndex);"

    It will work fine. Test with your same test.txt file and 2GB file. Plz
    let me know your test result.

    Thanks again,

    muhammad iftikhar to me May 21 2008

    Ohhhhhhhhhhhhh Great Suman!
    It's worked fine. Once the application complete then i will send accross.

    Thank you so much for your help.

    Brilient! Cheers

    Muhammad Iftikhar

  27. Blogger venki | July 24, 2009 at 11:47 AM |  

    Hey Suman
    Its working file.
    Thank You soooooooooo much.


  28. Blogger L-deathnote | September 28, 2009 at 11:42 PM |  

    Link die! I can't download

  29. Blogger Suman Biswas | September 29, 2009 at 4:20 PM |  

    Have you tried with following link, this link is available at the below of article.


  30. Blogger L-deathnote | September 29, 2009 at 8:03 PM |  

    Thank you very much

  31. Blogger Roy | October 21, 2009 at 2:22 PM |  

    Hi Suman Biswas.

    It was studying all your Socket Programming.

    I have a problem getting the file from the server.

    when it come to small type of file theirs no problem but in the larger file the file is corrupted or the data inside the file is not complete,

    Can you help me.

    BTW I'm Roy from the Philippines.

  32. Blogger Kevin | October 29, 2009 at 11:24 PM |  

    link die, can't find dll source...
    can't be found....

  33. Blogger UkraineTrain | November 24, 2009 at 12:34 AM |  

    The links provided are dead. Can you, please, give valid links?

  34. Blogger Suman Biswas | November 24, 2009 at 5:47 PM |  

    Thanks for pointing that.
    I have updated the link. Now you may try to download.

    Thanks again,

  35. Blogger TARUN | January 18, 2010 at 3:04 PM |  

    Hi Suman Biswas.

    It was studying all your Socket Programming.

    I have a problem getting the file from the server.

    when it come to small type of file theirs no problem but in the larger file the file is corrupted or the data inside the file is not complete,

    Can you help me.

  36. Blogger Suman Biswas | February 1, 2010 at 2:51 PM |  

    Hi Tarun,
    Please read my new article (SEND FILE FROM SERVER TO CLIENT USING C# SOCKET) for your answer.


  37. Blogger zoheir | April 7, 2010 at 1:36 PM |  

    It is very useful,Thank you for response to other's comments thank you for your attention to other and thank you for every thing.

  38. Blogger Suman Biswas | April 7, 2010 at 2:20 PM |  

    If you think it is really helpful and I have invest my time to do such thing which has helped you. Then my request is please help me, by donate some amount.

    To donate no need to pay money from your pocket, just click on any add and watch this for few moments, I will get money for your time and it will help me a lot.


  39. Blogger quickview | August 26, 2010 at 2:20 PM |  

    Dear Suman ! The first i would like to say thanks for your Post about send file socket.
    Can you help me ?
    Problem : When I complete receive file from client, I can not open it.

  40. Blogger quickview | August 26, 2010 at 3:54 PM |  

    Dear Suman !
    the bug at line : bWriter.Write(data, 0, len ) in Server code which i fixed by replace bWriter.Write(data, fileContentStartIndex, len - fileContentStartIndex). But when i send large image file (>4Mb)then it can not work fine(result file is missing some byte)
    Please help me. Thanks and Regards Suman !

  41. Blogger Next com | November 19, 2010 at 9:49 AM |  

    hi suman thanks for ur article thats really help me.

    hm.. may i ask u... how i send the file and save it into database?

    thank u

  42. Blogger Chandan......The Smell for God | November 30, 2010 at 5:30 PM |  

    hi Dear,
    i need this's very much urgent for me.i tried with these two rapidshare links but it is showing ERROR...File not found.
    Can u please load it again..?

  43. Blogger Suman Biswas | November 30, 2010 at 6:26 PM |  

    Hi Chandan,
    Where have you got the link of rapid share. I see it is link and it is working, just now I have chacked it.
    Try once again,


  44. Blogger abd | February 21, 2011 at 7:55 PM |  

    hi is there any specific method or function to ask another user(connection request) while in LAN...thank u

  45. Blogger sowa | February 23, 2011 at 8:54 PM |  

    Links are not working

  46. Blogger Suman Biswas | March 1, 2011 at 8:34 PM |  

    Hi Sowa,
    The upload site presently is down so the links are not working presently. However when upload site will up please try then once again.


  47. Blogger Adriano | March 19, 2011 at 12:36 AM |  

    Hello Friend,

    Can you send me a complete code ?
    Server and Client Console system.
    I'm trying to do some similar, but I havind some problems with buffer trash ...


  48. Blogger sap | March 25, 2011 at 10:28 AM |  

    Hi Suman,
    Great Article this is exactly what i needed to assist my development of a server client program.
    I am happy to support you and click some links. You are providing excellent assistance.

    I have one question are you able to upload the file again? the links are dead, i know this is form a few years ago but i hope you are able to re upload it

  49. Blogger KILUA | April 30, 2011 at 7:02 AM |  

    Hello Mr. Suman, i've trying re-code your application to make more understanding the your app to send? but it didn't work as well as yours. Is there any secret code or something i don't know ??thanks.

  50. Blogger Suman Biswas | May 18, 2011 at 10:19 PM |  

    Hi All,
    I have paste a new url please try it.


  51. Blogger Your Blog | July 4, 2011 at 12:14 PM |  

    Hello All,

    I am new to socket programming. The task is to receive data from some sources and stream it continuously. The whole file need not to be streamed. The source will be xml file and xml file will contain objects. I need to fetch objects from xml file selected by the user and need to stream it.

    I want transfer rate to be very high.

    Your approach to socket pro. is awesome. Please suggest me something.

    Akki J

  52. Blogger Binod | October 9, 2011 at 7:24 AM |  

    hi i can't download dll zip file please upload it again.

    thank you

  53. Blogger Suman Biswas | October 11, 2011 at 3:29 AM |  

    Hi Binod,
    Try with rapid share link, you will get it at the below of article. It is working

  54. Blogger Matias Ringa | October 13, 2011 at 5:55 AM |  

    Hello, Suman Biswas!

    I am a programming student from Argentina, the information of your blog on programming is the best I found on the network.

    I have a bug with the program, when I send a file from the client Sample.txt to the server, the file disappears completely from the pc.

    download link, thanks.

  55. Blogger Matias Ringa | October 13, 2011 at 5:55 AM |  

    Hello, Suman Biswas!

    I am a programming student from Argentina, the information of your blog on programming is the best I found on the network.

    I have a bug with the program, when I send a file from the client Sample.txt to the server, the file disappears completely from the pc.

    download link, thanks.

  56. Blogger spider.blass | December 16, 2011 at 6:49 AM |  

    hi suman....
    i'am new in C#, can you explain, what the meaning :
    1. Private objClient As New SynchronusClient_ByteArr("", "localhost", "C:\Documents and Settings\Administrator\UserData")

    2. objClient.SendFileToServer("", "")

    can i change ? and what should I fill ?

    sample :
    Client IP :
    Server IP :
    file client to send server : C:\me.jpg

    i'am very very sorry for my stupid question

  57. Blogger Atiqur Rahman | September 18, 2012 at 3:24 PM |  

    Dear Mr, Suman Biswas, thanks for writing this wonderful socket program and post it to help us. But I am facing a problem when I try to run server code at line no 104 inside “SyncSocketServerMulClient
    ” class. (receiveSock.Bind(ipEndReceive);) the error message is “Only one usage of each socket address (protocol/network address/port) is normally permitted”. Sorry for this type of question. But a bit newer in programming. So could you please help me in this matter???

  58. Blogger Suman Biswas | September 18, 2012 at 3:28 PM |  

    Hi Atik,
    It seems you are not getting this error message first time when you start running the application. From error it seems to me there is running one background application when you are trying to run next one.
    Check in task manager and kill this process so from next you will not get similar error.

    Thanks for reading my blog and expecting some clicks on advertisement and like on facebook button;


  59. Blogger Atiqur Rahman | September 19, 2012 at 12:51 PM |  

    Dear Mr. Suman Biswas,
    Thanks you for your response, the code is working fine to me. But I have to bother you a bit further. I have another question to you. If I put this server code inside a web server then how can I authenticate it? Actually I am trying to build software like “DropBox” but facing thousands of problems. Could you please help me regarding this matter??? Sorry for bothering you again.


  60. Blogger Suman Biswas | September 19, 2012 at 6:49 PM |  

    Hi Atik,
    This is desktop type application, probably this can not run from web server for shared hosting. No company will provide you to run EXE file from there and if any company provides it then it is a threat for your website (including all website in this server). However dedicated webserver can run it.

    Authentication need to perform as normal way this socket will help just to transfer data between client and server. Remaining all job you need to do normally.

    It seems you are new in programming specially in socket programming so it will be difficult to you now. Before doing all of these you need learn this technology clearly.

    Try more you will learn.


  61. Blogger Atiqur Rahman | September 28, 2012 at 11:51 AM |  

    Dear Suman Biswas,
    Sorry for bothering you again. But I need your help. I need to vary the receive path for the server. Suppose two or more users(User A, User B, User C) is trying to send the file at server, then I need the receive the files at server at in different location. When the thread for user A starts then I need to set this.outpath as “C:/A” and When the thread for user B starts then I need to set this.outpath as “C:/B” and so on. Could you please help me in this matter? Again very sorry for disturbing you.

  62. Blogger Suman Biswas | September 28, 2012 at 3:43 PM |  

    Hi Atiqur,
    I can help you by providing algorithm of these code but not actual code. You can try to develop this by following below steps:
    1. When request sending from client server then send client name with initial data. For your example send A,B,C etc.

    2. In server side accept and receive these data and get client name from there.

    3. Before saving file data using StreamWritter first create folder with clinet name and set streamwritter path accordingly.

    If you follow these three steps you can do the code.
    But my personal recombination is do not create folder for each client because data in server generally temporary but if you create so much folder then it will be a burden to server and performance will get effect for that. Better to use temporary file name.


  63. Blogger Fikri | May 28, 2013 at 2:21 PM |  

    Hai all

    dear suman biswas, thanks for writing wonderfull c# code up there
    but im so sorry, your download link in rapidshare broke again
    could you reupload it?
    or anyone who had succesfull runing this program please send me an email at

    i want to learn more about c#
    anyone please help me :(

  64. Blogger Ravindra | August 28, 2013 at 1:51 PM |  

    there is no file at rapidshare

  65. Blogger Ravindra | August 28, 2013 at 1:53 PM |  

    There is no link available at RapidShare Please send me it at

  66. Blogger Unknown | November 27, 2013 at 2:03 PM |  

    This comment has been removed by the author.

  67. Blogger Unknown | November 27, 2013 at 2:05 PM |  

    This comment has been removed by the author.

  68. Blogger Unknown | November 27, 2013 at 2:07 PM |  

    Same here~
    Thanks in advance..

  69. Blogger luke Griffiths | February 10, 2014 at 10:50 PM |  

    hi the file isn't in the link provided can you please send it on

  70. Blogger Unknown | February 27, 2014 at 1:58 PM |  

    please send me it at

  71. Blogger Unknown | February 27, 2014 at 1:59 PM |  

    This comment has been removed by the author.

  72. Blogger Unknown | February 27, 2014 at 2:01 PM |  

    This comment has been removed by the author.

  73. Blogger marinaldo pereira ramos | June 10, 2014 at 4:31 PM |  

    There is no link available at RapidShare Please send me it at

  74. Blogger Coding4fun | June 12, 2014 at 3:22 PM |  

    File available at below link

  75. Blogger Nurul Fredyani Tanaya | September 22, 2014 at 2:00 PM |  

    usefull topic, but no link available, can you send me at

    Thank you

  76. Blogger Perrin Allison Ganal | October 18, 2014 at 2:05 PM |  

    Hi, Can I please have a copy of the code?

    my email is:

    Thank you!

  77. Blogger hafizuddin said | November 27, 2014 at 9:17 AM |  

    There is no link available at RapidShare Please send me it at

    thank you so much~

  78. Blogger BeerFizz | May 5, 2015 at 11:38 PM |  

    Please send me the code at:

    phil.h.davis @

    Thank you


  79. Blogger Cary Reno | August 11, 2015 at 6:32 PM |  

    This looks very well put together and promising. Thank you for such a large effort.

    Can you please forward me the source code at Thanks again for such a big help on a technology that is not heavily pushed any longer.

    Best regards, Cary

  80. Blogger Ben Babu | August 13, 2015 at 9:48 AM |  

    Hey good work but rapid share has removed or something. Plz re upload it. Recently got into a proj to create chat room need help with file transfer.
    Plz send me a copy to :

    thnx in advance

  81. Blogger maria khalid | December 10, 2015 at 6:58 PM |  

    This comment has been removed by the author.

  82. Blogger maria khalid | December 10, 2015 at 6:59 PM |  

    Hi suman what is datasending speed per second and can we increase sending speed

  83. Blogger Anand Singh | February 2, 2016 at 4:26 PM |  

    Tecnostore-Group is a highly-secured way to Send Big Files Online Fast. We’re using highly secured and encryption method, which are unbreakable by any one. You can send up to 50 GB at once.

leave a response