【正文】
ved(IAsyncResult asyn) { try { CSocketPacket theSockId = (CSocketPacket) 。 //end receive... int iRx = 0 。 iRx = (asyn)。 char[] chars = new char[iRx + 1]。 d = ()。 int charLen = (, 0, iRx, chars, 0)。 szData = new (chars)。 = + szData。 WaitForData()。 } catch (ObjectDisposedException ) { (0,1,\nOnDataReceived: Socket has been closed\n)。 } catch(SocketException se) { ( )。 } } To see the whole application download the code and you can see the code. There is one thing which you may be wondering about. When you call BeginReceive, you have to pass a buffer and the number of bytes to receive. The question here is how big should the buffer be. Well, the answer is it depends. You can have a very small buffer size say, 10 bytes long and if there are 20 bytes ready to be read, then you would require 2 calls to receive the data. On the other hand if you specify the length as 1024 and you know you are always going to receive data in 10byte chunks you are unnecessarily wasting memory. So the length depends upon your application. If you have understood whatever I have described so far, you will easily understand the Server part of the socket application. So far we have been talking about a client making connection to a server and sending and receiving data. On the Server end, the application has to send and receive data. But in addition to adding and receiving data, server has to allow the clients to make connections by listening at some port. Server does not need to know client . addresses. It really does not care where the client is because its not the server but client who is responsible for making connection. Server39。s responsibility is to manage client connections. On the server side there has to be one socket called the Listener socket that 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。 public void StartListening() { try { //create the listening socket... m_socListener = new Socket(,)。 IPEndPoint ipLocal = new IPEndPoint ( ,8221)。 //bind to local IP Address... ( ipLocal )。 //start listening... (4)。 // create the call back for any client connections... (new AsyncCallback ( OnClientConnect ),null)。 = false。 } catch(SocketException se) { ( )。 } } 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)。 WaitForData(m_socWorker)。 } catch(ObjectDisposedException) { (0,1,\n OnClientConnection: Socket has been closed\n)。 } catch(SocketException se) { ( )。 } } 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 = 。 byte[] byData = ( ())。 (byData)。 And that39。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.