Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 23 Networking: Streams-Based Sockets and Datagrams.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 23 Networking: Streams-Based Sockets and Datagrams."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 23 Networking: Streams-Based Sockets and Datagrams

2  2006 Pearson Education, Inc. All rights reserved. 2 If the presence of electricity can be made visible in any part of a circuit, I see no reason why intelligence may not be transmitted instantaneously by electricity. — Samuel F. B. Morse Protocol is everything. — Francois Giuliani

3  2006 Pearson Education, Inc. All rights reserved. 3 The port is near, the bells I hear, the people all exulting. — Walt Whitman What networks of railroads, highways and canals were in another age, the networks of telecommunications, information and computerization are today. — Bruno Kreisky

4  2006 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  To implement networking applications that use sockets and datagrams.  To implement clients and servers that communicate with one another.  To implement network-based collaborative applications.  To construct a multithreaded server.  To use the WebBrowser control to add Web browsing capabilities to any application.  To use.NET remoting to enable an application executing on one computer to invoke methods from an application executing on a different computer.

5  2006 Pearson Education, Inc. All rights reserved. 5 23.1Introduction 23.2Connection-Oriented vs. Connectionless Communication 23.3Protocols for Transporting Data 23.4Establishing a Simple TCP Server (Using Stream Sockets) 23.5Establishing a Simple TCP Client (Using Stream Sockets) 23.6Client/Server Interaction with Stream-Socket Connections 23.7Connectionless Client/Server Interaction with Datagrams 23.8Client/Server Tic-Tac-Toe Using a Multithreaded Server 23.9 WebBrowser Control 23.10.NET Remoting 23.11Wrap-Up

6  2006 Pearson Education, Inc. All rights reserved. 6 23.1 Introduction The.NET FCL provides a number of built-in networking capabilities -Makes it easy to develop Internet- and Web-based applications Provide overview of the communication techniques and technologies used to transmit data over the Internet Discuss basic concepts of establishing a connection between two applications using streams of data -Enables programs to communicate with one another as easily as writing to and reading from files on disk Discuss connectionless techniques for transmitting data between applications that is less reliable but much more efficient

7  2006 Pearson Education, Inc. All rights reserved. 7 23.2 Connection-Oriented vs. Connectionless Communication Two primary approaches to communicating between applications -Connection oriented communications Similar to the telephone system -A connection is established and held for the length of the session Handshaking -Computers send each other control information Through a technique called initiate an end-to-end connection Data is sent in packets -Contain pieces of the data along with information that helps the Internet route the packets to the proper destination Ensures reliable communications on unreliable networks -Guarantee that sent packets will arrive at the intended receiver undamaged and be reassembled in the correct sequence The Internet does not guarantee anything about the packets sent

8  2006 Pearson Education, Inc. All rights reserved. 8 23.2 Connection-Oriented vs. Connectionless Communication (Cont.) -Connectionless communications Similar to the postal service: -Two letters mailed from the same place and to the same destination may take two dramatically different paths through the system and even arrive at different times, or not at all The two computers do not handshake before transmission -Reliability is not guaranteed Data sent may never reach the intended recipient Avoids the overhead associated with handshaking and enforcing reliability -Less information often needs to be passed between the hosts

9  2006 Pearson Education, Inc. All rights reserved. 9 23.3 Protocols for Transporting Data Protocols -Sets of rules that govern how two entities should interact -Networking capabilities are defined in the System.Net.Sockets namespace Transmission Control Protocol (TCP) User Datagram Protocol (UDP) -TCP A connection-oriented communication protocol which guarantees that sent packets will arrive at the intended receiver undamaged and in the correct sequence -If packets are lost, TCP ensures that the packets are sent again -If the packets arrive out of order, TCP reassembles them in the correct order transparently to the receiving application -If duplicate packets arrive, TCP discards them Send information across a network as simply and reliably as writing to a file on a local computer

10  2006 Pearson Education, Inc. All rights reserved. 10 23.3 Protocols for Transporting Data (Cont.) -UDP Incurs the minimum overhead necessary to communicate between applications -Do not need to carry the information that TCP packets carry to ensure reliability -Reduces network traffic relative to TCP due to the absence of handshaking, retransmissions, etc Makes no guarantees that packets, called datagrams, will reach their destination or arrive in their original order

11  2006 Pearson Education, Inc. All rights reserved. 11 23.4 Establishing a Simple TCP Client (Using Stream Sockets) Typically with TCP, a server waits for a connection request -Contains a control statement or block of code that executes until the server receives the request -On receiving a request, the server establishes a connection to the client Socket object -Manages the connection between server and client

12  2006 Pearson Education, Inc. All rights reserved. 12 23.4 Establishing a Simple TCP Client (Using Stream Sockets) (Cont.) Establish a simple server with TCP and stream sockets requires 5 steps (Uses namespace System.Net.Sockets ) -Step 1 : Create an object of class TcpListener of namespace Using an IP Address and port number -Step 2 : Call TcpListener ’s Start method Causes the TcpListener object to begin listening for connection requests An object of class Socket manages a connection to client Method AcceptSocket of class TcpListener accepts a connection request -Step 3 : Establishes the streams used for communication with the client Create a NetworkStream object that uses the Socket object -Represent the connection to perform the sending and receiving of data -Step 4 : The server and client communicate using the connection established in the third step -Step 5 : Terminate connections Server calls method Close of the BinaryReader, BinaryWriter, NetworkStream and Socket to terminate the connection

13  2006 Pearson Education, Inc. All rights reserved. 13 Software Engineering Observation 23.1 Port numbers can have values between 0 and 65535. Many operating systems reserve port numbers below 1024 for system services (such as e-mail and Web servers). Applications must be granted special privileges to use these reserved port numbers.

14  2006 Pearson Education, Inc. All rights reserved. 14 Software Engineering Observation 23.2 Multithreaded servers can efficiently manage simultaneous connections with multiple clients. This architecture is precisely what popular UNIX and Windows network servers use.

15  2006 Pearson Education, Inc. All rights reserved. 15 Software Engineering Observation 23.3 A multithreaded server can be implemented to create a thread that manages network I/O across a Socket object returned by method AcceptSocket. A multithreaded server also can be implemented to maintain a pool of threads that manage network I/O across newly created Socket s.

16  2006 Pearson Education, Inc. All rights reserved. 16 Performance Tip 23.1 In high-performance systems with abundant memory, a multithreaded server can be implemented to create a pool of threads. These threads can be assigned quickly to handle network I/O across multiple Socket s. Thus, when a connection is received, the server does not incur the overhead of thread creation.

17  2006 Pearson Education, Inc. All rights reserved. 17 23.5 Establishing a Simple TCP Client (Using Stream Sockets) There are 4 steps to creating a simple TCP client: -Step 1: Create an object of class TcpClient and connect to the server using method Connect If the connection is successful, returns a positive integer -Otherwise, it returns 0 -Step 2: The TcpClient uses its GetStream method to get a NetworkStream so that it can write to and read from the server Use the NetworkStream object to create a BinaryWriter and a BinaryReader that will be used to send information to and receive information from the server -Step 3: The client and the server communicate -Step 4: Require the client to close the connection by calling method Close For BinaryReader, BinaryWriter, NetworkStream and TcpClient

18  2006 Pearson Education, Inc. All rights reserved. 18 23.6 Client/Server Interaction with Stream-Socket Connections Windows Form controls are not thread safe -A control that is modified from multiple threads is not guaranteed to be modified correctly The Visual Studio 2005 Documentation recommends that only the thread which created the GUI should modify the controls -Class Control ’s method Invoke to help ensure this Localhost -A.K.A. the loopback IP address -Equivalent to the IP address 127.0.0.1

19  2006 Pearson Education, Inc. All rights reserved. 19 Outline ChatServer.cs (1 of 7) Using namespaces for networking capabilities Create private instance variables for networking purposes Create a Thread that will accept connections from clients Starts the Thread

20  2006 Pearson Education, Inc. All rights reserved. 20 Outline ChatServer.cs (2 of 7) Terminate program and close its threads Represents methods that take a string argument and do not return a value Returns true if the current thread is not allowed to modify this control directly Pass a new DisplayDelegate representing the method DisplayMessage and a new object array consisting of the argument message Appends message to displayTextBox

21  2006 Pearson Education, Inc. All rights reserved. 21 Outline ChatServer.cs (3 of 7) Delegate for DisableInput Set inputTextBox.ReadOnly to the value of the boolean argument Returns true if the current thread is not allowed to modify this control directly Pass a new DisableInputDelegate representing the method DisableInput and a new object array consisting of the argument value

22  2006 Pearson Education, Inc. All rights reserved. 22 Outline ChatServer.cs (4 of 7) Sends message via method Write of class BinaryWriter Calls method Close of the Socket object to close the connection

23  2006 Pearson Education, Inc. All rights reserved. 23 Outline ChatServer.cs (5 of 7) Create instance of TcpListener Instantiate the TcpListener object to listen for a connection request from a client at port 50000 Causes the TcpListener to begin waiting for request Calls method AcceptSocket of the TcpListener object, which returns a Socket upon successful connection Passes the Socket object as an argument to the constructor of a NetworkStream object

24  2006 Pearson Education, Inc. All rights reserved. 24 Outline ChatServer.cs (6 of 7) Create instances of BinaryWriter and BinaryReader classes for writing and reading data Send to the client a string notifying the user of a successful connection Read a string from the stream

25  2006 Pearson Education, Inc. All rights reserved. 25 Outline ChatServer.cs (7 of 7) Release program’s resources by closing its connections

26  2006 Pearson Education, Inc. All rights reserved. 26 Outline ChatClient.cs (1 of 9) Using namespaces for networking capabilities Create private instance variables for networking purposes Create a Thread to handle all incoming messages Starts the Thread

27  2006 Pearson Education, Inc. All rights reserved. 27 Outline ChatClient.cs (2 of 9) Terminate program and close its threads Represents methods that take a string argument and do not return a value Returns true if the current thread is not allowed to modify this control directly Pass a new DisplayDelegate representing the method DisplayMessage and a new object array consisting of the argument message Appends message to displayTextBox

28  2006 Pearson Education, Inc. All rights reserved. 28 Outline ChatClient.cs (3 of 9) Delegate for DisableInput Set inputTextBox.ReadOnly to the value of the boolean argument Returns true if the current thread is not allowed to modify this control directly Pass a new DisableInputDelegate representing the method DisableInput and a new object array consisting of the argument value

29  2006 Pearson Education, Inc. All rights reserved. 29 Outline ChatClient.cs (4 of 9) Sends message via method Write of class BinaryWriter

30  2006 Pearson Education, Inc. All rights reserved. 30 Outline ChatClient.cs (5 of 9) Create instance of TcpClient Instantiate the TcpClient, then call its Connect method to establish a connection Retrieve the NetworkStream object that send data to and receive data from the server Create instances of BinaryWriter and BinaryReader classes for writing and reading data

31  2006 Pearson Education, Inc. All rights reserved. 31 Outline ChatClient.cs (6 of 9) Read and display the message Release program’s resources by closing its connections

32  2006 Pearson Education, Inc. All rights reserved. 32 Outline ChatClient.cs (7 of 9) (a)(b)

33  2006 Pearson Education, Inc. All rights reserved. 33 Outline ChatClient.cs (8 of 9) (f) (e) (d) (c)

34  2006 Pearson Education, Inc. All rights reserved. 34 Outline ChatClient.cs (9 of 9) (g)

35  2006 Pearson Education, Inc. All rights reserved. 35 23.7 Connectionless Client/Server Interaction with Datagrams Connectionless transmission -Bundles and sends information in packets called datagrams -Class UdpClient For connectionless transmission Methods Send and Receive -Transmit data with Socket ’s SendTo method -Read data with Socket ’s ReceiveFrom method -Class IPEndPoint Hold the IP address and port number of the client(s)

36  2006 Pearson Education, Inc. All rights reserved. 36 Outline PacketServer.cs (1 of 3) Using namespaces for networking capabilities Create private instance variables of class UdpClient and IPEndPoint Receives data at port 50000 Hold the IP address and port number of the clients that transit to PacketServerForm Create a Thread to handle packages Starts the Thread

37  2006 Pearson Education, Inc. All rights reserved. 37 Outline PacketServer.cs (2 of 3) Terminate program and close its threads

38  2006 Pearson Education, Inc. All rights reserved. 38 Outline PacketServer.cs (3 of 3) Receives a byte array from the client Return the string of the byte array Echos the data back to the client using UdpClient ’s method Send

39  2006 Pearson Education, Inc. All rights reserved. 39 Outline PacketClient.cs (1 of 4) Using namespaces for networking capabilities Create private instance variables of class UdpClient and IPEndPoint Hold the IP address and port number of the clients that transit to PacketClientForm Create a Thread to handle packages Starts the Thread Receives data at port 50001

40  2006 Pearson Education, Inc. All rights reserved. 40 Outline PacketClient.cs (2 of 4) Terminate program and close its threads

41  2006 Pearson Education, Inc. All rights reserved. 41 Outline PacketClient.cs (3 of 4) Converts the string that the user entered to a byte array Send the byte array to the PacketServerForm that is located on the localhost

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline PacketClient.cs (4 of 4) (b) Packet Client window after sending a packet to the server and receiving it back (a) Packet Client window before sending a packet to the server Receives a byte array from the server Return the string of the byte array Display its contents in the TextBox

43  2006 Pearson Education, Inc. All rights reserved. 43 23.8 Client/Server Tic-Tac-Toe Using a Multithreaded Server Multithreaded servers can manage many simultaneous connections with multiple clients

44  2006 Pearson Education, Inc. All rights reserved. 44 Outline TicTacToeServer.cs (1 of 10) Using namespaces for networking capabilities Create instance variables for implementing the server side of the Tic-Tac-Toe networking game

45  2006 Pearson Education, Inc. All rights reserved. 45 Outline TicTacToeServer.cs (2 of 10) Store the moves the players have made Arrays referencing Player and Thread objects Keep track of the current player Create and start Thread to accept connections so that the current thread does not block while awaiting players Notify and terminate program and close its associated threads

46  2006 Pearson Education, Inc. All rights reserved. 46 Outline TicTacToeServer.cs (3 of 10) Creates a TcpListerner object to listen for requests on port 50000

47  2006 Pearson Education, Inc. All rights reserved. 47 Outline TicTacToeServer.cs (4 of 10) Instantiate Player objects representing players Creates two Thread s that execute the Run methods of each Player object Notify and unsuspend the first Player since other Player has connected Allow only one move to be attempted at a time Wait if it’s not the player’s turn

48  2006 Pearson Education, Inc. All rights reserved. 48 Outline TicTacToeServer.cs (5 of 10) Checks to see if the user have selected an unoccupied space Place the mark on the local representation of the board Determine and set the currentPlayer to the appropriate value Notifies the other player that a move has been made Invokes the Pulse method so that the waiting Player can validate a move

49  2006 Pearson Education, Inc. All rights reserved. 49 Outline TicTacToeServer.cs (6 of 10)

50  2006 Pearson Education, Inc. All rights reserved. 50 Outline TicTacToeServer.cs (7 of 10) Create instance variables for implementing the player of the Tic-Tac-Toe networking game Instantiate the NetworkStream object by passing the Socket object into the constructor Instantiate the BinaryWriter and BinaryReader objects by passing the NetworkStream object into the constructor

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline TicTacToeServer.cs (8 of 10) Notify player that the opponent has moved via method Write of BinaryWriter Notify the server of a successful connection and send to the client the char that the client will place on the board when making a move

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline TicTacToeServer.cs (9 of 10) Notify player X of the current situation Suspends the Player “X” thread until the server signals that Player O has connected Loops until Socket property Available indicates that there is information to receive from the Socket If there is no information, Thread goes to sleep for one second Checks whether server is disconnected

53  2006 Pearson Education, Inc. All rights reserved. 53 Outline TicTacToeServer.cs (10 of 10) Read in an int representing the location in which the client wants to place a mark Validates the player’s move Determine if the game is over Release program’s resources by closing its connections

54  2006 Pearson Education, Inc. All rights reserved. 54 Outline TicTacToeClient.cs (1 of 13) Using namespaces for networking capabilities Create instance variables for implementing the client side of the Tic-Tac-Toe networking game

55  2006 Pearson Education, Inc. All rights reserved. 55 Outline TicTacToeClient.cs (2 of 13) Initialize the Tic-Tac-Toe board Create a black SolidBrush object for coloring the “X”s and “O”s Instantiate the instance variables

56  2006 Pearson Education, Inc. All rights reserved. 56 Outline TicTacToeClient.cs (3 of 13) Create and start Thread to read messages sent from the server to the client Notify and terminate program and close its associated threads Repaint the squares by calling PaintSquare

57  2006 Pearson Education, Inc. All rights reserved. 57 Outline TicTacToeClient.cs (4 of 13)

58  2006 Pearson Education, Inc. All rights reserved. 58 Outline TicTacToeClient.cs (5 of 13)

59  2006 Pearson Education, Inc. All rights reserved. 59 Outline TicTacToeClient.cs (6 of 13) Create an instance of Graphics for repainting Retrieve and reference the Graphics object for each Panel Mark the appropriate square

60  2006 Pearson Education, Inc. All rights reserved. 60 Outline TicTacToeClient.cs (7 of 13) Determine and update which square was clicked Notify the server of the clicked square Get and output the player’s mark

61  2006 Pearson Education, Inc. All rights reserved. 61 Outline TicTacToeClient.cs (8 of 13) Invoke ProcessMessage with the return value of ReadString of the BinaryReader object as the argument If the message indicates that a move is valid, the client sets its Mark to the current square and repaints the board

62  2006 Pearson Education, Inc. All rights reserved. 62 Outline TicTacToeClient.cs (9 of 13) If the message indicates that a move is invalid, the client notifies the user to click a different square If the message indicates that the opponent made a move, read an int from the server specifying where on the board the client should place the opponent’s Mark Update the player’s turn Display the read message

63  2006 Pearson Education, Inc. All rights reserved. 63 Outline TicTacToeClient.cs (10 of 13) Determine which player’s turn it is Update the player’s turn Notify the location of the move via BinaryWriter ’s method Write

64  2006 Pearson Education, Inc. All rights reserved. 64 Outline TicTacToeClient.cs (11 of 13) At the start of the game. After Player X makes the first move. (a) (b) (c) (d)

65  2006 Pearson Education, Inc. All rights reserved. 65 Outline TicTacToeClient.cs (12 of 13) After Player O makes the second move. After Player X makes the final move. (e) (f) (g) (h)

66  2006 Pearson Education, Inc. All rights reserved. 66 Outline TicTacToeClient.cs (13 of 13) server output after (a,b) server output after (c,d) server output after (e,f) server output after (g,h) The Tie Tac Toe Server’s output from the client interactions (i)

67  2006 Pearson Education, Inc. All rights reserved. 67 Outline Square.cs (1 of 2) Represents a square in the Tic-Tac-Toe game

68  2006 Pearson Education, Inc. All rights reserved. 68 Outline Square.cs (2 of 2)

69  2006 Pearson Education, Inc. All rights reserved. 69 23.9 WebBrowser Control WebBrowser control -Enables applications to incorporate Web browsing capabilities -Generates events as the user interacts with the content displayed in the control Applications can respond to these events -Properties CanGoBack and CanGoForward Determine whether the back and forward buttons are enabled

70  2006 Pearson Education, Inc. All rights reserved. 70 23.9 WebBrowser Control (Cont.) WebBrowser methods -Method GoBack Causes the control to navigate back to the previous page in the navigation history -Method GoForward Causes the control to navigate forward to the next page in the navigation history - Method Stop Causes the control to stop loading the current page -Method Refresh Causes the control to reload the current page - Method GoHome Causes the control to navigate to the user’s home page, as defined under Internet Explorer’s settings -Method Navigate Retrieve the document at the specified URL

71  2006 Pearson Education, Inc. All rights reserved. 71 23.9 WebBrowser Control (Cont.) WebBrowser ’s Events - Navigating event Occurs when the WebBrowser starts loading a new page - StatusTextChanged event Occurs when the WebBrowser ’s StatusText property changes - ProgressChanged event Occurs when the WebBrowser control’s page-loading progress is updated - DocumentCompleted event Occurs when the WebBrowser finishes loading a document - DocumentTitleChanged event Occurs when a new document is loaded in the WebBrowser control

72  2006 Pearson Education, Inc. All rights reserved. 72 Outline Browser.cs (1 of 4) Navigate back to the previous page in the navigation history Navigate forward to the next page in the navigation history Stop loading the current page

73  2006 Pearson Education, Inc. All rights reserved. 73 Outline Browser.cs (2 of 4) Reload the current page Navigate to the user’s homepage Navigate to the user specified URL Event occurs when loading a new page Enable the stop button

74  2006 Pearson Education, Inc. All rights reserved. 74 Outline Browser.cs (3 of 4) Event occurs when the StatusText property changes Assigns the new contents of the control’s StatusText property to statusTextBox’s Text property Event occurs when the page-loading progress is updated Update pageProgressBar ’s Value to reflect how much of the current document has been loaded Event occurs when finish loading a document Update to show the URL of currently loaded page Determine whether the back and forward buttons should be enabled or disabled Disable the stop button

75  2006 Pearson Education, Inc. All rights reserved. 75 Outline Browser.cs (4 of 4) Indicates that no content is currently being loaded Event occurs when a new document is loaded Sets the BrowserForm ’s Text property to the WebBrowser ’s current DocumentTitle

76  2006 Pearson Education, Inc. All rights reserved. 76 23.10.NET Remoting.NET Remoting - System.Runtime.Remoting -A distributed computing technology -Allows a program to access objects on another machine over a network -For.NET remoting, both must be written in.NET languages -Marshaling the objects A client and a server can communicate via method calls and objects can be transmitted between applications

77  2006 Pearson Education, Inc. All rights reserved. 77 23.10.NET Remoting (Cont.) Channels -The client and the server are able to communicate with one another through channels -The client and the server each create a channel Both channels must use the same protocol to communicate with one another -HTTP channel Firewall usually permit HTTP connections by default -TCP channel Better performance than an HTTP channel Firewalls normally block unfamiliar TCP connections - System.Runtime.Remoting.Channels - System.Runtime.Remoting.Channels.Http

78  2006 Pearson Education, Inc. All rights reserved. 78 23.10.NET Remoting (Cont.) Marshalling -Marshaling by value Requires that the object be serializable -Marshaling by reference Requires that the object’s class extend class MarshalByRefObject of namespace System The object itself is not transmitted -Instead, two proxy objects are created Transparent proxy - Provides all the public services of a remote object - Calls the Invoke method of the real proxy Real proxy

79  2006 Pearson Education, Inc. All rights reserved. 79 Outline CityWeather.cs (1 of 3) The class will be published in the Weather.dll class library file Indicates that an object of this class can be marshaled by value Implements interface IComparable so that the ArrayList of CityWeather objects can be sorted alphabetically

80  2006 Pearson Education, Inc. All rights reserved. 80 Outline CityWeather.cs (2 of 3)

81  2006 Pearson Education, Inc. All rights reserved. 81 Outline CityWeather.cs (3 of 3) Must declare CompareTo since implemented IComparable Update and override ToString

82  2006 Pearson Education, Inc. All rights reserved. 82 Outline Report.cs Interface will also be included in the Weather.dll class library Interface include read-only property Reports of type ArrayList

83  2006 Pearson Education, Inc. All rights reserved. 83 Outline ReportInfo.cs (1 of 4) Using Weather.dll class library Extends MarshalByRefObject and implements Report Declare data structure to hold information Interact with a data source specified by a URL Return a stream that the program can use to read data containing the weather information from the URL Store separators Read the HTML markup one line at a time Call ReadCities to read the first batch of cities

84  2006 Pearson Education, Inc. All rights reserved. 84 Outline ReportInfo.cs (2 of 4) Read the HTML markup one line at a time Call ReadCities to read the second batch of cities Sort the CityWeather objects in alphabetical order by city name The header for the daytime/nighttime information

85  2006 Pearson Education, Inc. All rights reserved. 85 Outline ReportInfo.cs (3 of 4) Continue to read the page one line at a time until it finds the header line Reads the next line from the Web page, which is the first line containing temperature information Create a new CityWeather object to represent the current city Store the CityWeather object in the ArrayList Continue by reading the next city’s information

86  2006 Pearson Education, Inc. All rights reserved. 86 Outline ReportInfo.cs (4 of 4) Declare read-only property Reports to satisfy the Report interface

87  2006 Pearson Education, Inc. All rights reserved. 87 23.10.NET Remoting (Cont.) Class WebClient -Interact with a data source that is specified by a URL -Method OpenRead Returns a Stream that the program can use to read data from the specified URL Remote class registered as Singleton -One remote object will be created when the first client requests that remote class The remote object will service all clients Remote class registered as SingleCall -One remote object is created for each individual remote method call to the remote class

88  2006 Pearson Education, Inc. All rights reserved. 88 Outline WeatherServer.cs Using namespaces for remoting capabilities Register an HTTP channel on the current machine at port 50000 Indicates that we do not wish to enable security Register the ReportInfo class type at the “Report” URI as a Singleton remote class

89  2006 Pearson Education, Inc. All rights reserved. 89 Outline WeatherClient.cs (1 of 5) Using namespaces for remoting capabilities Create a HTTP channel without specifying a port number Register the channel on the client computer Connects to the server and returns a reference to the proxy for the Report object

90  2006 Pearson Education, Inc. All rights reserved. 90 Outline WeatherClient.cs (2 of 5) Retrieve the ArrayList of CityWeather objects generated by the ReportInfo constructor Create and place an array of Label so they can be access programmatically to display weather information

91  2006 Pearson Education, Inc. All rights reserved. 91 Outline WeatherClient.cs (3 of 5) Store pairs of weather conditions and the names for images associate with those conditions

92  2006 Pearson Education, Inc. All rights reserved. 92 Outline WeatherClient.cs (4 of 5) Retrieve the Label that will display the weather information for the next city Uses ArrayList cities to retrieve the CityWeather object that contains the weather information for the city Set the Label ’s image to the PNG image from the Hashtable Display the city’s name and high/low temperatures

93  2006 Pearson Education, Inc. All rights reserved. 93 Outline WeatherClient.cs (5 of 5)


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 23 Networking: Streams-Based Sockets and Datagrams."

Similar presentations


Ads by Google