1 / 33

L53

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.

kschafer
Download Presentation

L53

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. L53 • Networking (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. 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. 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

  5. Outline • Server.java • (2 of 8)

  6. 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

  7. 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

  8. 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

  9. 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

  10. Outline • Server.java • (7 of 8)

  11. Outline • Server.java • (8 of 8)

  12. Outline • ServerTest.java

  13. Outline • Client.java • (1 of 7)

  14. Outline • Client.java • (2 of 7)

  15. Outline • Client.java • (3 of 7)

  16. 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

  17. Invoke Socket method close to close the socket Read a String object from server Outline • Client.java • (5 of 7) • Line 117 • Line 138

  18. Use ObjectOutputStream method writeObject to send a String to server Outline • Client.java • (6 of 7) • Line 151

  19. Outline • Client.java • (7 of 7)

  20. Outline • ClientTest.java • (1 of 2) • Program output

  21. Outline • ClientTest.java • (2 of 2) • Program output

  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. Use a DatagramSocket as our server Outline • Server.java • (1 of 4) • Line 16

  24. 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

  25. 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

  26. Use method send of DatagramSocket to send the packet over the network Outline • Server.java • (4 of 4)

  27. Outline • ServerTest.java • Program ouptut Server window after packet of data is received from client

  28. Outline • Client.java • (1 of 5)

  29. 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

  30. Create a DatagramSocket for sending and receiving packets Outline • Client.java • (3 of 5) • Line 71

  31. 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

  32. Outline • Client.java • (5 of 5)

  33. Outline • ClientTest.java • Program output Client window after sending packet to Server and receiving packet back from Server

More Related