C# file Transfer

B

Bonzol

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’m having trouble sending/
receiving an actual file. I’m just not sure on the best way to do it.
I’ve provided parts of my code below. I’m 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.Stream,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
 
A

Arne Vajhøj

Bonzol said:
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’m having trouble sending/
receiving an actual file. I’m just not sure on the best way to do it.
I’ve provided parts of my code below. I’m 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
 
B

Ben Voigt [C++ MVP]

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

Arne Vajhøj

Ben said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top