Archive for April, 2007

Web hosting reseller - 1124 Networking: Streams-Based Sockets and Datagrams Chapter 22

Thursday, April 12th, 2007

1124 Networking: Streams-Based Sockets and Datagrams Chapter 22 68 // send packet to server on port 5000 69 client.Send( data, data.Length, “localhost”, 5000 ); 70 displayTextBox.Text += “rnPacket sentrn”; 71 inputTextBox.Clear(); 72 } 73 } // end method inputTextBox_KeyDown 74 75 // wait for packets to arrive 76 public void WaitForPackets() 77 { 78 while ( true ) 79 { 80 // receive byte array from server 81 byte[] data = client.Receive( ref receivePoint ); 82 83 // output packet data to TextBox 84 displayTextBox.Text += “rnPacket received:” + 85 “rnLength: ” + data.Length + “rnContaining: ” + 86 System.Text.Encoding.ASCII.GetString( data ) + 87 “rn”; 88 } 89 90 } // end method WaitForPackets 91 92 } // end class Client Clientwindow before sending Clientwindow after sending a packet a packet to the server to the server and receiving it back Fig. 22.4 Fig. 22.FiFig. 22.4g. 22.4Client portion of connectionless client/server computing. (Part 3 of 3.) Fig. 22.4 The code in Fig. 22.3 defines the Server for this application. Line 28 in the constructor for class Servercreates an instance of the UdpClient class that receives data at port 5000. This initializes the underlying Socketfor communications. Line 29 creates an instance of class IPEndPointto hold the IP address and port number of the client(s) that transmit to Server. The first argument to the constructor of IPEndPoint is an IPAddressobject; the second argument to the constructor for IPEndPointis the port number of the endpoint. These values are both 0, because we need only instantiate an empty IPEndPoint object. The IP addresses and port numbers of clients are copied into the IPEndPointwhen datagrams are received from clients.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

X web hosting - Chapter 22 Networking: Streams-Based Sockets and Datagrams 1123

Thursday, April 12th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1123 15 // run the UDP client 16 public class Client : System.Windows.Forms.Form 17 { 18 private System.Windows.Forms.TextBox inputTextBox; 19 private System.Windows.Forms.TextBox displayTextBox; 20 21 private UdpClient client; 22 private IPEndPoint receivePoint; 23 24 private System.ComponentModel.Container components = null; 25 26 // no-argument constructor 27 public Client() 28 { 29 InitializeComponent(); 30 31 receivePoint = new IPEndPoint( new IPAddress( 0 ), 0 ); 32 client = new UdpClient( 5001 ); 33 Thread thread = 34 new Thread( new ThreadStart( WaitForPackets ) ); 35 thread.Start(); 36 } 37 38 // Visual Studio.NET generated code 39 40 [STAThread] 41 static void Main() 42 { 43 Application.Run( new Client() ); 44 } 45 46 // shut down the client 47 protected void Client_Closing( 48 object sender, CancelEventArgs e ) 49 { 50 System.Environment.Exit( System.Environment.ExitCode ); 51 } 52 53 // send a packet 54 protected void inputTextBox_KeyDown( 55 object sender, KeyEventArgs e ) 56 { 57 if ( e.KeyCode == Keys.Enter ) 58 { 59 // create packet (datagram) as string 60 string packet = inputTextBox.Text; 61 displayTextBox.Text += 62 “rnSending packet containing: ” + packet; 63 64 // convert packet to byte array 65 byte[] data = 66 System.Text.Encoding.ASCII.GetBytes( packet ); 67 Fig. 22.4 Fig. 22.FiFig. 22.4g. 22.4Client portion of connectionless client/server computing. (Part 2 of 3.) Fig. 22.4
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision best web hosting services

Yahoo free web hosting - 1122 Networking: Streams-Based Sockets and Datagrams Chapter 22

Wednesday, April 11th, 2007

1122 Networking: Streams-Based Sockets and Datagrams Chapter 22 58 59 // output packet data to TextBox 60 displayTextBox.Text += “rnPacket received:” + 61 “rnLength: ” + data.Length + “rnContaining: ” + 62 System.Text.Encoding.ASCII.GetString( data ); 63 64 displayTextBox.Text += 65 “rnrnEcho data back to client…”; 66 67 // echo information from packet back to client 68 client.Send( data, data.Length, receivePoint ); 69 displayTextBox.Text += “rnPacket sentrn”; 70 } 71 72 } // end method WaitForPackets 73 74 } // end class Server Fig. 22.3 Fig. 22.FiFig. 22.3g. 22.3Server-side portion of connectionless client/server computing. (Part 3 of 3.) Fig. 22.3 1 // Fig. 22.6: Client.cs 2 // Set up a Client that sends packets to a server and receives 3 // packets from a server. 4 5 using System; 6 using System.Drawing; 7 using System.Collections; 8 using System.ComponentModel; 9 using System.Windows.Forms; 10 using System.Data; 11 using System.Net; 12 using System.Net.Sockets; 13 using System.Threading; 14 Fig. 22.4 Fig. 22.FiFig. 22.4g. 22.4Client portion of connectionless client/server computing. (Part 1 of 3.) Fig. 22.4
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Chapter 22 Networking: Streams-Based Sockets (Web hosting companies) and Datagrams 1121

Wednesday, April 11th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1121 using System; 6 using System.Drawing; 7 using System.Collections; 8 using System.ComponentModel; 9 using System.Windows.Forms; using System.Data; 11 using System.Net; 12 using System.Net.Sockets; 13 using System.Threading; 14 // create the UDP server 16 public class Server : System.Windows.Forms.Form 17 { 18 private System.Windows.Forms.TextBox displayTextBox; 19 private UdpClient client; private IPEndPoint receivePoint; 21 private System.ComponentModel.Container components = null; 22 23 // no-argument constructor 24 public Server() { 26 InitializeComponent(); 27 28 client = new UdpClient( 5000 ); 29 receivePoint = new IPEndPoint( new IPAddress( 0 ), 0 ); Thread readThread = new Thread( 31 new ThreadStart( WaitForPackets ) ); 32 33 readThread.Start(); 34 } 36 // Visual Studio .NET generated code 37 38 [STAThread] 39 static void Main() { 41 Application.Run( new Server() ); 42 } 43 44 // shut down the server protected void Server_Closing( 46 object sender, CancelEventArgs e ) 47 { 48 System.Environment.Exit( System.Environment.ExitCode ); 49 } 51 // wait for a packet to arrive 52 public void WaitForPackets() 53 { 54 while ( true ) { 56 // receive byte array from client 57 byte[] data = client.Receive( ref receivePoint ); Fig. 22.3 Fig. 22.FiFig. 22.3g. 22.3Server-side portion of connectionless client/server computing. (Part 2 of 3.) Fig. 22.3
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

1120 Networking: Streams-Based Sockets and Datagrams Chapter 22 (Web server version)

Wednesday, April 11th, 2007

1120 Networking: Streams-Based Sockets and Datagrams Chapter 22 3). Line 110 displays the message, and lines 124 127 close the BinaryWriter, BinaryReader, NetworkStreamand TcpClientobjects (Step 4). When the user of the client application enters a stringin the TextBoxand presses the Enter key, the event handler inputTextBox_KeyDown (lines 54 74) reads the string from the TextBox and sends it via BinaryWriter method Write. Notice that, here, the Serverreceives a connection, processes it, closes it and waits for the next one. In a real-world application, a server would likely receive a connection, set up the connection to be processed as a separate thread of execution and wait for new connections. The separate threads that process existing connections can continue to execute while the Serverconcentrates on new connection requests. 22.5 Connectionless Client/Server Interaction with Datagrams Up to this point, we have discussed connection-oriented, streams-based transmission. Now, we consider connectionless transmission using datagrams. Connection-oriented transmission is similar to interaction over a telephone system, in which a user dials a number and is connected to the telephone of the party they wish to connect. The system maintains the connection for the duration of the phone call, regardless of whether the users are speaking. By contrast, connectionless transmission via datagrams more closely resembles the method by which the postal service carries and delivers mail. Connectionless transmission bundles and sends information in packets called datagrams, which can be thought of as similar to posted letters. If a large message will not fit in one envelope, that message is broken into separate message pieces and placed in separate, sequentially numbered envelopes. All the letters are mailed at once. The letters might arrive in order, out of order or not at all. The person at the receiving end reassembles the message pieces into sequential order before attempting to interpret the message. If the message is small enough to fit in one envelope, the sequencing problem is eliminated, but it is still possible that the message will never arrive. (Unlike with posted mail, duplicate of datagrams could reach receiving computers.) C# provides the UdpClient class for connectionless transmission. Like TcpListener and TcpClient, UdpClientuses methods from class Socket. The UdpClientmethods Send and Receive are used to transmit data with Socket s SendTomethod and to read data with Socket s ReceiveFrommethod, respectively. The programs in Fig. 22.3 and Fig. 22.4 use datagrams to send packets of information between a client and server applications. In the Clientapplication, the user types a message into a TextBox and presses Enter. The client converts the message to a bytearray and sends it to the server. The server receives the packet and displays the packet s information, then echoes, or returns, the packet back to the client. When the client receives the packet, the client displays the packet s information. In this example, the implementations of the Clientand Serverclasses are similar. 1 // Fig. 22.5: Server.cs 2 // Set up a Server that will receive packets from a 3 // client and send packets to a client. 4 Fig. 22.3 Fig. 22.FiFig. 22.3g. 22.3Server-side portion of connectionless client/server computing. (Part 1 of 3.) Fig. 22.3
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Chapter 22 Networking: Streams-Based Sockets and Datagrams (Web site directory) 1119

Tuesday, April 10th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1119 BinaryWriter method Write has many overloaded versions, which enable the method to write various types to a stream. (You might remember that we used these overloaded methods in Chapter 17 to write record data to files.) Line 115 uses method Writeto send to the client a string notifying the user of a successful connection. Lines 121 139 declare a do/while structure that executes until the server receives a message indicating connection termination (i.e., CLIENT>>>TERMINATE). Line 126 uses BinaryReader method ReadString to read a string from the stream (Step 4). (You might remember that we also used this method in Chapter 17 to read records first-name and last-name strings from files.) Method ReadString blocks until a string is read. To prevent the whole server from blocking, we use a separate Thread to handle the transfer of information. The while statement loops until there is more information to read this results in I/O blocking, which causes the program always to appear frozen. However, if we run this portion of the program in a separate Thread, the user can interact with the Windows Form and send messages while the program waits in the background for incoming messages. When the chat is complete, lines 146 149 close the BinaryWriter, BinaryReader, NetworkStream and Socket (Step 5) by invoking their respective Close methods. The server then waits for another client connection request by returning to the beginning of the while loop (line 97). When the user of the server application enters a string in the TextBox and presses the Enter key, event handler inputTextBox_KeyDown (lines 53 78) reads the string and sends it via method Write of class BinaryWriter. If a user terminates the server application, line 69 calls method Close of the Socket object to close the connection. Lines 46 50 define the Server_Closing event handler for the Closing event. The event closes the application and uses System.Environment.Exit method with parameter System.Environment.ExitCode to terminate all threads. Method Exit of class Environment closes all threads associated with the application. Figure 22.2 lists the code for the Client object. Like the Server object, the Client object creates a Thread (lines 35 36) in its constructor to handle all incoming messages. Client method RunClient (lines 77 137) connects to the Server, receives data from the Server and sends data to the Server (when the user presses Enter). Lines 87 88 instantiate a TcpClient object, then call its method Connect to establish a connection (Step 1). The first argument to method Connect is the name of the server in our case, the server s name is “localhost”, meaning that the server is located on the same machine as the client. The localhost is also known as the loopback IP address and is equivalent to the IP address 127.0.0.1. This value sends the data transmission back to the sender s IP address. [Note: We chose to demonstrate the client/server relationship by connecting between programs that are executing on the same computer (localhost). Normally, this argument would contain the Internet address of another computer.] The second argument to method Connect is the server port number. This number must match the port number at which the server waits for connections. The Client uses a NetworkStream to send data to and receive data from the server. The client obtains the NetworkStream on line 91 through a call to TcpClient method GetStream (Step 2). The do/while structure in lines 102 119 loops until the client receives the connection-termination message (SERVER>>>TERMINATE). Line 109 uses BinaryReader method ReadString to obtain the next message from the server (Step
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Web site domain - 1118 Networking: Streams-Based Sockets and Datagrams Chapter 22

Tuesday, April 10th, 2007

1118 Networking: Streams-Based Sockets and Datagrams Chapter 22 Fig. 22.2 Fig. 22.FiFig. 22.2g. 22.2Client portion of a client/server stream-socket connection. (Part 5 of 5.) Fig. 22.2 As we analyze this example, we begin by discussing class Server(Fig. 22.1). In the constructor, line 34 creates a Thread that will accept connections from clients. Line 35 starts the Thread, which invokes method RunServer (lines 81 160). Method Run- Serverinitializes the server to receive connection requests and process connections. Line 91 instantiates the TcpListener to listen for a connection request from a client at port 5000 (Step 1). Line 94 then calls method Start of the TcpListener object, which causes the TcpListenerto begin waiting for requests (Step 2). Lines 97 152 declare an infinite while loop that establishes connections requested by clients (Step 3). Line 102 calls method AcceptSocketof the TcpListenerobject, which returns a Socket upon successful connection. The thread in which method AcceptSocketis called stops executing until a connection is established. The Socket object will manage the connection. Line 105 passes this Socketobject as an argument to the constructor of a NetworkStream object. Class NetworkStreamprovides access to streams across a network in this example, the NetworkStream object provides access to the Socketconnection. Lines 108 109 create instances of the BinaryWriter and BinaryReader classes for writing and reading data. We pass the Network- Stream object as an argument to each constructor; BinaryWriter can write bytes to the NetworkStream, and BinaryReader can read bytes from NetworkStream. Lines 111 112 append text to the TextBox, indicating that a connection was received.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1117 (Cheapest web hosting)

Tuesday, April 10th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1117 121 displayTextBox.Text += “rnClosing connection.rn”; 122 123 // Step 4: close connection 124 writer.Close(); 125 reader.Close(); 126 output.Close(); 127 client.Close(); 128 Application.Exit(); 129 } 130 131 // handle exception if error in establishing connection 132 catch ( Exception error ) 133 { 134 MessageBox.Show( error.ToString() ); 135 } 136 137 } // end method RunClient 138 139 } // end class Client Fig. 22.2 Fig. 22.FiFig. 22.2g. 22.2Client portion of a client/server stream-socket connection. (Part 4 of 5.) Fig. 22.2
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Web hosting ratings - 1116 Networking: Streams-Based Sockets and Datagrams Chapter 22

Monday, April 9th, 2007

1116 Networking: Streams-Based Sockets and Datagrams Chapter 22 69 catch ( SocketException ioe ) 70 { 71 displayTextBox.Text += “nError writing object”; 72 } 73 74 } // end method inputTextBox_KeyDown 75 76 // connect to server and display server-generated text 77 public void RunClient() 78 { 79 TcpClient client; 80 81 // instantiate TcpClient for sending data to server 82 try 83 { 84 displayTextBox.Text += “Attempting connectionrn”; 85 86 // Step 1: create TcpClient and connect to server 87 client = new TcpClient(); 88 client.Connect( “localhost”, 5000 ); 89 90 // Step 2: get NetworkStream associated with TcpClient 91 output = client.GetStream(); 92 93 // create objects for writing and reading across stream 94 writer = new BinaryWriter( output ); 95 reader = new BinaryReader( output ); 96 97 displayTextBox.Text += “rnGot I/O streamsrn”; 98 99 inputTextBox.ReadOnly = false; 100 101 // loop until server signals termination 102 do 103 { 104 105 // Step 3: processing phase 106 try 107 { 108 // read message from server 109 message = reader.ReadString(); 110 displayTextBox.Text += “rn” + message; 111 } 112 113 // handle exception if error in reading server data 114 catch ( Exception ) 115 { 116 System.Environment.Exit( 117 System.Environment.ExitCode ); 118 } 119 } while( message != “SERVER>>> TERMINATE” ); 120 Fig. 22.2 Fig. 22.FiFig. 22.2g. 22.2Client portion of a client/server stream-socket connection. (Part 3 of 5.) Fig. 22.2
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Chapter 22 Networking: Streams-Based Sockets and Datagrams (Web hosting reviews) 1115

Monday, April 9th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1115 18 private System.Windows.Forms.TextBox displayTextBox; 19 20 private NetworkStream output; 21 private BinaryWriter writer; 22 private BinaryReader reader; 23 24 private string message = “”; 25 26 private Thread readThread; 27 28 private System.ComponentModel.Container components = null; 29 30 // default constructor 31 public Client() 32 { 33 InitializeComponent(); 34 35 readThread = new Thread( new ThreadStart( RunClient ) ); 36 readThread.Start(); 37 } 38 39 // Visual Studio .NET-generated code 40 41 [STAThread] 42 static void Main() 43 { 44 Application.Run( new Client() ); 45 } 46 47 protected void Client_Closing( 48 object sender, CancelEventArgs e ) 49 { 50 System.Environment.Exit( System.Environment.ExitCode ); 51 } 52 53 // sends text the user typed to server 54 protected void inputTextBox_KeyDown ( 55 object sender, KeyEventArgs e ) 56 { 57 try 58 { 59 if ( e.KeyCode == Keys.Enter ) 60 { 61 writer.Write( “CLIENT>>> ” + inputTextBox.Text ); 62 63 displayTextBox.Text += 64 “rnCLIENT>>> ” + inputTextBox.Text; 65 66 inputTextBox.Clear(); 67 } 68 } Fig. 22.2 Fig. 22.FiFig. 22.2g. 22.2Client portion of a client/server stream-socket connection. (Part 2 of 5.) Fig. 22.2
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services