daJbot
daJbot

Reputation: 59

Try Catch, with event handler

I have a client that connects to my server, now with the following code the connection happens. But if the connection can't be made an event is created so my main program can show a message if it fails. But this event isn't triggered. How can I correct my code to make this happen?

Wrapper class for my Client Socket

public class ClientWrapper
{
    Socket clientSocket;
    public delegate void ErrorMessageHandler(string errorMsg);
    public event ErrorMessageHandler OnErrorRecieved;

    public ClientWrapper(IPAddress serverIP, int port)
    {
        clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        try
        {
            clientSocket.Connect(new IPEndPoint(serverIP, port));
        }
        catch (Exception e)
        {
            if (OnErrorRecieved != null)
                OnErrorRecieved(e.Message);
        }
    }
}

In my program code, Where the event is used

//Connect to Server
ClientWrapper clientSocket = new ClientWrapper(serverIP, port);

//Event trigger if client fails to connect to server

clientSocket.OnErrorRecieved += new ClientWrapper.ErrorMessageHandler(clientSocket_OnErrorRecieved);

And the method for the event

void clientSocket_OnErrorRecieved(string errorMsg)
{
    MessageBox.Show("No response from server. This can be caused if Server is offline or incorrect detail is provided.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

Upvotes: 1

Views: 4002

Answers (3)

No Idea For Name
No Idea For Name

Reputation: 11597

when you call the line

ClientWrapper clientSocket = new ClientWrapper(serverIP, port);

the constructor start to run. only when it ends then you connect the event

clientSocket.OnErrorRecieved += new ClientWrapper.ErrorMessageHandler(clientSocket_OnErrorRecieved);

you got 2 Easy possible ways to do it.

the first one is what Jeroen van Langen proposed.

the second is ask, in the constructor for delegate:

public ClientWrapper(IPAddress serverIP, int port, ErrorMessageHandler method)
{
    OnErrorRecieved += method;

    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    try
    {
        clientSocket.Connect(new IPEndPoint(serverIP, port));
    }
    catch (Exception e)
    {
        if (OnErrorRecieved != null)
            OnErrorRecieved(e.Message);
    }
}

also, please use using statement when using socket

Upvotes: 0

Rowland Shaw
Rowland Shaw

Reputation: 38130

It looks like your event would've failed prior to you registering the event handler, as you raise it in the onstructor.

There are many options as to the approach you could try instead, such as passing the event handler to the constructor, so that it could subscribe it prior to being called, or breaking the connection code into a separate method, to give consumers a chance to subscribe to the error handler themselves.

Upvotes: 0

Jeroen van Langen
Jeroen van Langen

Reputation: 22073

That's because your constructor is already connecting, before you bound the event to it. You should connect in a method other than the constructor:

public void Connect(IPAddress serverIP, int port)
{
    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        try
        {
            clientSocket.Connect(new IPEndPoint(serverIP, port));
        }
        catch (Exception e)
        {
            if (OnErrorRecieved != null)
                OnErrorRecieved(e.Message);
        }

}

And construct it this way.

ClientWrapper clientSocket = new ClientWrapper();
clientSocket.OnErrorRecieved += new clientWrapper.ErrorMessageHandler(clientSocket_OnErrorRecieved);
clientSocket.Connect(serverIP, port);

The event will only be triggered when it's bound before you connect.

Upvotes: 4

Related Questions