File sending problem in chat server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • click2ranjan
    New Member
    • Mar 2008
    • 3

    File sending problem in chat server

    Hi friend i am new in network programming and trying to develop a chat messenger. for this most of the portion has completed but still i have some problem with file sending to anothe user,
    files like .zip,.jpeg ,though the file is transfer through socket and well accepted by the user ,but i cant understand where is the mistake i have done , only text file i can transfer to another user,or it also readable ,even word file i cant read.
    if any one have the solution of this problem please help me....


    Thank you in advance...
    Ranjan
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by click2ranjan
    Hi friend i am new in network programming and trying to develop a chat messenger. for this most of the portion has completed but still i have some problem with file sending to anothe user,
    files like .zip,.jpeg ,though the file is transfer through socket and well accepted by the user ,but i cant understand where is the mistake i have done , only text file i can transfer to another user,or it also readable ,even word file i cant read.
    if any one have the solution of this problem please help me....


    Thank you in advance...
    Ranjan
    Are you getting any errors?

    Could you please post your code that you're using to transfer the files so that we can see what your doing?

    -Frinny

    Comment

    • click2ranjan
      New Member
      • Mar 2008
      • 3

      #3
      added
      Code:
       tags
      
      [QUOTE=Frinavale]Are you getting any errors?
      
      Could you please post your code that you're using to transfer the files so that we can see what your doing?
      
      -Frinny[/QUOTE]
      
      yes,I am getting error "file is not in correct format",file is transfered correctly but the file recieved by the user shows "file is not in correct format"
      
      here is the code from client side 
      [code=cpp]
      ///File to byte 
       FILE = System.IO.Path.GetFileName(filename);
                  StaticData.filedata = null;
                     
                      System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                      StaticData.filedata = new Byte[fs.Length];
                      fs.Read(StaticData.filedata, 0, StaticData.filedata.Length);
                      fs.Close();
      
      //clientside File sending code
      private void SendMsg(string text)
              {
                  Filesend = false;
                  if (StaticData.filedata != null)
                  {
                      if (StaticData.filedata.Length > 10 && StaticData.filedata.Length <= 25000000)
                      {
      
                          Filesend = true;
                      }
                      else
                      {
                          MessageBox.Show("Selected File is too large to send ");
                          Filesend = false;
                      }
                  }
                  string encryp = SSTCryptographer.Encrypt(text, SetKey());
                  //string decryp = SSTCryptographer.Decrypt(encryp, SetKey());
                  byte[] concatestr;
                  try
                  {
                     
                      if (m_clientSocket.Connected)
                      {
      
                          byte[] stringtobyte = System.Text.Encoding.ASCII.GetBytes(encryp);
                          byte[] tmprt = System.Text.Encoding.ASCII.GetBytes("CHARLEE");
      
      
                          if (Filesend)
                              {
                                  concatestr = new byte[stringtobyte.Length + StaticData.filedata.Length+ tmprt.Length];                           
                                  System.Buffer.BlockCopy(stringtobyte, 0, concatestr, 0, stringtobyte.Length);
                                  int vbdat = stringtobyte.Length + StaticData.filedata.Length;
                                  System.Buffer.BlockCopy(StaticData.filedata, 0, concatestr, stringtobyte.Length, StaticData.filedata.Length);
                                  System.Buffer.BlockCopy(tmprt, 0, concatestr, vbdat, tmprt.Length);
                                  //System.Buffer.BlockCopy(StaticData.filedata, 0, concatestr, 0, StaticData.filedata.Length);
                                  m_clientSocket.BeginSend(concatestr, 0, concatestr.Length, SocketFlags.None, SendCallBack, null);
      
                              }
                              else
                              {
                                  concatestr = new byte[stringtobyte.Length];
                                  System.Buffer.BlockCopy(stringtobyte, 0, concatestr, 0, stringtobyte.Length);
                                  //System.Buffer.BlockCopy(StaticData.filedata, 0, concatestr, 0, StaticData.filedata.Length);
                                  m_clientSocket.BeginSend(concatestr, 0, concatestr.Length, SocketFlags.None, SendCallBack, null);
      
                              }
                          
                         
      
                         
                          string msg = text;
                          Filesend = false;
                          //concatestr = ;
                          //m_clientSocket.BeginSend(concatestr, 0, concatestr.Length, SocketFlags.None, SendCallBack, null);
                          //StaticData.filedata = null;
                          // New code to send strings
                          //NetworkStream networkStream = new NetworkStream(m_clientSocket);
                          //System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);
                          //streamWriter.WriteLine(stringtobyte);
                          //streamWriter.Flush();
                      }
                    
                  }
                  catch (SocketException se)
                  {
                      MessageBox.Show(se.Message);
                  }
      
              }
      
      
      //client side file recieving code
      
       int ptr1=szData.IndexOf('@',0);                   
                          StaticData.Formrecieve = true;
                          StaticData.FormAccept = true;
                          //extract mailto name
                          //rest
                          string[] filestring = szData.Split('|');
                         // StaticData.filesize = 0;
                          //int up = theSockId.dataBuffer.GetUpperBound(0);
                          //StaticData.Formrecieve = Convert.ToBoolean(filestring[2]);
                          StaticData.FileName = filestring[1];
                          Int32 filelength = Convert.ToInt32(filestring[2]);
                          //StaticData.filesize = Convert.ToInt32(filestring[3]);
                         
                          string strndata = filestring[0];
                          filestring = strndata.Split('*');
                          strndata = filestring[1];
                          filestring = strndata.Split('^');
                          strndata = filestring[0];
                          filestring = strndata.Split(';');
                          StaticData.recieve_user = filestring[1];
                          StaticData.send_user = filestring[0];
                          Byte[] dataBuffer1234 = new Byte[filelength];
                          System.Buffer.BlockCopy(theSockId.dataBuffer, ptr1+1, dataBuffer1234, 0, filelength);
                          //System.Buffer.BlockCopy(StaticData.filedata, 0, concatestr, stringtobyte.Length, StaticData.filedata.Length);
                          StaticData.filedata = new Byte[filelength];
                          System.Buffer.BlockCopy(dataBuffer1234, 0, StaticData.filedata, 0, dataBuffer1234.Length);
                          FileAcceptance = true;
      //File saving
      
       file.Write(StaticData.filedata, 0, StaticData.filedata.Length);
                      file.Flush();
                      file.Close();

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Converting the bytes for the file to a string (for encryption?) will cause the file to become scrambled. Certain bytes will be stripped away in the process.
        If you wish to send binary files, you will need to keep them as byte[] and not do any string conversion on them.

        If you want an example of this, try using an FTP program and send a picture file in "text" mode and not "binary" mode and watch what happens.

        Comment

        Working...