473,379 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

sovitagar
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!!!
Apr 8 '14 #1
3 5156
Rabbit
12,516 Expert Mod 8TB
A zipped file is still a file
Apr 8 '14 #2
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...
Apr 10 '14 #3
SERVER SIDE

Expand|Select|Wrap|Line Numbers
  1. string[] fileName = { "AppReplayIp.zip" };
  2.             string filePath = @"D:\islands\";
  3.             int islandCount = fileName.Count();
  4.             byte[] sendCount = ASCIIEncoding.ASCII.GetBytes(islandCount.ToString());
  5.             clientSock.Send(sendCount);
  6.  
  7.  
  8.             foreach (string file in fileName)
  9.             {
  10.                 byte[] fileNameByte = Encoding.ASCII.GetBytes(file);
  11.                 byte[] fileData = File.ReadAllBytes(filePath + file);
  12.                 byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
  13.                 byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
  14.                 fileNameLen.CopyTo(clientData, 0);
  15.                 fileNameByte.CopyTo(clientData, 4);
  16.                 fileData.CopyTo(clientData, 4 + fileNameByte.Length);
  17.                 clientSock.Send(clientData);
  18.                 Console.WriteLine("File:{0} has been sent.", file);
  19.  
  20.             }

CLIENT SIDE
Expand|Select|Wrap|Line Numbers
  1. byte[] buf = new byte[1024];
  2.             int countByte = clientSock.Receive(buf);
  3.             int Count = Convert.ToInt32(countByte);
  4.             Console.WriteLine("Island count :{0}" ,count);
  5.             for (j = 0; j < count;j++ )
  6.             {
  7.                 byte[] clientData = new byte[1024 * 10000];
  8.                 string receivedPath = @"D:\sovit\";
  9.                 int receivedBytesLen = clientSock.Receive(clientData);
  10.                 Console.WriteLine("received Bytes Len {0} ", receivedBytesLen);
  11.                 int fileNameLen = BitConverter.ToInt32(clientData, 0);
  12.                 Console.WriteLine("fileNameLen  {0}", fileNameLen);
  13.                 string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);
  14.                 string CompleteFilePath = Path.Combine(receivedPath, fileName);
  15.                 Console.WriteLine("Connection Established\n & File started receiving : {0}", fileName);
  16.                 BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); ;
  17.                 bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);
  18.                 Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);
  19.                 bWrite.Close();
  20.                 try
  21.                 {
  22.                     Console.WriteLine("Extratction started");
  23.                     var options = new ReadOptions { StatusMessageWriter = System.Console.Out };
  24.                     using (ZipFile zip = ZipFile.Read(CompleteFilePath, options))
  25.                     {
  26.                         zip.ExtractAll(completePath[i]);
  27.                     }
  28.                     Console.WriteLine("FIles from {0} extracted successfully",fileName);
  29.                     byte[] sendStatus = ASCIIEncoding.ASCII.GetBytes("Files extracted successfully");
  30.                     clientSock.Send(sendStatus);
  31.                 }
  32.                 catch (System.Exception ex1)
  33.                 {
  34.                     System.Console.Error.WriteLine("exception: " + ex1);
  35.                     byte[] sendStatus= ASCIIEncoding.ASCII.GetBytes("The file couldnt be extracted successfully");
  36.                     clientSock.Send(sendStatus);
  37.                 }
  38.             }
  39.  
  40.                 clientSock.Close();
  41.             }
The error occurs while trying to extract files on the client side.
Zip.ZipException: Cannot read that as a zip file-->Ionic.Zip.BadReadException: Bad Signature at position .......
Please help with the same
Apr 10 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Raichu | last post by:
Hi All, I'm new to VC++.net and I searched through the forums and couldn't find anything related to this. I'm trying to send a flat file from a VC++.net program to a PHP file residing on a...
1
by: Syed Aun Ali | last post by:
i have a device connected to a node computer(any client/server computer) in a intanet.the device sends data contineously.i want to store the data from device in a text box in the node computer as...
4
by: wackyphill | last post by:
Is there a way to code it so that you can transmit a file to a client programatically so that you could send it a file that is not part of your website? Like if my site is in c:\inetpub\mySite ...
0
by: Jacek Francuz | last post by:
Hi! How can I change button image end send file to Client in one request? I try to change image in first step and I add onstartupscript: "Form1.submit()" and then in second step I send...
2
by: Alpha | last post by:
Hi, I'm able to make connection to a server using socket connection. However, when I send a command string the server just ignores it. All command string needs to start with "0xF9" at Byte 0. ...
1
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
7
by: JamesHoward | last post by:
Does anyone know any method to have one program, acting as a server transfer a socket connection to another program? I looked into transferring the connection via xml rpc to no avail. It seems to...
2
by: Martin | last post by:
I have the following code being used in several of my PHP scripts (it's in an include file). It opens a socket connection to a process that's running on the same computer that IIS/PHP is running...
2
by: kashifjawed | last post by:
I'm developing a c# asynchronous socket application for tranfering file or large data from client to server and server to client as well in chunks. Application sometimes transfer file from client...
2
by: vvinc | last post by:
I need to upload a file (xml, pdf, etc) from the user's PC, but I need to write it directly to a file server. I cannot use FileUpload because there is no way to direct its initial upload to anything...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.