m11 - networking
Post on 07-Aug-2018
245 Views
Preview:
TRANSCRIPT
-
8/21/2019 M11 - Networking
1/14
Module 11Core Java API: Networking
Java Course
-
8/21/2019 M11 - Networking
2/14
2
Module Objectives
At the end of this module, participants will beable to:
Define the basic concepts about TCP/IP
networking.
Establish a network connection.
Send data across the network.
-
8/21/2019 M11 - Networking
3/14
3
TCP/IP and UDP Networking
Applications that communicate over the Internet use the TCPprotocol or the UDP protocol.
TCP is a protocol that is best applicable for connections thatrequire reliability and guaranteed sequence of delivery.
UDP is a connectionless protocol. It does not care about the
sequence that the data is sent, or even if it was received at all.
-
8/21/2019 M11 - Networking
4/14
4
TCP/IP and UDP Networking (cont.)
9999
3333
80
Server IP: 192.168.0.1
80
Server IP: 192.168.0.2
9999
Client App
Client App
Client App
Client App
Client App
Individual nodes in anetwork are identified by
an IP address.
Network connections
created by applications
running on a singlecomputer on the network
can be further identified by
an assignedport number.
The data is transferredthrough streams.
-
8/21/2019 M11 - Networking
5/14
5
Establishing a Connection
For TCP connections, the URL, URLConnection, Socket, andServerSocket classes are used.
For UDP connections, the DatagramPacket, DatagramSocket,
and MulticastSocket are used.
-
8/21/2019 M11 - Networking
6/14
6
Using URLs
A URL is a Uniform Resource Locator and is used as areference to any resource on the Internet.
A URL is composed of aprotocol and a resource identifier.
The protocol refers to a set of rules governing the format of
messages that are exchanged between computers.
The resource identifier indicates where the resource is to be
found.
Protocol Resource Name URL
http www.accenture.com http://www.accenture.com
-
8/21/2019 M11 - Networking
7/147
Using URLs (cont.)
Create an instance representing a specific URL using the URLandURLConnection classes.
URL accentureURL = newURL ("http://www.accenture.com");
URLConnection connection = accentureURL.openConnection();
Open a stream in order to obtain that resource from thespecified URL.
BufferedReader in = newBufferedReader(newInputStreamReader(accentureURL.openStream()));
-
8/21/2019 M11 - Networking
8/148
Using URLs (cont.)
To send data to a URL resource, set the connection object to beable to send data and obtain an output stream instead.
connection.setDoOutput(t rue );
OutputStreamWriter out = newOutputStreamWriter(connection.getOutputStream());
After reading/writing to the URL, close the stream.
in.close();
out.close();
** Refer to the URLSample.java sample code
-
8/21/2019 M11 - Networking
9/149
Using Sockets
Sockets represent the end points between two applications in anetwork.
When a socket is created it is bound to an IP address and a port
number.
One end point acts as the server that waits for connections
(represented by ServerSocket class).
One or more end points acts as a client and that seeks to
connect to a server (represented by Socket class).
** Refer to the ServerSample.java and ClientSample.java sample code
-
8/21/2019 M11 - Networking
10/1410
The InetAddress Class
The InetAddress class is the representation of an IP addressthat is used by the TCP and UDP protocols.
Instances of this class are returned by various factory methods:
InetAddress.getLocalHost()returns the InetAddress of the local
machine.
InetAddress.getByAddress()
returns an InetAddress based on thehost name given.
InetAddress.getByAddress(byte[])returns an InetAddress based
on the IP address passed as an array of bytes.
** Refer to the InetAddressSample.java sample code
-
8/21/2019 M11 - Networking
11/14
11
Creating a ServerSocket
Instantiate a ServerSocket instance and bind it to a specific portnumber.
ServerSocket server = newServerSocket(9999);
Wait for a connection from a client.
client = server.accept();
Use the returned Socket object to obtain an output or input
stream to the client.
out = newPrintWriter(client.getOutputStream(), t rue );
Close any open streams, Sockets, and ServerSockets when
finished.
-
8/21/2019 M11 - Networking
12/14
12
Creating a Client Socket
Instantiate an instance of the Socket class and pass the networkaddress and the port of the server the application.
Socket socket = newSocket(InetAddress.getLocalHost(), 9999);
Retrieve input or output streams bound to the socket.
BufferedReader in = newBufferedReader(newInputStreamReader(socket.getInputStream()));
Close all the streams and sockets when finished.
-
8/21/2019 M11 - Networking
13/14
13
Questions and Comments
-
8/21/2019 M11 - Networking
14/14
14
Activity
1) Create a simple chat server that will allow
multiple telnet clients to connect to your
server and chat.
2) Text entered through the telnet client
should be broadcast to all other clients
connected to the server.
3) To open a telnet client just type telnet in the console Substitute
the host and part values with whatever
you are using.
Hint: A common algorithm for the server is1) Create a ServerSocket.
2) Wait for connections.
3) For every client that connects, create a
new thread to read input from that client.
4) Go back to step 2.
top related