【正文】
ily 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)。 As you can see the first parameter takes a IPAddress object. If you examine the IPAddress class you will see that it has a static method called Parse that returns IPAddress given a string (of dot notation) and second parameter will be the port number. Once we have endpoint ready we can use Connect method of this Socket class to connect to the end point ( remote server puter ). Here is the code: ipAdd = ()。 remoteEP = new IPEndPoint (iAdd,8221)。 (remoteEP)。 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 will get following exception if you try to send data. An existing connection was forcibly closed by the remote host Assuming that the connection is made, you can send data to other side using the Send method of the Socket class. Send method has several overloads. All of them take a byte array . For example if you want to send Hello There to host you can use following call: try { String szData = Hello There。 byte[] byData = (szData)。 (byData)。 } catch (SocketException se) { ( )。 } Note that the Send method is blocking. This means the call will block (wait) till the data has been sent or an exception has been thrown. There is an nonblocking version of the send which we will discuss in the next part of this article. Similar to Send there is a Receive method on the Socket class. You can receive data using following call: byte [] buffer = new byte[1024]。 int iRx = (buffer)。 The Receive method again is blocking. It means that if there is no data available the call will block until some data arrives or an exception is thrown. Nonblocking version of Receive method is more useful than the nonblocking version of Send because if we opt for block Receive , we are effectively doing polling. There is no events about data arrival. This model does not work well for serious applications. But all that is the subject of our next part of this article. For now we will settle with the blocking version. In order to use the source code and application here you would need to run the Server first: Here is the way Server looks like: When you launch the Server, click Start to start listening. The Server listens at port 8221. So make sure you specify the port number 8221 in the port field of our client application. And in the IPAddress field of Client App enter the IP Address of the machine on which the Server is running. If you send some data to server from the client by pressing Tx button, you will see that data in the grayed out edit box. That39。s it for now! The next part in this article will cover the Server program. Socket Programming in C Part 2 – Introduction This is the second part of the previous article about the socket programming. In the earlier article we created a client but that client used to make blocking IO calls ( Receive ) to read 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 whenev