【正文】
listens at a specific port number for client connections. When the client makes a connection, the server needs to accept the connection and then in order for the server to send and receive data from that connected client it needs to talk to that client through the socket that it got when it accepted the connection. The following code illustrates how server listens to the connections and accepts the connection: public Socket m_socListener。 //start listening... (4)。 } } If you look at the above code carefully you will see that its similar to we did in the asynchronous client. First of all the we need to create a listening socket and bind it to a local IP address. Note that we have given Any as the IPAddress (I will explain what it means later), and we have passed the port number as 8221. Next we made a call to Listen function. The 4 is a parameter indicating backlog indicating the maximum length of the queue of pending connections. Next we made a call to BeginAccept passing it a delegate callback. BeginAccept is a nonblocking method that returns immediately and when a client has made requested a connection, the callback routine is called and you can accept the connection by calling EndAccept. The EndAccept returns a socket object which represents the ining connection. Here is the code for the callback delegate: public void OnClientConnect(IAsyncResult asyn) { try { m_socWorker = (asyn)。 } } Here we accept the connection and call WaitForData which in turn calls BeginReceive for the m_socWorker. If we want to send data some data to client we use m_socWorker socket for that purpose like this: Object objData = 。s all there is to it! Here is how our client looks like Here is how our server looks like That is all there is to the socket programming. 。 (byData)。 } catch(ObjectDisposedException) { (0,1,\n OnClientConnection: Socket has been closed\n)。 = false。 IPEndPoint ipLocal = new IPEndPoint ( ,8221)。 } catch(SocketException se) { ( )。 szData = new (chars)。 iRx = (asyn)。 = m_socClient。 } The OnConnect function makes a connection to the server and then makes a call to WaitForData. WaitForData creates the callback function and makes a call to BeginReceive passing a global buffer and the callback function. When data arrives the OnDataReceive is called and the m_socClient39。 d = ()。 // now start to listen for any data... m_asynResult = (m_DataBuffer,0,pfnCallBack,null)。 //create the end point IPEndPoint ipEnd = new IPEndPoint (,iPortNo)。 public Socket m_socClient。s the data? The data is now available in the buffer that you passed as the first parameter while making call to BeginReceive() method . In the following example the data will be available in m_DataBuffer : BeginReceive(m_DataBuffer,0,pfnCallBack,null)。 int charLen = (m_DataBuffer, 0, iRx, chars, 0)。 if ( () ) { int iRx = 0 。s Socket class provides BeginReceive method to receive data asynchronously ., in an nonblocking manner The BeginReceive method has following signature: public IAsyncResult BeginReceive( byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state )。 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。 (byData)。 remoteEP = new IPEndPoint (iAdd,8221)。Socket Programming