About me

Michael L Perry

Improving Enterprises

Principal Consultant

@michaellperry

User login

Hint

Your homework is extended for another week, but let me give you a hint what I'm looking for. The problem was to Fix the Socket API. Rewrite the .NET Socket interface so that the assertions made in the MSDN documentation can be proven. To give you an example of what I'm looking for, take the following assertion:

  • You have to either call Connect or Accept before Sending and Receiving data.

This is a prerequisite assertion. We must first call Connect or Accept, then we can call Send or Receive. To prove this assertion, I'll use factory methods. Here's the original Socket class:

public class Socket : IDisposable
{
    public Socket(SocketInformation socketInformation);
    public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType);

    public Socket Accept();
    public void Bind(EndPoint localEP);
    public void Connect(EndPoint remoteEP);
    public void Connect(string host, int port);
    public void Dispose();
    public void Listen(int backlog);
    public int Receive(byte[] buffer);
    public int Send(byte[] buffer);
}

Move the Send and Receive methods out of this class. We cannot call them yet. Move them into a new class called ConnectedSocket.

public class ConnectedSocket
{
    public int Receive(byte[] buffer);
    public int Send(byte[] buffer);
}

To prove that we have to call Connect or Accept before any of the ConnectedSocket methods, we make Connect and Accept factory methods:

public class Socket : IDisposable
{
    public Socket(SocketInformation socketInformation);
    public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType);

    public ConnectedSocket Accept();
    public void Bind(EndPoint localEP);
    public ConnectedSocket Connect(EndPoint remoteEP);
    public ConnectedSocket Connect(string host, int port);
    public void Dispose();
    public void Listen(int backlog);
}

This API proves one of the assertions in the MSDN documentation. Try the others on your own. Answers will be posted next week.