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