471,344 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

C# file Transfer

Hello Everyone,

I have been looking around a lot to create a server app which can
accept multiple client connections and receive files. So far I have
combined a few examples and added lots of my own code to make it do
what I want so far, but at the moment I知 having trouble sending/
receiving an actual file. I知 just not sure on the best way to do it.
I致e provided parts of my code below. I知 just not sure the best way
to receive the file stream and re-create it in to the file again.

Any help would be greatly appreciated.

Client Code

I connect to the server using

m_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );

I can send normal text data to the server fine

Object Data = message.Text;
byte[] byData = System.Text.Encoding.ASCII.GetBytes(Data.ToString ());
m_socClient.Send (byData);

But now I want to send a file instead
m_socClient.SendFile(@"C:\NewDocument.doc");

So I do this, and it sends and the server recieves it, but displays it
as weird ASCII charactors in message received box (obviously). Is this
the best way to send a file?
The server Code

I listen on
m_socListener = new
Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

When the data is received

public void WaitForData(System.Net.Sockets.Socket soc)
{
if ( pfnWorkerCallBack == null )
{
pfnWorkerCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = soc;
// now start to listen for any data...
soc .BeginReceive (theSocPkt.dataBuffer ,
0,theSocPkt.dataBuffer.Length ,SocketFlags.None,pfnWorkerCallBack,theSocPkt);
}
public void OnDataReceived(IAsyncResult asyn)
{
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive (asyn);
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
//Writes to textbox
txtDataRx.Text += status;
WaitForData(theSockId.thisSocket);
}

So how do I modify this to receive a file propperly?
Thanks in advance
Jul 30 '08 #1
3 5079
Bonzol wrote:
I have been looking around a lot to create a server app which can
accept multiple client connections and receive files. So far I have
combined a few examples and added lots of my own code to make it do
what I want so far, but at the moment I知 having trouble sending/
receiving an actual file. I知 just not sure on the best way to do it.
I致e provided parts of my code below. I知 just not sure the best way
to receive the file stream and re-create it in to the file again.
m_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream ,ProtocolType.Tcp );

I can send normal text data to the server fine

Object Data = message.Text;
Why object and not string ?
byte[] byData = System.Text.Encoding.ASCII.GetBytes(Data.ToString ());
m_socClient.Send (byData);
Encoding.ASCII will only work with characters in US ASCII.

I doubt that is your intention.
But now I want to send a file instead
m_socClient.SendFile(@"C:\NewDocument.doc");
char[] chars = new char[iRx + 1];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
Now you assume UTF-8 ?
int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
I suggest you fix the character set stuff and if it still fails post
the correct content and how it looks like with your code.

Arne
Jul 30 '08 #2
I suggest you fix the character set stuff and if it still fails post
the correct content and how it looks like with your code.
Better yet, change from string to byte arrays so there is no character set
conversion whatsoever, if you want a faithful copy.
>
Arne

Aug 6 '08 #3
Ben Voigt [C++ MVP] wrote:
>I suggest you fix the character set stuff and if it still fails post
the correct content and how it looks like with your code.

Better yet, change from string to byte arrays so there is no character set
conversion whatsoever, if you want a faithful copy.
He is sending and receiving bytes.

But he has the data in a String, so he has to convert from
String to Bytes.

Arne
Aug 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Lingyun Yang | last post: by
11 posts views Thread by Abhishek | last post: by
11 posts views Thread by Stephan Steiner | last post: by
8 posts views Thread by Xarky | last post: by
1 post views Thread by Alex | last post: by
10 posts views Thread by David | last post: by
2 posts views Thread by tedpottel | last post: by
6 posts views Thread by Thom Little | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.