-1

I'm trying to whip up an application "suite" that will use quite a bit of shared code and I'm trying to (re)learn how to handle certain things - if this is not the way to do it, please let me know.

I have one solution in VS2015, which will include 4, maybe 5 projects. 2 different yet similar client-apps, a server app(still need to work this out), and a shared library (I might add another one).

Right now I have my code for connecting to the client in my library. My WinForm app calls this code, which "works" (i need to develop the server app still, but it's trying to connect). However, I want to catch and handle the exceptions thrown by the DLL with a nice error to inform the user what is going wrong.

ClientConnect.cs

public class ClientConnection
{
    public void ConnectToServer(IPEndPoint ip, string type, string name)
    {      
        clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        ipEndPoint = ip;
        strName = name;
        strTypeOfCLient = type;

        clientSock.BeginConnect(ip, new AsyncCallback(LoginToServer), clientSock);
    }

    public void LoginToServer(IAsyncResult AR)
    {
        try
        {
            clientSock.EndConnect(AR); // This statement is where it goes wrong.
        }
        catch (Exception)
        {
            throw;
        }
    }
}

CarbonCats_Main.cs (in project called Carbon Cats

public class CarbonCats_Main
{
    private static ClientConnection clientcon;

    private void btnConnect_Click(object sender, EventArgs e)
    {
        //placeholder for making the connection to "Mother Base"
        int port = Int32.Parse(txtPort.Text);
        System.Net.IPAddress ipA = System.Net.IPAddress.Parse(txtServer.Text);
        System.Net.IPEndPoint ipE = new System.Net.IPEndPoint(ipA,port);

        try
        {
            clientcon.ConnectToServer(ipE, "Cat", txtName.Text);                
        }
        catch (Exception ex)
        {
            MessageBox.Show("An error occurred! are you sure the server is online? \n" +
            "Here is more information: \n" + ex.Message, "Carbon Cats", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
        }
    }
}

I have tried different ways to catch the error - wrap a try-catch around both, around either, made my lib static, instantiated it, ... but it always lets me down.

How do I actually handle the exception thrown (in this case, it'll always be a SocketException since it can not find a server yet. But I have more Exception(Types) to handle once I get to cleaning up the code and making it user-proof.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • Be careful to format your code properly! And read about [mvce](http://stackoverflow.com/help/mcve), it should help you asking better questions and getting more precise answers. – Guillaume Racicot Sep 01 '16 at 01:43

1 Answers1

-1

Shouldn't be doing what I tried to do on Async calls. Async exception handling should be done differently. Catch an exception thrown by an async method

Community
  • 1
  • 1