Download presentation
Presentation is loading. Please wait.
1
Sockets and URLs 3-Dec-18
2
Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect two programs running on the same computer There are two kinds of sockets: A Socket is a client socket, and initiates the communication A ServerSocket waits for a request; it then May perform the requested action May return a result to the requester
3
Socket constructors import java.net.*;
Socket(InetAddress address, int port) Creates a client socket and connects it to the specified port number at the specified IP address ServerSocket(int port) Creates a server socket on the specified port, with a default queue length of 50 The actual work, for both kinds of sockets, is done by an instance of the SocketImpl class
4
Some Socket methods getInputStream() getOutputStream() close()
Returns an input stream for this socket getOutputStream() Returns an output stream for this socket close() Closes this socket
5
Some ServerSocket methods
accept() Listens for a connection to be made to this socket and accepts it Returns a Socket as its result Once you have a Socket , you can use the same methods on both the client and the server sides For input, use the getInputStream() and getOutputStream() methods on the returned Socket close() Closes this socket
6
Socket fields protected InetAddress address The IP address of the remote end of this socket protected FileDescriptor fd The file descriptor object for this socket protected int localport The local port number to which this socket is connected protected int port The port number on the remote host to which this socket is connected All of these except FileDescriptor have associated getter methods
7
URLs A URL is a Uniform Resource Locator, typically a Web address
Some constructors: URL(String spec) Creates a URL object from the information in the String URL(String protocol, String host, int port, String file) Creates a URL object from the specified protocol, host, port number, and file The host may be localhost, to indicate “this machine” URL(String protocol, String host, String file) Same as above, but uses the default port for this protocol
8
Some URL methods openStream() openConnection()
Opens a connection to this URL and returns an InputStream for reading from that connection openConnection() Returns a URLConnection object that represents a connection to the remote object referred to by the URL getProtocol(), getHost(), getPort(), getFile(), getQuery()
9
Using a URLConnection The steps in using a URLConnection are:
Use openConnection() to create the URLConnection object Set the parameters as desired: setAllowUserInteraction, setDoInput, setDoOutput, setIfModifiedSince, setUseCaches, setRequestProperty Use the connect method to make the actual connection Use the remote object
10
Some URLConnection methods
getHeaderField(int n) Returns the value for the nth header field getHeaderField(String name) Returns the value of the named header field getHeaderFieldKey(int n) Returns the key for the nth header field getInputStream() Returns an input stream that reads from this open connection getOutputStream() Returns an output stream that writes to this connection
11
The End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.