Advertisement

08.14.2008 at 07:30AM PDT, ID: 23648236 | Points: 125
[x]
Attachment Details

Transferring binary file across socket in network corrupting file at the othe end

Asked by upsilon_matt in Peer to Peer, TCP/IP

Tags: ,

Hi There,

  I am developing some code that will be used to transfer a binary file from one machine to another on the local network.  When the client connects to the server on localhost the file ALWAYS transfers correctly, unfortunately when connecting to a remote machine on the same LAN only some files transfer OK; others transfer but end up getting corrupted or not transferred properly over.  I am transferring media files (image files, movies, etc)

The following is the code I am using:  I am initializing the BinaryReader and Writers from the Network stream object obtained from the socket object like so:

              NetworkStream networkStream = socketForServer.GetStream();;
              StreamReader streamReader = new System.IO.StreamReader(networkStream);
              StreamWriter streamWriter = new System.IO.StreamWriter(networkStream);

            socketForServer.ReceiveTimeout = 5000;
              
              BinaryWriter bw = new BinaryWriter(networkStream);

The stream writers are used to send the file size and other commands, so in sequence:

1) Initialize socket connection to server
2) Initiailize stream writers, readers and binary writers and readers
3) Send command like (SENDMEDIA)
4) Send file size as a string, other side convert to int using convert.toint32
5) Call the appropriate functions below (readbinary receives, writebinary writes)

Please help!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
public static bool writeBinary(BinaryWriter bw, FileStream myFile)
        {
        	try
        	{
	        	long ii = 0;
 
	        	byte[] myData = new byte[myFile.Length];
	        	
	        	//myFile.Read(myData,0,Convert.ToInt32(myFile.Length));
	        	
	        	while(ii < myFile.Length)
	        	{
	        		myData[ii] = (byte)myFile.ReadByte(); //br.ReadByte();
	        		//bw.Write(
	        		//bw.Flush();
	        		ii++;
	        	}	        	
	        	
	        	bw.Write(myData);
	        	bw.Flush();
	        	
	        	return true;
        	}
        	catch(Exception ex)
        	{        	
        		MessageBox.Show(ex.ToString());
        		return false;
        	}
        }
        
public static bool readBinary(BinaryReader br, String fileName, int byteSize)
        {
        	try
        	{        		
	        	String storPath = fileName;
	        	
	        	if(File.Exists(storPath))
	        		File.Delete(storPath);
	        		
	        	FileStream fs = new FileStream(storPath,FileMode.CreateNew, FileAccess.Write);
	        	
	        	BinaryWriter sw = new BinaryWriter(fs);
	        	
	        	//for(int ii = 0; ii < byteSize; ii++)
	        	//{
	        	//	byte inChar = (byte)br.ReadByte();
	        	//	sw.Write(inChar);
	        	//}
	        	
	        	byte[] byteData = br.ReadBytes(byteSize);
		        sw.Write(byteData);
		        	
	        	sw.Close();
	        	fs.Close();   
	        	
	        	return true;
        	}
        	catch(Exception ex)
        	{
        		MessageBox.Show(ex.ToString());
        		return false;
        	}
        	
        }
 
Loading Advertisement...
 
[+][-]08.14.2008 at 07:37AM PDT, ID: 22230686

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.01.2008 at 09:08PM PDT, ID: 22364125

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628