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,So cketType.Stream ,ProtocolType.T cp );
I can send normal text data to the server fine
Object Data = message.Text;
byte[] byData = System.Text.Enc oding.ASCII.Get Bytes(Data.ToSt ring ());
m_socClient.Sen d (byData);
But now I want to send a file instead
m_socClient.Sen dFile(@"C:\NewD ocument.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(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
When the data is received
public void WaitForData(Sys tem.Net.Sockets .Socket soc)
{
if ( pfnWorkerCallBa ck == null )
{
pfnWorkerCallBa ck = new AsyncCallback (OnDataReceived );
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisS ocket = soc;
// now start to listen for any data...
soc .BeginReceive (theSocPkt.data Buffer ,
0,theSocPkt.dat aBuffer.Length ,SocketFlags.No ne,pfnWorkerCal lBack,theSocPkt );
}
public void OnDataReceived( IAsyncResult asyn)
{
CSocketPacket theSockId = (CSocketPacket) asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisS ocket.EndReceiv e (asyn);
char[] chars = new char[iRx + 1];
System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
int charLen = d.GetChars(theS ockId.dataBuffe r, 0, iRx, chars, 0);
System.String szData = new System.String(c hars);
//Writes to textbox
txtDataRx.Text += status;
WaitForData(the SockId.thisSock et);
}
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,So cketType.Stream ,ProtocolType.T cp );
I can send normal text data to the server fine
Object Data = message.Text;
byte[] byData = System.Text.Enc oding.ASCII.Get Bytes(Data.ToSt ring ());
m_socClient.Sen d (byData);
But now I want to send a file instead
m_socClient.Sen dFile(@"C:\NewD ocument.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(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);
When the data is received
public void WaitForData(Sys tem.Net.Sockets .Socket soc)
{
if ( pfnWorkerCallBa ck == null )
{
pfnWorkerCallBa ck = new AsyncCallback (OnDataReceived );
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisS ocket = soc;
// now start to listen for any data...
soc .BeginReceive (theSocPkt.data Buffer ,
0,theSocPkt.dat aBuffer.Length ,SocketFlags.No ne,pfnWorkerCal lBack,theSocPkt );
}
public void OnDataReceived( IAsyncResult asyn)
{
CSocketPacket theSockId = (CSocketPacket) asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisS ocket.EndReceiv e (asyn);
char[] chars = new char[iRx + 1];
System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
int charLen = d.GetChars(theS ockId.dataBuffe r, 0, iRx, chars, 0);
System.String szData = new System.String(c hars);
//Writes to textbox
txtDataRx.Text += status;
WaitForData(the SockId.thisSock et);
}
So how do I modify this to receive a file propperly?
Thanks in advance
Comment