• /  20
  • 下載費用: 10.00積分  

Java語言程序設計10E LiangPPT習題案例31slide.ppt

'Java語言程序設計10E LiangPPT習題案例31slide.ppt'
*Chapter 31 Networking*ObjectivesTo explain terms: TCP, IP, domain name, domain name server, stream-based communications, and packet-based communications (§31.2).To create servers using server sockets (§31.2.1) and clients using client sockets (§31.2.2).To implement Java networking programs using stream sockets (§31.2.3).To develop an example of a client/server application (§31.2.4).To obtain Internet addresses using the InetAddress class (§31.3).To develop servers for multiple clients (§31.4).To send and receive objects on a network (§31.5).To develop an interactive tic-tac-toe game played on the Internet (§31.6).*Client/Server CommunicationsThe server must be running when a client starts. The server waits for a connection request from a client. To establish a server, you need to create a server socket and attach it to a port, which is where the server listens for connections.After a server socket is created, the server can use this statement to listen for connections.The client issues this statement to request a connection to a server.After the server accepts the connection, communication between server and client is conducted the same as for I/O streams. *Data Transmission through Sockets InputStream input = socket.getInputStream();OutputStream output = socket.getOutputStream();*A Client/Server ExampleProblem: Write a client to send data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. In this example, the data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle. *A Client/Server Example, cont.*A Client/Server Example, cont.Server CodeClient CodeNote: Start the server, then the client.Start ServerStart Client*The InetAddress Class Occasionally, you would like to know who is connecting to the server. You can use the InetAddress class to find the client's host name and IP address. The InetAddress class models an IP address. You can use the statement shown below to create an instance of InetAddress for the client on a socket. InetAddress inetAddress = socket.getInetAddress(); Next, you can display the client's host name and IP address, as follows: System.out.println("Client's host name is " + inetAddress.getHostName());System.out.println("Client's IP Address is " + inetAddress.getHostAddress());*Serving Multiple Clients Multiple clients are quite often connected to a single server at the same time. Typically, a server runs constantly on a server computer, and clients from all over the Internet may want to connect to it. You can use threads to handle the server's multiple clients simultaneously. Simply create a thread for each connection. Here is how the server handles the establishment of a connection:while (true) { Socket socket = serverSocket.accept(); Thread thread = new ThreadClass(socket); thread.start();}The server socket can have ma。省略部分。in Network ProgramsWrite a program that collects student information from a client and send them to a server. Passing student information in an object.Student SeverStudent ClassStart ServerNote: Start the server first, then the client.Start ClientStudent Client*Case Studies: Distributed TicTacToe GamesTicTacToeServerRun ServerTicTacToeClientRun ClientOptional*Distributed TicTacToe, cont.*Distributed TicTacToe Game*Stream Socket vs. Datagram Socket A dedicated point-to-point channel between a client and server.Use TCP (Transmission Control Protocol) for data transmission. Lossless and reliable. Sent and received in the same order.No dedicated point-to-point channel between a client and server.Use UDP (User Datagram Protocol) for data transmission. May lose data and not 100% reliable. Data may not received in the same order as sent.Stream socketDatagram socketCompanion Website*DatagramPacket The DatagramPacket class represents a datagram packet. Datagram packets are used to implement a connectionless packet delivery service. Each message is routed from one machine to another based solely on information contained within the packet.Companion Website*DatagramSocket The DatagramSocket class represents a socket for sending and receiving datagram packets. A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order. To create a server DatagramSocket, use the constructor DatagramSocket(int port), which binds the socket with the specified port on the local host machine.To create a client DatagramSocket, use the constructor DatagramSocket(), which binds the socket with any available port on the local host machine. DatagramSocket Create a server DatagramSocketCreate a client DatagramSocketCompanion Website*Sending and Receiving a DatagramSocket To send data, you need to create a packet, fill in the contents, specify the Internet address and port number for the receiver, and invoke the send(packet) method on a DatagramSocket.To receive data, create an empty packet and invoke the receive(packet) method on a DatagramSocket.Sending ReceivingCompanion Website*Datagram Programming Datagram programming is different from stream socket programming in the sense that there is no concept of a ServerSocket for datagrams. Both client and server use DatagramSocket to send and receive packets.Designate one a serverCompanion Website*Example: A Client/Server ExampleSection 31.2 presents a client program and a server program using socket streams. The client sends radius to a server. The server receives the data, uses them to find the area, and then sends the area to the client. Rewrite the program using datagram sockets.Server CodeClient CodeNote: Start the server, then the client.Start ServerStart ClientCompanion Website
關 鍵 詞:
liangppt 習題 10 10e 案例 31slide 程序設計 語言 31 java
 天天文庫所有資源均是用戶自行上傳分享,僅供網友學習交流,未經上傳用戶書面授權,請勿作他用。
關于本文
本文標題:Java語言程序設計10E LiangPPT習題案例31slide.ppt
鏈接地址: http://www.476824.live/p-51617558.html
關于我們 - 網站聲明 - 網站地圖 - 資源地圖 - 友情鏈接 - 網站客服點擊這里,給天天文庫發消息,QQ:1290478887 - 聯系我們

本站為“文檔C2C交易模式”,即用戶上傳的文檔直接賣給(下載)用戶,本站只是中間服務平臺,本站所有文檔下載所得的收益歸上傳人(含作者)所有【成交的100%(原創)】。本站是網絡服務平臺方,若您的權利被侵害,侵權客服QQ:1290478887 歡迎舉報。

[email protected] 2017-2027 http://www.476824.live 網站版權所有

粵ICP備19057495號 

收起
展開
球探网即时蓝球比分 1分快3计划预测 股票春节开盘时间 青海体彩十一选五怎么玩 快乐双彩今晚开奖结果双色球 重庆彩票幸运农场走势图 在线配资平台咨询天牛宝优越 甘肃11选五前三直遗漏 江西十一选五预测推荐 辽宁十一选五走势手机版 上证指数股票