Saturday, February 26, 2011

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).

Source Code
https://drive.google.com/file/d/0Bwd4GP6MNsEgXy1ZZmxFdVFfS0U/view?usp=sharing


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

Steps
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 C#.net ), 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-
http://alap.me/blog/All_Source_Codes.rar


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 = "";

}
#region SERVER SYNCHRONOUS SOCKET DATA RECEIVE
Thread threadReceiveServer, threadSendServer;
private void StartReceiveServer()
{
SyncSocketServerMulClient.isServerRunning = true;
threadReceiveServer = new Thread(new ThreadStart(this.StartReceiveServerThread));
threadReceiveServer.Start();
}
private void StopReceiveServer()
{
SyncSocketServerMulClient.isServerRunning = false;
//
try
{

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

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

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

Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
//
}

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);
receiveSock.Bind(ipEndReceive);
SyncSocketServerMulClient.isServerRunning = true;
receiveSock.Listen(maxClientReceived);
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);
newClient.Start(clientSock);
}
receiveSock.Close();

}
private void ReadDataFromClient(object clientObject)
{
Socket clientSock = null;
BinaryWriter bWriter=null;
string fileName = "";
try
{
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];
//DATA FORMAT: [FILE SIZE LEN INFO[0-3]][FILE NAME LEN INFO[4-7]][FILE NAME DATA][FILE CONTENT]



//GET FILE NAME, SIZE ETC.
currentStatus =presentOperation = "Data Receiving";
int len = clientSock.Receive(data);
if (len == 0)
{
clientSock.Close();
return;
}
if (len == 3)
{
string clientData = Encoding.ASCII.GetString(data);
if (clientData.Substring(0, len) == "END")
return;
}
totalDataLen = BitConverter.ToInt32(data, 0);
fileNameLen = BitConverter.ToInt32(data, 4);
fileName = Encoding.ASCII.GetString(data, 8, fileNameLen);
fileContentStartIndex = 4 + 4 + fileNameLen;
receivedLen = len - fileContentStartIndex;
//READ DATA & STORE OF FIRST PACKET

//DELETE IF FILE ALREADY EXIST
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);
startFileTransferThread.Start(bothName);

//NEW FILE NAME IS NOW IN IT'S OWN EXTENSION NEED TO CHANGE .sjb FORM
//Only file name
int k = newFileName.Length - 1;
while (newFileName.Substring(k, 1) != ".")
{
k--;
}
newFileName = newFileName.Substring(0, k);
newFileName = newFileName + ".sjb";

return newFileName;
}
private void SendFileToServerByThread(object fileNameWithPathObj)
{
Socket server = null;
BinaryReader bReader = null;
string newFileName = "";
try
{
//BUILD A COPY OF SENDING FILE
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.Delete(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)
return;

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

server.Connect(ipEnd);

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="">

87 comments:

JILANI said...

Its Really very Usefull topic I will implement it


Thank You


JILANI SHAIKH

Anonymous said...

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

Congrats.

Suman Biswas said...

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
man
how are you?
Suman: yes fine...and u?
RRaveen: first i would like to thank you
fine
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
sock.Listen(65000)?
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
BackgroundThread
Suman: which comes by .net it self?
RRaveen: .net
vb.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 limited...in microsoft site i have seen that one user was facing problem for around 4k user
it may for u 10l
10k
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 limit..it;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
like
Thread threadobject =new thread(object)
then threadobject.open()
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
RRaveen:
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
?
please
Suman: see below of that article there has link
1.http://rapidshare.com/files/202775661/File_Transfer_Win_App_using_DLL_-_Server_and_Client.zip
for windows app with dll
and 2 http://rapidshare.com/files/202778243/File_Transfer__DLL_-_Client_and_Server.zip
RRaveen: hi do you have paypal account?
Suman: for dll
yes
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
and
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
tonight
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
bye
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

Muhammad Iftikhar said...

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.
Thanks
Muhammad
muhammad_iftikhar@hotmail.com

Suman Biswas said...

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

http://rapidshare.com/files/202775661/File_Transfer_Win_App_using_DLL_-_Server_and_Client.zip


Thanks,
Suman

vinhshinichi said...

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.

Suman Biswas said...

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.
Regards,
Suman

venki said...

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.

Adeel said...

Hi,

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...

Suman Biswas said...

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.

-Suman

Anonymous said...

I' ve run ur file which i got from ur gvn link .
http://rapidshare.com/files/202778243/File_Transfer__DLL_-_Client_and_Server.zip

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

anasanjaria said...

I' ve run ur file which i got from ur gvn link .
http://rapidshare.com/files/202778243/File_Transfer__DLL_-_Client_and_Server.zip

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

Suman Biswas said...

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.
Thanks,
Suman

anasanjaria said...

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...

anasanjaria said...

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

Suman Biswas said...

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

-Suman

nima said...

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?

Thanks,
Nima

Suman Biswas said...

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.

-Suman

nima said...

Dear Suman,

Thanks alot for your soon reply.

I studied before your program in this link: http://www.codeproject.com/KB/cs/SocketApplication.aspx

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:
http://atn.parsaspace.com/Transfer%20a%20file.rar
I will grateful if you can help me.

Thanks again,
Nima

Suman Biswas said...

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!!!

Suman

nima said...

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,
Nima

Unknown said...

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.

Anonymous said...
This comment has been removed by a blog administrator.
venki said...

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

venki said...

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

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

Suman Biswas said...

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

Thanks,
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,
Suman



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

venki said...

Hey Suman
Its working file.
Thank You soooooooooo much.


Bye
Venkat

Mẹ bỉm sữa said...

Link die! I can't download

Suman Biswas said...

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

http://rapidshare.com/files/269113557/File_Transfer_Win_App_using_DLL_-_Server_and_Client.zip

Suman

Mẹ bỉm sữa said...

Thank you very much

Unknown said...

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.

Kevin said...

link die, can't find dll source...
http://rapidshare.com/files/202778243/File_Transfer__DLL_-_Client_and_Server.zip
can't be found....

UkraineTrain said...

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

Suman Biswas said...

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

Thanks again,
Suman

Tarun Varshney said...

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.

Suman Biswas said...

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

Thanks,
Suman

Unknown said...

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.

Suman Biswas said...

Hi,
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.

Thanks,
Suman

quick view 123 said...

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.
Why?

quick view 123 said...

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 !

Next com said...

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

Chandan......The Smell for God said...

hi Dear,
i need this file.it'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..?

Suman Biswas said...

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

Thanks
Suman

Unknown said...

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

sowa said...

Links are not working

Suman Biswas said...

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.

Regards,
Suman

Adriano said...

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 ...

Tks,
Adriano.

Unknown said...

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

Unknown said...

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.

Suman Biswas said...

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

Thanks
Suman

Your Blog said...

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

Binod said...

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

thank you
binod

Suman Biswas said...

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

Unknown said...

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.

https://rapidshare.com/files/2051755716/File_Transfer_using_C__Socket.zip

download link, thanks.

Unknown said...

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.

https://rapidshare.com/files/2051755716/File_Transfer_using_C__Socket.zip

download link, thanks.

spider.blass said...

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 : 10.10.10.1
Server IP : 20.20.20.2
file client to send server : C:\me.jpg


i'am very very sorry for my stupid question

Unknown said...

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???
Atik
Atik2k@yahoo.com
atik3k@gmail.com

Suman Biswas said...

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;


Regards
Suman

Unknown said...

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.

Thanks,
ATIK
Atik2k@yahoo.com
Atik3k@gmail.com

Suman Biswas said...

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.

Regards
Suman

Unknown said...

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.
Thanks,
ATIK
Atik2k@yahoo.com
Atik3k@gmail.com

Suman Biswas said...

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.

Thanks
Suman

Fikri said...

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
v_key_dallaz@yahoo.co.id

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

Ravindra said...

there is no file at rapidshare

Ravindra said...

There is no link available at RapidShare Please send me it at ravindrasingh.vit@gmail.com

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

Same here~
rn5347@gmail.com
Thanks in advance..

Unknown said...

hi the file isn't in the link provided can you please send it on lukes-world@hotmail.com

Unknown said...

please send me it at hoanghai1812@gmail.com
Thanks

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

There is no link available at RapidShare Please send me it at marinaldopr@gmail.com

Suman Biswas said...

File available at below link

Nurul Fredyani Tanaya said...

usefull topic, but no link available, can you send me at fredy.tube@gmail.com

Thank you

Unknown said...

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

my email is: perrin.ganal@gmail.com

Thank you!

Sung Jin Woo said...

There is no link available at RapidShare Please send me it at apis_ho@yahoo.com.my

thank you so much~

BeerFizz said...

Please send me the code at:

phil.h.davis @ gmail.com

Thank you

Phil

Unknown said...

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

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

Best regards, Cary

Unknown said...

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 : benigasb@gmail.com

thnx in advance
ben

Unknown said...
This comment has been removed by the author.
Unknown said...

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

Unknown said...

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.

Unknown said...

Please send me the code at:

trinhngoctung1303@gmail.com

Thank you!

Unknown said...

pls send me : trinhngoctung1303@gmail.com
Thank you !

Unknown said...

pls send me: trinhngoctung1303@gmail.com
thank you!

Unknown said...

hi , pls send me : trinhngoctung1303@gmail.com
thank you!