1

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);
}
4

3 回答 3

4

那是因为在您将事件绑定到它之前,您的构造函数已经在连接。您应该使用构造函数以外的方法进行连接:

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);
        }

}

并以这种方式构建它。

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

该事件只会在您连接之前绑定时触发。

于 2013-09-02T07:19:06.867 回答
0

在您注册事件处理程序之前,您的事件似乎已经失败,因为您在 onstructor 中引发了它。

对于您可以尝试的方法有很多选择,例如将事件处理程序传递给构造函数,以便它可以在被调用之前订阅它,或者将连接代码分解为一个单独的方法,让消费者有机会自己订阅错误处理程序。

于 2013-09-02T07:23:20.823 回答
0

当您拨打电话时

ClientWrapper clientSocket = new ClientWrapper(serverIP, port);

构造函数开始运行。只有当它结束时,你才连接事件

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

你有两种简单的方法来做到这一点。

第一个是 Jeroen van Langen 提出的。

第二个是在委托的构造函数中询问:

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);
    }
}

另外,请using在使用套接字时使用语句

于 2013-09-02T07:27:30.617 回答