Monday, June 1, 2009

OPENING A SOCKET TO REMOTE HOST

•STEPS
•1.Create an endpoint object that defines the address of the remote host
•Create DnsEndPoint object that points to port 4510 on host www.MySocketServer.com
•DnsEndPoint ep= new DnsEndPoint(www.MySocketServer.com,4510);
The DnsEndPoint class contains a host name or an IP address and remote port information needed by an application to connect to a service on a host. By combining the host name or IP address and port number of a service, the DnsEndPoint class forms a connection point to a service.
2.To create Socket Object use Constructor of Socket class .
ØSocket s = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
Socket class Constructor takes three arguments
ØAddressFamily -Gets the Internet Protocol (IP) address family.
ØSocketType – Support only Stream
ØProtocolType - Support only TCP
3.Set up the SocketAsyncEventArgs object that will be used to connect the socket to the remote host.
ØSocketAsyncEventArgs sa = new SocketAsyncEventArgs();
sa.UserToken =s;
sa.RemoteEndPoint =ep;
4.Attact an event handler to handle the completed connection event
Øsa. Completed += new
EventHandler<>
(OnSocketConnectCompleted);
5.To open connection to remote host ,use
ConnectAsync() of socket object and pass the SocketAsyncEventArgs object
Øs. ConnectAsync(sa);

No comments:

Post a Comment