Socket Programming in C# .Net

Dot Net Programming in C# with source code and DLL file. Covers Socket, Remoting, Stored Procedure, ASP.Net, IO, Web Service, Windows Service, Installer, Console Programming, Win Forms, Web Forms, Ajax, Web publishing, Text Chat, Large File Transfer, File Handling etc.

Download source code from here

Large File (2 GB) Transfer using C# Socket

Now I will discuss about, how we can transfer a large file (file size in GB) using Microsoft dot net socket 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 for Beginner

2. Asynchronous Socket Client for Beginner

3. File Transfer using C# Socket

4. Split and Assemble large file (around 2GB) in C# dot net


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. How it can be done, has covered in 3rd article (File Transfer using C# Socket). 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’ve 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-


DLL link is: here

http://rapidshare.com/files/269113556/File_Transfer__DLL_-_Client_and_Server.zip

Example is here in Windows forms application using these DLL is here :
http://rapidshare.com/files/269113557/File_Transfer_Win_App_using_DLL_-_Server_and_Client.zip


If you get benefit from that article then my request to specially you that donate by clicking on any advertisement to encourage me to write more code to help you

Any well come your reply on my article to here your comments.


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

Labels: , , , , ,

Large File (2 GB) Transfer using C# Socket

Now I will discuss about, how we can transfer a large file (file size in GB) using Microsoft dot net socket 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 for Beginner

2. Asynchronous Socket Client for Beginner

3. File Transfer using C# Socket

4. Split and Assemble large file (around 2GB) in C# dot net


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. How it can be done, has covered in 3rd article (File Transfer using C# Socket). 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’ve 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-


DLL link is: here

http://rapidshare.com/files/269113556/File_Transfer__DLL_-_Client_and_Server.zip

Example is here in Windows forms application using these DLL is here :
http://rapidshare.com/files/269113557/File_Transfer_Win_App_using_DLL_-_Server_and_Client.zip


If you get benefit from that article then my request to specially you that donate by clicking on any advertisement to encourage me to write more code to help you

Any well come your reply on my article to here your comments.


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

Labels: , , , , ,

  1. Blogger jilani | March 2, 2009 6:51 AM |  

    Its Really very Usefull topic I will implement it


    Thank You


    JILANI SHAIKH

  2. Anonymous Anonymous | March 25, 2009 7:22 AM |  

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

    Congrats.

  3. Blogger Suman Biswas | March 25, 2009 7:37 AM |  

    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

  4. Anonymous Muhammad Iftikhar | April 29, 2009 5:30 AM |  

    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

  5. Blogger Suman Biswas | April 29, 2009 5:40 AM |  

    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

  6. Blogger vinhshinichi | May 6, 2009 8:56 PM |  

    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 12:10 AM |  

    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

  8. Blogger venki | May 24, 2009 11:58 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 2, 2009 11:07 PM |  

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

  10. Blogger Suman Biswas | June 3, 2009 1:58 AM |  

    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

  11. Anonymous Anonymous | June 9, 2009 12:42 AM |  

    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

  12. Blogger anasanjaria | June 9, 2009 12:48 AM |  

    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

  13. Blogger Suman Biswas | June 9, 2009 1:00 AM |  

    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

  14. Blogger anasanjaria | June 10, 2009 4:47 AM |  

    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 4:49 AM |  

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

  16. Blogger Suman Biswas | June 10, 2009 5:06 AM |  

    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

  17. Anonymous nima | June 18, 2009 1:53 AM |  

    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

  18. Blogger Suman Biswas | June 18, 2009 2:04 AM |  

    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

  19. Anonymous nima | June 18, 2009 4:50 AM |  

    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

  20. Blogger Suman Biswas | June 18, 2009 6:26 AM |  

    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

  21. Anonymous nima | June 21, 2009 11:55 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,
    Nima

  22. Blogger fps | June 21, 2009 6:03 PM |  

    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 9:07 AM |  

    This post has been removed by a blog administrator.

  24. Blogger venki | July 22, 2009 8:26 PM |  

    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 7:00 AM |  

    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

  26. Blogger Suman Biswas | July 23, 2009 7:40 AM |  

    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

  27. Blogger venki | July 23, 2009 11:17 PM |  

    Hey Suman
    Its working file.
    Thank You soooooooooo much.


    Bye
    Venkat

  28. Blogger L-deathnote | September 28, 2009 11:12 AM |  

    Link die! I can't download

  29. Blogger Suman Biswas | September 29, 2009 3:50 AM |  

    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

  30. Blogger L-deathnote | September 29, 2009 7:33 AM |  

    Thank you very much

  31. Blogger Roy | October 21, 2009 1:52 AM |  

    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 10:54 AM |  

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

leave a response