Download presentation
Presentation is loading. Please wait.
1
1 L53 Networking (2)
2
2 OBJECTIVES In this chapter you will learn: To understand Java networking with URLs, sockets and datagrams. To implement Java networking applications by using sockets and datagrams. To understand how to implement Java clients and servers that communicate with one another. To understand how to implement network-based collaborative applications. To construct a multithreaded server.
3
3 24.6 Client/Server Interaction with Stream Socket Connections Client/server chat application – Uses stream sockets – Server waits for a client connection attempt – Client connects to the server Send and receive messages – Client or server terminates the connection – Server waits for the next client to connect
4
4 Outline Server.java (1 of 8) Lines 8-9 Line 25 Line 26 Import ServerSocket and Socket from package java.net Declare ServerSocket server which waits for client connection Declare Socket connection which connects to the client
5
5 Outline Server.java (2 of 8)
6
6 Outline Server.java (3 of 8) Line 62 Line 68 Line 69 Line 70 Create ServerSocket at port 12345 with queue of length 100 Wait for a client connection After the connection is established, obtain references to the streams for the connection Send the initial connection message to the client and process all messages received from the client
7
7 Outline Server.java (4 of 8) Line 93 Line 95 Line 102 Line 103 Use ServerSocket method accept to wait for a connection from a client Output the host name of the computer that made the connection using Socket method getInetAddress and InetAddress method getHostName Obtain Socket ’s OutputStream and use it to initialize ObjectOutputStream Method flush empties output buffer and sends header information
8
8 Outline Server.java (5 of 8) Line 106 Line 124 Obtain Socket ’s InputStream and use it to initialize ObjectInputStream Use ObjectInputStream method readObject to read a String from client
9
9 Outline Server.java (6 of 8) Lines 136-151 Line 145 Line 158 Method closeConnection closes streams and sockets Invoke Socket method close to close the socket Use ObjectOutputStream method writeObject to send a String to client
10
10 Outline Server.java (7 of 8)
11
11 Outline Server.java (8 of 8)
12
12 Outline ServerTest.java
13
13 Outline Client.java (1 of 7)
14
14 Outline Client.java (2 of 7)
15
15 Outline Client.java (3 of 7)
16
16 Outline Client.java (4 of 7) Line 87 Lines 90-91 Line 98 Line 99 Create a Socket that will connect with port 12345 on the server Use InetAddress static method getByName to obtain an InetAdress object containing the IP address specified as a command- line argument to the application Display a message indicating the name of the server computer to which the client has connected Method flush empties output buffer and sends header information Obtain Socket ’s OutputStream and use it to initialize ObjectOutputStream
17
17 Outline Client.java (5 of 7) Line 117 Line 138 Read a String object from server Invoke Socket method close to close the socket
18
18 Outline Client.java (6 of 7) Line 151 Use ObjectOutputStream method writeObject to send a String to server
19
19 Outline Client.java (7 of 7)
20
20 Outline ClientTest.java (1 of 2) Program output
21
21 Outline ClientTest.java (2 of 2) Program output
22
22 24.7 Connectionless Client/Server Interaction with Datagrams Connectionless transmission with datagrams – No connection maintained with other computer – Break message into separate pieces and send as packets – Message arrive in order, out of order or not at all – Receiver puts messages in order and reads them
23
23 Outline Server.java (1 of 4) Line 16 Use a DatagramSocket as our server
24
24 Outline Server.java (2 of 4) Line 30 Lines 47-48 Line 50 Use the DatagramSocket constructor that takes an integer port number argument to bind the server to a port where it can receive packets from clients Create a DatagramPacket in which a received packet of information can be stored Use DatagramSocket method receive to wait for a packet to arrive at the server
25
25 Outline Server.java (3 of 4) Line 54 Line 55 Line 56 Line 57 Lines 77-79 Use DatagramPacket method getAddress to obtain the host name of the computer from which the packet was sent Use DatagramPacket method getPort to obtain the port number through which the host computer sent the packet Use DatagramPacket method getLength to obtain the number of bytes of data sent Use DatagramPacket method getData to obtain an byte array containing the data Create a DatagramPacket, which specifies the data to send, the number of bytes to send, the client computer’s Internet address and the port where the client is waiting to receive packets
26
26 Outline Server.java (4 of 4) Use method send of DatagramSocket to send the packet over the network
27
27 Outline ServerTest.java Program ouptut Server window after packet of data is received from client
28
28 Outline Client.java (1 of 5)
29
29 Outline Client.java (2 of 5) Line 41 Lines 44-45 Line 47 Convert the String to a byte array Create a DatagramPacket and initialize it with the byte array, the length of the string that was entered by the user, the IP address to which the packet is to be sent and the port number at which the server is waiting Use DatagramPacket method send to send the packet
30
30 Outline Client.java (3 of 5) Line 71 Create a DatagramSocket for sending and receiving packets
31
31 Outline Client.java (4 of 5) Lines 88-89 Line 95 Line 96 Line 97 Line 98 Create a DatagramPacket to store received information Use DatagramPacket method getAddress to obtain the host name of the computer from which the packet was sent Use DatagramPacket method getPort to obtain the port number through which the host computer sent the packet Use DatagramPacket method getLength to obtain the number of bytes of data sent Use DatagramPacket method getData to obtain an byte array containing the data
32
32 Outline Client.java (5 of 5)
33
33 Outline ClientTest.java Program output Client window after sending packet to Server and receiving packet back from Server
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.