Can we send a .Zip file from server to client using TCP socket connection in C#?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sovitagar
    New Member
    • Apr 2014
    • 8

    #1

    Can we send a .Zip file from server to client using TCP socket connection in C#?

    I know how to transfer files over a network but I have a confusion whether or not can we send a Zipped file from server to client using a TCP connection.
    P.S. : the code is to be written in C#
    Please help.
    Thanks in advance!!!
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    A zipped file is still a file

    Comment

    • sovitagar
      New Member
      • Apr 2014
      • 8

      #3
      The point is while sending the zipped file it so happens that the file transfer is sometimes successful while most of the times there is an error where the entire content of the zip file is not transferred.The error relates to either "Bad Read Exception" or Index out of bound Exception...

      Comment

      • sovitagar
        New Member
        • Apr 2014
        • 8

        #4
        SERVER SIDE

        Code:
        string[] fileName = { "AppReplayIp.zip" };
                    string filePath = @"D:\islands\";
                    int islandCount = fileName.Count();
                    byte[] sendCount = ASCIIEncoding.ASCII.GetBytes(islandCount.ToString());
                    clientSock.Send(sendCount);
        
                
                    foreach (string file in fileName)
                    {
                        byte[] fileNameByte = Encoding.ASCII.GetBytes(file);
                        byte[] fileData = File.ReadAllBytes(filePath + file);
                        byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                        byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
                        fileNameLen.CopyTo(clientData, 0);
                        fileNameByte.CopyTo(clientData, 4);
                        fileData.CopyTo(clientData, 4 + fileNameByte.Length);
                        clientSock.Send(clientData);
                        Console.WriteLine("File:{0} has been sent.", file);
        
                    }

        CLIENT SIDE
        Code:
        byte[] buf = new byte[1024];
                    int countByte = clientSock.Receive(buf);
                    int Count = Convert.ToInt32(countByte);
                    Console.WriteLine("Island count :{0}" ,count);
                    for (j = 0; j < count;j++ )
                    {
                        byte[] clientData = new byte[1024 * 10000];
                        string receivedPath = @"D:\sovit\";
                        int receivedBytesLen = clientSock.Receive(clientData);
                        Console.WriteLine("received Bytes Len {0} ", receivedBytesLen);
                        int fileNameLen = BitConverter.ToInt32(clientData, 0);
                        Console.WriteLine("fileNameLen  {0}", fileNameLen);
                        string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
                        string CompleteFilePath = Path.Combine(receivedPath, fileName);
                        Console.WriteLine("Connection Established\n & File started receiving : {0}", fileName);
                        BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
                        bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
                        Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);
                        bWrite.Close();
                        try
                        {
                            Console.WriteLine("Extratction started");
                            var options = new ReadOptions { StatusMessageWriter = System.Console.Out };
                            using (ZipFile zip = ZipFile.Read(CompleteFilePath, options))
                            {
                                zip.ExtractAll(completePath[i]);
                            }
                            Console.WriteLine("FIles from {0} extracted successfully",fileName);
                            byte[] sendStatus = ASCIIEncoding.ASCII.GetBytes("Files extracted successfully");
                            clientSock.Send(sendStatus);
                        }
                        catch (System.Exception ex1)
                        {
                            System.Console.Error.WriteLine("exception: " + ex1);
                            byte[] sendStatus= ASCIIEncoding.ASCII.GetBytes("The file couldnt be extracted successfully");
                            clientSock.Send(sendStatus);
                        }
                    }
                          
                        clientSock.Close();
                    }
        The error occurs while trying to extract files on the client side.
        Zip.ZipExceptio n: Cannot read that as a zip file-->Ionic.Zip.BadR eadException: Bad Signature at position .......
        Please help with the same

        Comment

        Working...