Tuesday, June 2, 2009

RECEIVING DATA FROM A SOCKET

RECEIVING DATA FROM A SOCKET
ØTo receive data from a socket ,use the ReceiveAsync() method
ØBegins an asynchronous request to receive data from a connected Socket object. \
ReceiveAsync()
ØNamespace: System.Net.SocketsAssembly: System.Net (in System.Net.dll)
C#
Øpublic bool ReceiveAsync( SocketAsyncEventArgs e)
STEPS
1.
ØCreate a byte array that can store data that is received on socket.
Øbyte[] response =new byte[1024];
2.
ØSet up the SocketAsyncEventArgs object that will be used to connect the socket to the remote host.
•SocketAsyncEventArgs arg = new SocketAsyncEventArgs();
arg.UserToken =s;
arg.RemoteEndPoint =ep;
3.
ØAttaching it to the SocketAsyncEventArgs object using SetBuffer()
ØSetBuffer accepts a byte array as 1st parameter,int offset as 2nd parameter and int length as 3rd parameter
•arg.SetBuffer(response,0, response.Lenth);
4.
ØOnReceivedCompleted() event handler to a SocketAsynEventArgs object
•arg.Completed+= new EventHandler
( OnReceivedCompleted);
5.
ØTo received data to remote host
ØReceivedAsync() method of the socket object and pass the SocketAsynEventArgs object
Øs. ReceivedAsync(arg);
6.
ØWhen data is received from the remote host the OnReceivedCompleted() event handler is called.

No comments:

Post a Comment