Download presentation
Presentation is loading. Please wait.
1
Networking
2
Class InetAddress This class represents an Internet Protocol (IP) address . getAddress public byte[] getAddress() Returns the raw IP address of this InetAddress object. The result is in network byte order: the highest order byte of the address is in getAddress()[0]. getHostAddress public String getHostAddress() Returns the IP address string
3
getByName(String host) throws UnknownHostException
Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as "java.sun.com", or a string representing its IP address, such as " ". Parameters: host - the specified host, or null for the local host. Returns: an IP address for the given host name. Throws: UnknownHostException - if no IP address for the host could be found.
4
InetAddress[] getAllByName(String host)
Determines all the IP addresses of a host, given the host's name. getLocalHost Returns the local host. getHostName Gets the host name for this IP address. public boolean isMulticastAddress() Utility routine to check if the InetAddress is an IP multicast address. Logical identifier for a group of hosts.
5
java.net.URL The java.net.URL class represents a URL. There are constructors to create new URLs and methods to parse the different parts of a URL public URL(String u) throws MalformedURLException public URL(String protocol, String host, String file) throws MalformedURLException public URL(String protocol, String host, int port, String file) throws MalformedURLException
6
URL like http://www.xyz.edu/schedule/summer2011/bgrad.html#cs
URL u = new URL(" "); u = new URL("http", " "/schedule/summer2011/bgrad.html#cs"); u = new URL("http", " 80, "/schedule/summer2011/bgrad.html#cs");
7
Methods public String getProtocol() public String getHost()
Gets the protocol name of this URL. public String getHost() Gets the host name of this URL, public String getFile() Gets the file name of this URL
8
public String getPath() public String toExternalForm()
Gets the path part of this URL. public String toExternalForm() returns a human-readable String representing the URL . Public String sameFile(Url u) Compares two URLs public int getPort() Gets the port number of this URL
9
If a port is not explicitly specified in the URL, it's set to -1.
This does not mean that the connection is attempted on port -1 (which doesn't exist) but rather that the default port is to be used.
10
Socket Class This class handles client sockets .
The java.net.Socket class allows you to connect to remote machines send data; receive data; close the connection. public Socket(String host, int port) throws UnknownHostException, IOException public Socket(InetAddress address, int port) throws IOException
11
int getPort() Returns the remote port to which this socket is connected.
Int getLocalPort() Returns the local port to which this socket is bound. InetAddress getInetAddress() Returns the address to which the socket is connected. InputStream getInputStream() Returns an input stream for this socket. OutputStream getOutputStream() Returns an output stream for this socket.
12
Once a socket has connected you send data to the server via an output stream.
You receive data from the server via an input stream.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.