0

I am reviewing source code of a voice chat application.

Here I want to run server program, so that any client can contact to server for voice chat. To run my server program I have to pass server name , port number and network interface that I am going to use for voice chat, after passing required arguments I have to call ServerStart method which is done by clicking on Start Checkbox in design view.If user has not passed appropriate type of arguments then it shows error by calling method ShowError().

Now, When I pass serverName, port number and Network Interface then serverName variable reference to null instead of the passed serverName argument.

Why An exception is thrown when I run server program that exception is "The source was not found, but some or all events logs could not be searched. Inaccessible logs: Security."

 public partial class ServerWindow
{
    private ChatServer server;
    public delegate void SetListBoxItem(string str, string type);
    public ServerWindow()
    {
        InitializeComponent();
        ObtainNetworkInterfaces();
    }


    private void cbStartStop_Checked(object sender, RoutedEventArgs e)
    {
        if (cbStartStop.IsChecked == true)
        {
            // validate the port number
            try
            {
                var port = Int32.Parse(tbPortNumber.Text);

                server = new ChatServer(port, cbInterfaces.SelectedItem, tbServerName.Text);
                server.ClientConnected += ServerOnClientConnected;
                server.ClientDisconnected += ServerOnClientDisconnected;
                var serverName = tbServerName.Text;
                if (string.IsNullOrWhiteSpace(serverName))
                {
                    ShowError();
                }
                else
                {
                    server.StartServer();
                    SetControls(false);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        else
        {
            if (server == null)
                return;
            server.StopServer();
            SetControls(true);
        }
    }

    private void ShowError()
    {
        MessageBox.Show(@"Please enter valid port number and/or server name");
        cbStartStop.IsChecked = false;
    }
mdadil2019
  • 807
  • 3
  • 12
  • 29
  • are you getting an exception or does this : `if (string.IsNullOrWhiteSpace(serverName))` evaluate to true? – Mong Zhu Aug 11 '16 at 13:24
  • @MongZhu if (string.IsNullOrWhiteSpace(serverName)) evaluate to true – mdadil2019 Aug 11 '16 at 13:26
  • that does not necessarily mean that `severName` is `null`. `""` or `" "` or `"\t"` would also evaluate to true. Did you have a look in the Debugger which value `tbServerName.Text;` has in this line: `var serverName = tbServerName.Text;` ? – Mong Zhu Aug 11 '16 at 13:29
  • might it be that you accidentally switched names of textboxes and you think that you typed in the server name into the right one, but you reference the wrong one? – Mong Zhu Aug 11 '16 at 13:30
  • @MongZhu Yes, In debugger serverName is null – mdadil2019 Aug 11 '16 at 13:31
  • "Did you have a look in the Debugger which value `tbServerName.Text`" ? – Mong Zhu Aug 11 '16 at 13:31
  • @MongZhu Here when exception occurs ShowError() method is called so I have removed that and catch the excection that occurs. I am updating my question – mdadil2019 Aug 11 '16 at 13:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120706/discussion-between-mong-zhu-and-mohammad-adil). – Mong Zhu Aug 11 '16 at 13:50

1 Answers1

1

The problem seems not to be in the code that you posted. After our chat I would suggest to look at this post try the accepted answer. I have the feeling it will solve your problem.

It is usually helpful to use the exception message in the catch clause. :)

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76