Archive for April, 2007

1144 Networking: Streams-Based Sockets and Datagrams Chapter 22 (My web site)

Saturday, April 21st, 2007

1144 Networking: Streams-Based Sockets and Datagrams Chapter 22 22.8 (Networked Morse Code) Perhaps the most famous of all coding schemes is the Morse code, developed by Samuel Morse in 1832 for use with the telegraph system. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Other representations of dots and dashes are used with lightoriented systems and signal-flag systems. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented system, a space is indicated by a short period of time during which no sound is transmitted. The international version of the Morse code appears in Fig. 22.8. Write an application that reads an English-language phrase and encodes the phrase into Morse code. Also, write a program that reads a phrase in Morse code and converts the phrase into the Englishlanguage equivalent. Use one blank between each Morse-coded letter and three blanks between each Morse-coded word. Then, enable these two applications to send Morse Code messages to each other through a multithreaded-server application. Each application should allow the user to type normal characters into a TextBox. The application should then translate the characters into Morse Code and send the coded message through the server to the other client. When messages are received, they should be decoded and displayed as normal characters and as Morse Code. The application should have two TextBoxes: One for displaying the other client s messages, and one for typing. Character Code Character Code A -T - B - U - C - - V - D - W — E X - - F - Y - — G — Z — H I Digits J —1 —- K - -2 — L - 3 — M –4 - N - 5 O —6 - P — 7 — Q — -8 — R - 9 —- S 0 —– Fig. 22.8 English letters of the alphabet and decimal digits as expressed in international Morse code.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1143 (Apache web server for windows)

Saturday, April 21st, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1143 ANSWERS TO SELF-REVIEW EXERCISES 22.1 a) False. UDP is a connectionless protocol, and TCP is a connection-oriented protocol. b) True. c) False. Packets can be lost, arrive out of order or even be duplicated. d) True. e) False. TcpListener AcceptSocket may be called as often as necessary each call will accept a new connection. f) False. A TcpListenercan listen for connections at only one port at a time. g) False. A UdpClient can send information to any port represented by an IPEndPoint. h) False. Packets may be sent more than once, to make it more likely that at least one copy of each packet arrives. i) True. 22.2 a) System.Net, System.Net.Sockets. b) UdpClient. c) IPAddress. d) stream, datagram. e) Transmission Control Protocol. f) TcpListener. g) TcpClient. h) Network- Stream. EXERCISES 22.3 Use a socket connection to allow a client to specify a file name and have the server send the contents of the file or indicate that the file does not exist. Allow the client to modify the file contents and to send the file back to the server for storage. 22.4 Multithreaded servers are quite popular today, especially because of the increasing use of multiprocessing servers (i.e., servers with more than one processor unit). Modify the simple server application presented in Section 22.4 to be a multithreaded server. Then, use several client applications and have each of them connect to the server simultaneously. 22.5 Create a client/server application for the game of Hangman, using socket connections. The server should randomly pick a word or phrase from a file or a database. After connecting, the client should be allowed to begin guessing. If a client guesses incorrectly five times, the game is over. Display the original phrase or word on the server. Display underscores (for letters that have not been guessed yet) and the letters that have been guessed in the word or phrase on the client. 22.6 Modify the previous exercise to be a connectionless game using datagrams. 22.7 (Modifications to the Multithreaded Tic-Tac-Toe Program) The programs of Fig. 22.5 Fig. 22.7 implement a multithreaded, client/server version of the game Tic-Tac-Toe. Our goal in developing this game was to demonstrate a multithreaded server that could process multiple connections from clients at the same time. The server in the example is really a mediator between the two clients it makes sure that each move is valid and that each client moves in the proper order. The server does not determine who won or lost or whether there was a draw. Also, there is no capability to allow a new game to be played or to terminate an existing game. The following is a list of suggested modifications to the multithreaded Tic-Tac-Toe application: a) Modify class Server to test for a win, loss or draw on each move in the game. When the game is over, send a message to each client that indicates the result of the game. b) Modify class Client to display a button that, when clicked, allows the client to play another game. The button should be enabled only when a game completes. Note that both class Client and class Server must be modified to reset the board and all state information. Also, the other Client should be notified of a new game, so that client can reset its board and state information. c) Modify class Client to provide a button that allows a client to terminate the program at any time. When the button is clicked, the server and the other client should be notified. The server should then wait for a connection from another client so that a new game can begin. d) Modify class Client and class Server so that the loser of a game can choose game piece X or O for the next game. Remember that X always goes first.
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

1142 Networking: Streams-Based Sockets and Datagrams Chapter 22 (Yahoo web space)

Friday, April 20th, 2007

1142 Networking: Streams-Based Sockets and Datagrams Chapter 22 network address server port number networking as file I/O socket NetworkStream class socket-based communications OpenRead method of class WebClient Socket class OpenWrite method of class WebClient spawning packet Start method of class TcpListener pool of threads stream port number stream socket protocol streams-based transmission Read method of class NetworkStream system service ReadByte method of class NetworkStream System.Net namespace reading a file on a Web server System.Net.Sockets namespace ReadString method of class BinaryReaderTcpClient class receive a connection TcpListener class receive data from a server telephone system Receive method of class Socket Thread class Receive method of class UdpClient Transmission Control Protocol (TCP) ReceiveFrom method of class Socket UdpClient class send data to a server User Datagram Protocol (UDP) Send method of class Socket Web server Send method of class UdpClient WebClient class SendTo method of class Socket Write method of class BinaryWriter server Write method of class NetworkStream server Internet address WriteByte method of class NetworkStream SELF-REVIEW EXERCISES 22.1 State whether each of the following is true or false. If false, explain why. a) UDP is a connection-oriented protocol. b) With stream sockets, a process establishes a connection to another process. c) Datagram-packet transmission over a network is reliable packets are guaranteed to arrive in sequence. d) Most of the time TCP protocol is preferred over the UDP protocol. e) Each TcpListener can accept only one connection. f) A TcpListener can listen for connections at more than one port at a time. g) A UdpClient can send information only to one particular port. h) Packets sent via a UDP connection are sent only once. i) Clients need to know the port number at which the server is waiting for connections. 22.2 Fill in the blanks in each of the following statements: a) Many of C# s networking classes are contained in namespaces and . b) Class is used for unreliable but fast datagram transmission. c) An object of class represents an Internet Protocol (IP) address. d) The two types of sockets we discussed in this chapter are sockets and sockets. e) The acronym TCP stands for . f) Class listens for connections from clients. g) Class connects to servers. h) Class provides access to stream data on a network.
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 (Web site designers) 1141

Friday, April 20th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1141 A port number is a numeric ID number that a process uses to identify itself at a given network address, also known as an Internet Protocol Address (IP Address). An individual process running on a computer is identified by an IP address/port number pair. Hence, no two processes can have the same port number at a given IP address. The establishment of a simple client requires four steps. In Step 1, we create a TcpClient to connect to the server. This connection is established through a call to the TcpClient method Connect containing two arguments the server s IP address and the port number In Step 2, the TcpClient uses method GetStream to get a Stream to write to and read from the server. Step 3 is the processing phase, in which the client and the server communicate. Step 4 has the client close the connection by calling the Close method on the NetworkStream. NetworkStream methods WriteByteand Write can be used to output individual bytes or sets of bytes to the server, respectively. NetworkStreammethods ReadByteand Readcan be used to read individual bytes or sets of bytes from the server, respectively. Class UdpClientis provided for connectionless transmission of data. Class UdpClientmethods Sendand Receive are used to transmit data. Class IPEndPoint represents an endpoint on a network. Class IPAddress represents an Internet Protocol address. Multithreaded servers can manage many simultaneous connections with multiple clients. TERMINOLOGY 127.0.0.1 AcceptSocket method of class TcpListener Berkeley Sockets Interface BinaryReader class BinaryWriterclass Bind method of class Socketbinding a server to a port block block until connection received client client/server chat client/server model Close method of class SocketClose method of class TcpClient collaborative applications Connect method of class TcpListenerconnection connection attempt connection between client and server terminates connection port connection to a server connectionless service connectionless transmission with datagrams connection-oriented service connection-oriented, streams-based transmission datagram datagram socket duplicate of datagram echo a packet back to a client e-mail Exit method of class Environment ExitCode property of class Environment file processing GetStream method of class Socket infinite loop Internet Protocol Addresses (IP Address) IP Address IPAddress class IPEndPoint class LAN Local Area Network (LAN) localhost loopback IP address Loopbackstatic member of class IPAddress Microsoft Internet Explorer Netscape Communicator
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

1140 Networking: Streams-Based Sockets and (Web design careers) Datagrams Chapter 22

Thursday, April 19th, 2007

1140 Networking: Streams-Based Sockets and Datagrams Chapter 22 43 44 // property Location; the square’s location on the board 45 public int Location 46 { 47 get 48 { 49 return location; 50 } 51 } // property Location 52 53 } // end class Square Fig. 22.7 Fig. 22.7Fig. 22FiFi.7g. 22.7g. 22.7Class Square. (Part 2 of 2.) In this chapter, we discussed how to use C# s networking technologies by providing both connection-oriented (i.e., streams-based) transmission and connectionless (i.e., packet-based) transmission. We showed how to create a simple server and client via stream sockets, then showed how to create a multithreaded server. In Chapter 23, Data Structures and Collections, we discuss how to store data dynamically and discuss several of the key classes that belong to the C# System.Collectionsnamespace. SUMMARY Sockets are the fundamental way to perform network communications in the .NET Framework. The term socket refers to the Berkeley Sockets Interface, which was developed in 1978 to facilitate network programming with UNIX and was popularized by C and C++ programmers. The two most popular types of sockets are stream sockets and datagram sockets. Stream sockets provide a connection-oriented service, meaning that one process establishes a connection to another process, and data can flow between the processes in continuous streams. Datagram sockets provide a connectionless service that uses messages to transmit data. Connectionless services generally offer greater performance but less reliability than connectionoriented services. Transmission Control Protocol (TCP) is the preferred protocol for stream sockets. It is a reliable and relatively fast way to send data through a network. The User Datagram Protocol (UDP) is the preferred protocol for datagram sockets. UDP is unreliable. There is no guarantee that packets sent with UDP will arrive in the order in which they were sent or that they will arrive at all. The establishment of a simple server with TCP and stream sockets in C# requires five steps. Step 1 is to create a TcpListenerobject. This class represents a TCP stream socket that a server can use to receive connections. To receive connections, the TcpListenermust be listening for them. For the TcpListener to listen for client connections, its Startmethod must be called (Step 2). TcpListenermethod AcceptSocketblocks indefinitely until a connection is established, at which point it returns a Socket(Step 3). Step 4 is the processing phase, in which the server and the client communicate via methods Read and Writevia a NetworkStreamobject. When the client and server have finished communicating, the server closes the connection with the Closemethod on the Socket(Step 5). Most servers will then, by means of a control loop, return to the AcceptSocketcall step to wait for another client s connection.
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

Web host - Chapter 22 Networking: Streams-Based Sockets and Datagrams 1139

Thursday, April 19th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1139 ated NetworkStream object from TcpClient (line 74). Lines 80 81 start a thread to read messages sent from the server to the client. The server passes messages (for example, whether each move is valid) to method ProcessMessage(lines 163 207). If the message indicates that a move is valid (line 168), the client sets its mark to the current square (the square that the user clicked) and repaints the board. If the message indicates that a move is invalid (line 178), the client notifies the user to click a different square. If the message indicates that the opponent made a move (line 185), line 188 reads from the server an intspecifying where on the board the client should place the opponent s mark. 1 // Fig. 22.7: Square.cs 2 // A Square on the TicTacToe board. 3 4 using System.Windows.Forms; 5 6 // the representation of a square in a tic-tac-toe grid 7 public class Square 8 { 9 private Panel panel; 10 private char mark; 11 private int location; 12 13 // constructor 14 public Square( Panel newPanel, char newMark, int newLocation ) 15 { 16 panel = newPanel; 17 mark = newMark; 18 location = newLocation; 19 } 20 21 // property SquarePanel; the panel which the square represents 22 public Panel SquarePanel 23 { 24 get 25 { 26 return panel; 27 } 28 } // end property SquarePanel 29 30 // property Mark; the mark of the square 31 public char Mark 32 { 33 get 34 { 35 return mark; 36 } 37 38 set 39 { 40 mark = value; 41 } 42 } // end property Mark Fig. 22.7 Fig. 22.7Fig. 22FiFi.7g. 22.7g. 22.7Class Square. (Part 1 of 2.)
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

Dedicated web hosting - 1138 Networking: Streams-Based Sockets and Datagrams Chapter 22

Wednesday, April 18th, 2007

1138 Networking: Streams-Based Sockets and Datagrams Chapter 22 server output after (1.) server output after (2.) server output after (3.) server output after (4.) Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 7 of 7.) Fig. 22.6 Line 229 of Fig. 22.5 begins a whilethat loops until Socketproperty Available indicates that there is information to receive from the Socket(or until the server disconnects from the client). If there is no information, the thread goes to sleep for one second. Upon awakening, the thread uses property Disconnected to check for whether server variable disconnectis true. If the value is true, the Threadexits the method (thus terminating the Thread); otherwise, the Thread loops again. However, if property Availableindicates that there is data to receive, the whileloop of lines 229 235 terminates, enabling the information to be processed. This information contains an intrepresenting the location in which the client wants to place a mark. Line 238 calls method ReadInt32 of the BinaryReader object (which reads from the NetworkStreamcreated with the Socket) to read this int. Line 242 then passes the int to Server method ValidMove. If this method validates the move, the Playerplaces the mark in the desired location. Method ValidMove(lines 101 131) sends to the client a message indicating whether the move was valid. Locations on the board correspond to numbers from 0 8(0 2for the first row, 3 5for the second and 6 8for the third). All statements in method ValidMove are enclosed in a lockstatement that allows only one move to be attempted at a time. This prevents two players from modifying the game s state information simultaneously. If the Player attempting to validate a move is not the current player (i.e., the one allowed to make a move), that Playeris placed in a wait state until it is that Player s turn to move. If the user attempts to place a mark on a location that already contains a mark, method ValidMove returns false. However, if the user has selected an unoccupied location (line 111), lines 114 115 place the mark on the local representation of the board. Line 121 notifies the other Player that a move has been made, and line 124 invokes the Pulse method so that the waiting Player can validate a move. The method then returns true to indicate that the move is valid. When a Clientapplication (Fig. 22.6) executes, it creates a TextBoxto display messages from the server and the Tic-Tac-Toe board representation. The board is created out of nine Squareobjects (Fig. 22.7) that contain Panels on which the user can click, indicating the position on the board in which to place a mark. The Client s constructor (line 50 82) opens a connection to the server (line 73) and obtains a reference to the connection s associ
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1137 (Email web hosting)

Wednesday, April 18th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1137 2. 3. 4. Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 6 of 7.) Fig. 22.6
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Web hosting rating - 1136 Networking: Streams-Based Sockets and Datagrams Chapter 22

Tuesday, April 17th, 2007

1136 Networking: Streams-Based Sockets and Datagrams Chapter 22 203 // display the message 204 else 205 displayTextBox.Text += message + “rn”; 206 207 } // end method ProcessMessage 208 209 // sends the server the number of the clicked square 210 public void SendClickedSquare( int location ) 211 { 212 // if it is the current player’s move right now 213 if ( myTurn ) 214 { 215 // send the location of the move to the server 216 writer.Write( location ); 217 218 // it is now the other player’s turn 219 myTurn = false; 220 } 221 } 222 223 // write-only property for the current square 224 public Square CurrentSquare 225 { 226 set 227 { 228 currentSquare = value; 229 } 230 } 231 232 } // end class Client 1. Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 5 of 7.) Fig. 22.6
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Chapter 22 (Http web server) Networking: Streams-Based Sockets and Datagrams 1135

Tuesday, April 17th, 2007

Chapter 22 Networking: Streams-Based Sockets and Datagrams 1135 150 // receive messages sent to client 151 while ( true ) 152 ProcessMessage( reader.ReadString() ); 153 } 154 catch ( EndOfStreamException ) 155 { 156 MessageBox.Show( “Server is down, game over”, “Error”, 157 MessageBoxButtons.OK, MessageBoxIcon.Error ); 158 } 159 160 } // end method Run 161 162 // process messages sent to client 163 public void ProcessMessage( string message ) 164 { 165 // if the move player sent to the server is valid 166 // update the display, set that square’s mark to be 167 // the mark of the current player and repaint the board 168 if ( message == “Valid move.” ) 169 { 170 displayTextBox.Text += 171 “Valid move, please wait.rn”; 172 currentSquare.Mark = myMark; 173 PaintSquares(); 174 } 175 176 // if the move is invalid, display that and it is now 177 // this player’s turn again 178 else if ( message == “Invalid move, try again” ) 179 { 180 displayTextBox.Text += message + “rn”; 181 myTurn = true; 182 } 183 184 // if opponent moved 185 else if ( message == “Opponent moved” ) 186 { 187 // find location of their move 188 int location = reader.ReadInt32(); 189 190 // set that square to have the opponents mark and 191 // repaint the board 192 board[ location / 3, location % 3 ].Mark = 193 ( myMark == ‘X’ ? ‘O’ : ‘X’ ); 194 PaintSquares(); 195 196 displayTextBox.Text += 197 “Opponent moved. Your turn.rn”; 198 199 // it is now this player’s turn 200 myTurn = true; 201 } 202 Fig. 22.6 Fig. 22.FiFig. 22.6g. 22.6Client side of client/server Tic-Tac-Toe program. (Part 4 of 7.) Fig. 22.6
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services