469,601 Members | 2,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

how to tranfer file using networkstream through sockets in c#..

hi,
plzz check my code and let me know where the problem is
lying...becuase whenever i try to tansfer the file of 573KB(mp3) it
just tranfer few Kb of file(Somtimes 5.2Kb,somtimes 32Kb..every time i
run the program).....but when i watch the transfering of bytes by
debuging it.(RED dot on while(nfs.CanRead) )..it shows that complete
bytes are transfered.(in my case i-e 573Kb)....i am unable to
understand whats going wrong in my program.
same is the problem with text file..it reads the bytes but not write
on file...

*CODE at RECIEVING END*
byte[] buffed = new byte[6000] ;
string Fname=System.Text.Encoding.ASCII.GetString(f_name,
0,count_char);
string pth=@"C:\Documents and Settings\Administrator\My Documents
\shared1\ ";
FileStream fout = new FileStream(pth+Fname, FileMode.Create,
FileAccess.Write) ;
NetworkStream nfs = new NetworkStream(Dsock) ;
string myCompleteMessage = "";
int numberOfBytesRead = 0;
while(nfs.CanRead)
{
if(nfs.DataAvailable)
{
numberOfBytesRead = nfs.Read(buffer, 0, buffer.Length);
myCompleteMessage =
string.Concat(System.Text.Encoding.ASCII.GetString (buffer, 0,
numberOfBytesRead));
fout.Write(buffer,0,(int)numberOfBytesRead) ;

}
}

*CODE at SENDING END*
int len=0;
byte[] buffed = new byte[6000] ;
//Open the file requested for download
FileStream fin = new FileStream(loadfile,FileMode.Open ,
FileAccess.Read) ;
//One way of transfer over sockets is Using a NetworkStream
NetworkStream nfs = new NetworkStream(DlSck_Arr[0]);
while(nfs.CanWrite)
{
//Read from the File (len contains the number of bytes read)
len =fin.Read(buffed,0,buffed.Length) ;
//Write the Bytes on the Socket
nfs.Write(buffed, 0,len);
//Increase the bytes Read counter
}
len=len+0;
}

Apr 21 '07 #1
3 10810
if you do async sockets, you can receive the entire file at once. this way
you wont have to fiddle with loops, a method will run when your receive
buffer is filled, or when an end of message is found. look at the BeginSend
and BeginReceive methods of a socket. if you need specific code, let me know
--
-iwdu15
Apr 22 '07 #2
On Apr 22, 7:20 am, iwdu15 <jmmgoalsteratyahoodotcomwrote:
if you do asyncsockets, you can receive the entirefileat once. this way
you wont have to fiddle with loops, a method will run when your receive
buffer is filled, or when an end of message is found. look at the BeginSend
and BeginReceive methods of a socket. if you need specific code, let me know
--
-iwdu15
hmmmm...i wud b greatful to u if u send me example code....thanks

Apr 23 '07 #3
heres a rough code example....should get u pointed in the right direction

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SampleSocket
{
public class SampleSocket
{
//our socket
private Socket soc;

//our buffer to receive bytes
private Byte[] buffer;

//ascii encoding for converting bytes to stings, etc
private ASCIIEncoding ascii;
//AsyncCallBack objects for Async Socker usage
private AsyncCallback ConnectCallBack;
private AsyncCallback ReceiveCallBack;
private AsyncCallback SendCallBack;
public SampleSocket()
{

//create socket
soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

//create a buffer of size 4095
buffer = new Byte[4095];

//create ascii encoding object
ascii = new ASCIIEncoding();

//create async objects to tell which method to go to when invoked
ConnectCallBack = new AsyncCallback(soc_ConnectCallBack);
ReceiveCallBack = new AsyncCallback(soc_ReceiveCallBack);
SendCallBack = new AsyncCallback(soc_SendCallBack);

}

public void Connect(String IP)
{

//parse IP
IPAddress ip = Dns.GetHostEntry(IP).AddressList[0];

//create IPEndPoint from ip and Port
IPEndPoint ep = new IPEndPoint(ip, 11000);

//begin connecting to other user
soc.BeginConnect(ep, ConnectCallBack, null);

}

private void soc_ConnectCallBack(IAsyncResult ar)
{

//finalize the connection and begin receiving information from
the user, passing in our AsyncCallBack object
soc.EndConnect(ar);
soc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
ReceiveCallBack, null);

}

private void soc_ReceiveCallBack(IAsyncResult ar)
{

//get the number of bytes received
int numBytes = soc.EndReceive(ar);

//if numBytes < 1 then other user disconnected from session, so
shutdown socket and exit method
if (numBytes < 1)
{

soc.Shutdown(SocketShutdown.Both);

return;

}

//if buffer is null, exit method
if (buffer == null)
return;

//get string from bytes (can also write the bytes to a file, all
bytes received are in the array named buffer)
String str = ascii.GetString(buffer);

//clear the array so if the array isnt filled next time, we wont
have extra info
Array.Clear(buffer, 0, buffer.Length);

//begin received again
soc.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
ReceiveCallBack, null);

}

public void Send(Byte[] bt)
{

//begin sending to connected client, passing our AsyncDelegate
telling where to go when the send is complete
soc.BeginSend(bt, 0, bt.Length, SocketFlags.None, SendCallBack,
null);

}

private void soc_SendCallBack(IAsyncResult ar)
{

//handle send finalize here...
soc.EndSend(ar);

}

}
}
hope this helps

--
-iwdu15
Apr 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Henk | last post: by
1 post views Thread by crashed | last post: by
10 posts views Thread by David | last post: by
reply views Thread by =?Utf-8?B?T2xpdmllciBHSUw=?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.