Documente Academic
Documente Profesional
Documente Cultură
.NET 2.0
By SumanBiswas, 26 Feb 2009
Rate this: vote 1vote 2vote 3vote 4vote 5
inShare 0
Object 1
Object 3
Object 4
Object 5
{
IPEndPoint ipEnd;
Socket sock;
public FTServerCode()
{
ipEnd = new IPEndPoint(IPAddress.Any, 5656);
//Make IP end point to accept any IP address with port no 5656.
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.IP);
//Here creating new socket object with protocol type and transfer data
type
sock.Bind(ipEnd);
//Bind end point with newly created socket.
}
public static string receivedPath;
public static string curMsg = "Stopped";
public void StartServer()
{
try
{
curMsg = "Starting...";
sock.Listen(100);
/* That socket object can handle maximum 100 client connection at a
time &
waiting for new client connection /
curMsg = "Running and waiting to receive file.";
Socket clientSock = sock.Accept();
/* When request comes from client that accept it and return
new socket object for handle that client. */
byte[] clientData = new byte[1024 * 5000];
int receivedBytesLen = clientSock.Receive(clientData);
curMsg = "Receiving data...";
int fileNameLen = BitConverter.ToInt32(clientData, 0);
/* I've sent byte array data from client in that format like
[file name length in byte][file name] [file data], so need to know
first how long the file name is. /
string fileName = Encoding.ASCII.GetString(clientData, 4,
fileNameLen);
/* Read file name */
BinaryWriter bWrite = new BinaryWriter(File.Open
}
catch (Exception ex)
{
curMsg = "File Receiving error.";
}
}
}
{
filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
fileName = fileName.Substring(fileName.IndexOf("/") + 1);
}
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);
if (fileNameByte.Length > 850 * 1024)
{
curMsg = "File size is more than 850kb, please try with small
file.";
return;
}
curMsg = "Buffering ...";
byte[] fileData = File.ReadAllBytes(filePath + fileName);
/* Read & store file byte data in byte array. */
byte[] clientData = new byte[4 + fileNameByte.Length +
fileData.Length];
/* clientData will store complete bytes which will store file name
length,
file name & file data. */
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
/* File name lengths binary data. */
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
/* copy these bytes to a variable with format line [file name length]
[file name] [ file content] */
curMsg = "Connection to server ...";
clientSock.Connect(ipEnd);
/* Trying to connection with server. /
curMsg = "File sending...";
clientSock.Send(clientData);
/* Now connection established, send client data to server. */
curMsg = "Disconnecting...";
clientSock.Close();
/* Data send complete now close socket. */
curMsg = "File transferred.";
}
catch (Exception ex)
{
I hope you understand how a file can be sent from client to server via TCP socket in C#. Here I've write
simple code to send a single file, but its the basic code. By modifying that code, multiple files can be
sent from client to server, and by incorporating thread technology, that server can handle multiple
clients at a time. And by using both end binary writer & reader can send large files too. Here, we will
give a small problem for TCP buffer overflow that needs to send data after slicing. To know more on
Socket programming, please visit my blog at http://socketprogramming.blogspot.com/.