【正文】
ead data at regular intervals (via clicking the Rx button). But as I said in my earlier article, that model does not work very well in a real world application. Also since Windows is an eventsbased system, the application (client) should get notifications of some kind whenever the data is received so that client can read it rather than client continuously polling for data. Well that is possible with a little effort. If you read the first part of this article, you already know that the Socket class in the namespace has several methods like Receive and Send which are blocking calls. Besides there are also functions like BeginReceive , BeginSend etc. These are meant for asynchronous IO . For example , there are at least two problems with the blocking Receive: When you call Receive function the call blocks if no data is present, the call blocks till some data arrives. Even if there is data when you made the receive call , you don39。 } catch (SocketException se) { ( )。 (remoteEP)。s puter ). Computers on work have a unique identifier called . address which is represented in dotnotation like etc. Lets see how all this works in .NET. Before we go any further, download the source code attached with this article. Extract the zip file to a folder say c:\Temp you will see following two folders : Server Client In the Server folder there will be one EXE. And in the client there will be the source code in C that is our client. There will be one file called which the solution file. If you double click that your will be launched and you will see the project SocketClientProj in the solution. Under this project you will have file. Now build the code (by pressing CtrlShiftB) and run the code you will see the following dialog box: As you can see the dialog box has a field for Host IP address ( which is the IP address of the machine on which you will run the Server Application, located under Server folder). Also there is a field where you can specify port number at which the Server is listening. The server app I have provided here listens at port 8221. So I have specified port to be 8221. After specifying these parameters we need to connect to the server. So pressing Connect will connect to the server and to close the connection press Close. To send some data to the server type some data in the field near the button name Tx and if you press Rx the application will block unless there is some data to read. With this info lets now try to check the code behind this: Socket programming in .NET is made possible by the Socket class present inside the namespace. This Socket class has several method and properties and a constructor. The first step is to create an object of this class. Since there is only one constructor we have no choice but to use it. Here is how to create the socket: m_socListener = new Socket(,)。 The first parameter is the address family which we will use, in this case, interNetwork (which is IP version 4) other options include Banyan NetBios, AppleTalk etc. (AddressFamily is an enum defined in Sockets namespace). Next we need to specify socket type: and we would use reliable two way connectionbased sockets (stream) instead of unreliable Connectionless sockets (datagrams) . So we obviously specify stream as the socket type and finally we are using TCP/IP so we would specify protocol type as Tcp. Once we have created a Socket we need to make a connection to the server since we are using connectionbased munication. To connect to the remote puter we need to know the IP Address and port at which to connect. In .NET there is a class under namespace called IPEndPoint which represents a work puter as an IP address and a port number. The IPEndPoint has two constructors one that takes a IP Address and Port number and one that takes long and port number. Since we have puter IP address we would use the former public IPEndPoint( address, int port)。 These three lines of code will make a connection to the remote host running on puter with IP and listening at port 8221. If the Server is running and started (listening), the connection will succeed. If however the server is not running an exception called SocketException will be thrown. If you catch the exception and check the Message property of the exception in this case you see following text: No connection could be made because the target machine actively refused it. Similarly if you already have made a connection and the server somehow dies, you wi