Help - Send a File via Socket
-
I have a Client and a Sever, i connect the client to the server via Socket, now i'd like to send a file from the Client to the Server, i read about Socket.SendFile(fileName), but can't find how, from the server i have to receive the file using it (sendfile) in the client, i'd like to see an example, thank you!
-
I have a Client and a Sever, i connect the client to the server via Socket, now i'd like to send a file from the Client to the Server, i read about Socket.SendFile(fileName), but can't find how, from the server i have to receive the file using it (sendfile) in the client, i'd like to see an example, thank you!
Hy, You don't need to send a file with the Send File method. The send method send a byte array. So use FileStream to read the byte content of the file and then send it to the client/server
//read the file
FileStream fstream = File.Open(filename,FileMode.Open);
byte []buffer = new byte[fstream.Lenght];
fstream.Read(buffer,0,buffer.Length);
fstream.Close();//send it
ClientSocket.Send(buffer);It works fine on any kind of files. After you have received the file use FileStream to write the file (of course you should know the extension to put to the file). Hope it helps
Do your best to be the best
-
Hy, You don't need to send a file with the Send File method. The send method send a byte array. So use FileStream to read the byte content of the file and then send it to the client/server
//read the file
FileStream fstream = File.Open(filename,FileMode.Open);
byte []buffer = new byte[fstream.Lenght];
fstream.Read(buffer,0,buffer.Length);
fstream.Close();//send it
ClientSocket.Send(buffer);It works fine on any kind of files. After you have received the file use FileStream to write the file (of course you should know the extension to put to the file). Hope it helps
Do your best to be the best
-
I'm having problem in the Server Side; How do i receive the file? and if i need the extension of the file, i have to send it with the client? and then how do i retrieve it? Thanks else if (data.StartsWith("$UploadFile")) { }
You can create your own communication protocol. Let's say that the client sends a message to the server telling him that he will upload a file with a specified extension and size. The server sends back an acknowledge message and the the client sends the file. Concerning the server. If you know the exact size of the file then you can do something like:
byte[] buffer = new byte[size];
client.Receive(buffer);If you do not know the size of the file then you can write a sequence that reads data from the network in packets let's say 1024 long, like this:
ArrayList list = new ArrayList();
byte[] buffer = new byte[1024];
int read = 0;
int length = 0;
while ((read = client.Receive(buffer) != 0)
{
length += read;
list.Add(buffer);
buffer = new byte[1024];
if (read < 1024) // reached the end of file
break;
}At the end of this code you have inside the array list the entire file stored in 1024 length byte , arrays. After this just extract the byte arrays from the array list, like this.
buffer = new byte[length];
byte []temp = null;
int k = 0;
for (int i = 0 ; i < list.Count ; i++)
{
temp = (byte[])list[i];
for (int j = 0 ; j < temp.Length ; j++)
byte[k++] = temp[j];
}Hope it helps.
Do your best to be the best
-
You can create your own communication protocol. Let's say that the client sends a message to the server telling him that he will upload a file with a specified extension and size. The server sends back an acknowledge message and the the client sends the file. Concerning the server. If you know the exact size of the file then you can do something like:
byte[] buffer = new byte[size];
client.Receive(buffer);If you do not know the size of the file then you can write a sequence that reads data from the network in packets let's say 1024 long, like this:
ArrayList list = new ArrayList();
byte[] buffer = new byte[1024];
int read = 0;
int length = 0;
while ((read = client.Receive(buffer) != 0)
{
length += read;
list.Add(buffer);
buffer = new byte[1024];
if (read < 1024) // reached the end of file
break;
}At the end of this code you have inside the array list the entire file stored in 1024 length byte , arrays. After this just extract the byte arrays from the array list, like this.
buffer = new byte[length];
byte []temp = null;
int k = 0;
for (int i = 0 ; i < list.Count ; i++)
{
temp = (byte[])list[i];
for (int j = 0 ; j < temp.Length ; j++)
byte[k++] = temp[j];
}Hope it helps.
Do your best to be the best
Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }
-
Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }
Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }
-
Ok, here is the Code, what's wrong? //Client Side FileStream fstream = File.Open(txtPathFile.Text, FileMode.Open); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, buffer.Length); fstream.Close(); Send("$UploadFile," + NameFileUpload.Remove(0, NomeFileUpload.ToString().LastIndexOf(@"\")).Remove(0, 1) + "," + buffer.Length + "|"); s.Send(buffer); //Server Side else if (data.StartsWith("$UploadFile")) { string[] b = data.Split(','); string nameFile = b[1]; string size = b[2]; byte[] buffer = new byte[Int32.Parse(size)]; sock1.Receive(buffer); File.WriteAllBytes(@"C:\" +nameFile, buffer); }