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

S

shahla.saeed

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;
}
 
G

Guest

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
 
S

shahla.saeed

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

hmmmm...i wud b greatful to u if u send me example code....thanks
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top