Powered by Blogger.

Saturday 31 January 2015

Java Sockets Programming

No comments :
There are two kinds of TCP sockets in java. One is for servers, and the other is for clients. The ServerSocket class is Designed to be a "Listener", which whats for clients to connect before doing anything. Thus, ServerSocket is for servers. The socket class is for clients. it is designed to connect to server sockets and initiate protocol exchanges. Because client sockets are the most commonly used by java application, they Examined here. Here are tow constructors used to create client sockets.

Socket(String hostName) throws UnkownHostException, IOException Creates a socket Connected to the Named Host and Port. 
Socket(InetAddress ipAddress, int port) throws IOException Creates a socket using a Preexisting InetAddress Object and port. 

Socket Defines Several instance Methods

InetAddress getInetAddress() Returns the InetAddress associated with the socket object. it returns null if the socket is not connected.
int getPort() Returns the remote Port to which invoking socket object is connected. it returns 0 if the socket is not connected.  
int getlocalport() Returns the local port to which the invoking socket object is bound. it returns -1 if the socket is not bound.

You can gain access to the input and output streams associated with a Socket by use of the getInputStream() and getOutputStream() method, as shown here. Each can throw an IOException if the socket has been invalidated by a loss of connection. These streams are used exactly like the I/O Streams to send and receive data .

InputStream getInputStream() throws IOExceptionReturns the InputStream associated with the invoking socket. 
OutputStream getOutputStream() throws IOException Returns the OutputStream associated with the invoking socket.

Several other methods are available,Including Connect(), Which allows you to specify a new Connection; isConnected(), which returns true if the socket is connected to a server; isBound(), which returns true if the socket is bound to an address; and isClosed(), which is returns true if the socket is Closed. To Close a socket, call close(). Closing a socket also closes the I/O Stream associated with the socket, call close(). Closing a Socket also closes the I/O Stream associated with the socket. The Following Program Provides a Simple Socket Example.



Out Put:
Whois Server Version 2.0 
Domain names in the .com and .net domains can now be registered 
with many different competing registrars. Go to http://www.internic.net for detailed information. Domain Name: MHPROFESSIONAL.COM
Registrar: CSC CORPORATE DOMAINS, INC. 
Sponsoring Registrar IANA ID: 299 
Whois Server: whois.corporatedomains.com 
Referral URL: http://www.cscglobal.com/digitalbrandservices 
Name Server: PDNS85.ULTRADNS.BIZ 
Name Server: PDNS85.ULTRADNS.COM.

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.