C# file Transfer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonzol

    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’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
  • =?windows-1252?Q?Arne_Vajh=F8j?=

    #2
    Re: C# file Transfer

    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’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,So cketType.Stream ,ProtocolType.T cp );
    >
    I can send normal text data to the server fine
    >
    Object Data = message.Text;
    Why object and not string ?
    byte[] byData = System.Text.Enc oding.ASCII.Get Bytes(Data.ToSt ring ());
    m_socClient.Sen d (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.Sen dFile(@"C:\NewD ocument.doc");
    char[] chars = new char[iRx + 1];
    System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
    Now you assume UTF-8 ?
    int charLen = d.GetChars(theS ockId.dataBuffe r, 0, iRx, chars, 0);
    System.String szData = new System.String(c hars);
    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

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: C# file Transfer

      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

      Comment

      • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

        #4
        Re: C# file Transfer

        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

        Comment

        Working...