330 likes | 340 Views
L53. Networking (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.
E N D
L53 • Networking (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.
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
Declare ServerSocketserver which waits for client connection Declare Socketconnection which connects to the client Import ServerSocket and Socket from package java.net Outline • Server.java • (1 of 8) • Lines 8-9 • Line 25 • Line 26
Outline • Server.java • (2 of 8)
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 Outline • Server.java • (3 of 8) • Line 62 • Line 68 • Line 69 • Line 70
Output the host name of the computer that made the connection using Socket method getInetAddress and InetAddress method getHostName Use ServerSocket method accept to wait for a connection from a client Obtain Socket’s OutputStream and use it to initialize ObjectOutputStream Method flush empties output buffer and sends header information Outline • Server.java • (4 of 8) • Line 93 • Line 95 • Line 102 • Line 103
Obtain Socket’s InputStream and use it to initialize ObjectInputStream Use ObjectInputStream method readObject to read a String from client Outline • Server.java • (5 of 8) • Line 106 • Line 124
Method closeConnection closes streams and sockets Invoke Socket method close to close the socket Use ObjectOutputStream method writeObject to send a String to client Outline • Server.java • (6 of 8) • Lines 136-151 • Line 145 • Line 158
Outline • Server.java • (7 of 8)
Outline • Server.java • (8 of 8)
Outline • ServerTest.java
Outline • Client.java • (1 of 7)
Outline • Client.java • (2 of 7)
Outline • Client.java • (3 of 7)
Use InetAddress static method getByName to obtain an InetAdress object containing the IP address specified as a command-line argument to the application Create a Socket that will connect with port 12345 on the server Method flush empties output buffer and sends header information Obtain Socket’s OutputStream and use it to initialize ObjectOutputStream Display a message indicating the name of the server computer to which the client has connected Outline • Client.java • (4 of 7) • Line 87 • Lines 90-91 • Line 98 • Line 99
Invoke Socket method close to close the socket Read a String object from server Outline • Client.java • (5 of 7) • Line 117 • Line 138
Use ObjectOutputStream method writeObject to send a String to server Outline • Client.java • (6 of 7) • Line 151
Outline • Client.java • (7 of 7)
Outline • ClientTest.java • (1 of 2) • Program output
Outline • ClientTest.java • (2 of 2) • Program output
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
Use a DatagramSocket as our server Outline • Server.java • (1 of 4) • Line 16
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 Outline • Server.java • (2 of 4) • Line 30 • Lines 47-48 • Line 50
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 Outline • Server.java • (3 of 4) • Line 54 • Line 55 • Line 56 • Line 57 • Lines 77-79
Use method send of DatagramSocket to send the packet over the network Outline • Server.java • (4 of 4)
Outline • ServerTest.java • Program ouptut Server window after packet of data is received from client
Outline • Client.java • (1 of 5)
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 Convert the String to a byte array Use DatagramPacket method send to send the packet Outline • Client.java • (2 of 5) • Line 41 • Lines 44-45 • Line 47
Create a DatagramSocket for sending and receiving packets Outline • Client.java • (3 of 5) • Line 71
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 Create a DatagramPacket to store received information Use DatagramPacket method getData to obtain an byte array containing the data Outline • Client.java • (4 of 5) • Lines 88-89 • Line 95 • Line 96 • Line 97 • Line 98
Outline • Client.java • (5 of 5)
Outline • ClientTest.java • Program output Client window after sending packet to Server and receiving packet back from Server