Hi all,
Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the common process of connecting and of the server and client...
here is the code i am trying to implement. this code snippet is for message transfer only for multiple clients.
/////////////////////////////file receive method when sent from client/////////////
[code=c]
public void OnReceive(IAsyn cResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar .AsyncState;
Socket handler = state.workSocke t;
int bytesRead;
if (handler.Connec ted)
{
// Read data from the client socket.
try
{
bytesRead = handler.EndRece ive(ar);
if (bytesRead > 0)
{
state.fst.Write (state.buffer, 0, bytesRead);
state.fst.Flush ();
//There might be more data, so store the data received so far.
// state.sb.Remove (0, state.sb.Length );
// state.sb.Append (Encoding.ASCII .GetString(
// state.buffer, 0, bytesRead));
//// Display Text in Rich Text Box
// content = state.sb.ToStri ng();
//SetText(content );
handler.BeginRe ceive(state.buf fer, 0, StateObject.BUF FER_SIZE, 0,
new AsyncCallback(O nReceive), state);
}
}
catch (SocketExceptio n socketException )
{
//the other side closed impolitely
if (socketExceptio n.ErrorCode == 10054 || ((socketExcepti on.ErrorCode != 10004) && (socketExceptio n.ErrorCode != 10053)))
{
// Complete the disconnect request.
String remoteIP = ((IPEndPoint)ha ndler.RemoteEnd Point).Address. ToString();
String remotePort = ((IPEndPoint)ha ndler.RemoteEnd Point).Port.ToS tring();
// this.owner.Disc onnectClient(re moteIP, remotePort);
handler.Close() ;
handler = null;
}
}
catch (Exception exception)
{
MessageBox.Show (exception.Mess age + "\n" + exception.Stack Trace);
}
}
}
}
//////////class which handles connected clients on server side for file transfer for sending and receiving//////////////////////////////////
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BUFFER_SIZE = 1024;
// Receive buffer.
public byte[] buffer = new byte[BUFFER_SIZE];
// Received file stored in static/fixed location.
public FileStream fst=new FileStream(@"c: \\sample1.txt", FileMode.Create ,FileAccess.Wri te);
//public StringBuilder sb = new StringBuilder() ;
}[/code]
Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the common process of connecting and of the server and client...
here is the code i am trying to implement. this code snippet is for message transfer only for multiple clients.
/////////////////////////////file receive method when sent from client/////////////
[code=c]
public void OnReceive(IAsyn cResult ar)
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar .AsyncState;
Socket handler = state.workSocke t;
int bytesRead;
if (handler.Connec ted)
{
// Read data from the client socket.
try
{
bytesRead = handler.EndRece ive(ar);
if (bytesRead > 0)
{
state.fst.Write (state.buffer, 0, bytesRead);
state.fst.Flush ();
//There might be more data, so store the data received so far.
// state.sb.Remove (0, state.sb.Length );
// state.sb.Append (Encoding.ASCII .GetString(
// state.buffer, 0, bytesRead));
//// Display Text in Rich Text Box
// content = state.sb.ToStri ng();
//SetText(content );
handler.BeginRe ceive(state.buf fer, 0, StateObject.BUF FER_SIZE, 0,
new AsyncCallback(O nReceive), state);
}
}
catch (SocketExceptio n socketException )
{
//the other side closed impolitely
if (socketExceptio n.ErrorCode == 10054 || ((socketExcepti on.ErrorCode != 10004) && (socketExceptio n.ErrorCode != 10053)))
{
// Complete the disconnect request.
String remoteIP = ((IPEndPoint)ha ndler.RemoteEnd Point).Address. ToString();
String remotePort = ((IPEndPoint)ha ndler.RemoteEnd Point).Port.ToS tring();
// this.owner.Disc onnectClient(re moteIP, remotePort);
handler.Close() ;
handler = null;
}
}
catch (Exception exception)
{
MessageBox.Show (exception.Mess age + "\n" + exception.Stack Trace);
}
}
}
}
//////////class which handles connected clients on server side for file transfer for sending and receiving//////////////////////////////////
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BUFFER_SIZE = 1024;
// Receive buffer.
public byte[] buffer = new byte[BUFFER_SIZE];
// Received file stored in static/fixed location.
public FileStream fst=new FileStream(@"c: \\sample1.txt", FileMode.Create ,FileAccess.Wri te);
//public StringBuilder sb = new StringBuilder() ;
}[/code]
Comment