question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataReceived event disconnects (kind of?) client when it is called.

See original GitHub issue

First of, what I am trying to do:

I am using Xamarin, Xamarin Forms, Windows Forms and the SuperSimpleTcp library to connect mobile phones (android tested, IOS should work too) via a server which is running on a PC with a Windows Forms UI. I’m new to C# and developing in Visual Studio, so bare with me, i might just be dumb.

I have already made the UI’s both for PC and mobile. Through the following tests I discovered the following:

The client can send data after he connects, but as soon as he receives server data (meaning the DataReceived event triggers), the next client.Send() generates a System.IO.IOException: ‘Not connected to the server; use Connect() first.’ The client can receive future messages and the disconnected event is never called, but the error indicates that the client may be disconnected.

If the client calls client.Connect() every time before he tries to call client.Send() all the data will go through, the server will receive the data and the client will not face any connection errors, but will be unable to receive data (after the second client.Connect(), the event DataReceived will no longer be called).

The code with which I have tested all of this with has been moddified and this is the final version, triggering the ‘use Connect() first.’ exception.

Here is the client:

` //MsgBox is a scrollable label used to show messages and info to the user //MsgText is a entry (a single-line string input) used to input a message //IPText is a entry (a single-line string input) used to input the IP of the server we want to connect to //ConnectClicked is and event triggered by a button used to connect to a server with the IP specified in the IPText entry //ConnectClicked is and event triggered by a button used to send the message contained in the MsgText entry public partial class MessagePage : ContentPage {

    public MessagePage()
    {
        InitializeComponent();
        MsgText.IsEnabled = false;
        SendButton.IsEnabled = false;
        IPText.IsEnabled = true;
        ConnectButton.IsEnabled = true;
    }

    private void Events_Disconnected(object sender, ClientDisconnectedEventArgs e)
    {
        MsgBox.Text += $"You disconnected. {Environment.NewLine}";
    }

    private void Events_DataReceived(object sender, DataReceivedEventArgs e)
    {
        MsgBox.Text += $"{Encoding.UTF8.GetString(e.Data)}{Environment.NewLine}";
    }

    private void Events_Connected(object sender, ClientConnectedEventArgs e)
    {
        MsgBox.Text += $"You connected. {Environment.NewLine}";
    }

    SimpleTcpClient client;
    private void ConnectClicked(object sender, EventArgs e)
    {
        IPText.IsEnabled = false;
        ConnectButton.IsEnabled = false;
        MsgText.IsEnabled = true;
        SendButton.IsEnabled = true;
        client = new SimpleTcpClient(IPText.Text);
        client.Events.Connected += Events_Connected;
        client.Events.DataReceived += Events_DataReceived;
        client.Events.Disconnected += Events_Disconnected;
        client.Connect();
    }
    private void SendClicked(object sender, EventArgs e)
    {
        client.Send(MsgText.Text);
        MsgText.Text = "";
    }
}

`

This is the server: ` // InfoBox is a multi-line label used to display info, messages and errors to the server UI // StartButton starts the server with the IP specified in IPInput // IPInput is a single-line entry used to input the IP the server should run on

public partial class Form1 : System.Windows.Forms.Form { public Form1() { InitializeComponent();

    }

    delegate void SetTextCallback(string text);

    private void SetText(string text)              //to be honest, I found this on stack overflow as a fix to the "using data outside class" error I get, but it's Windows Forms related and is only used to write errors and info to the server UI, so I don't think it causes the issue I have.
    {

        if (this.InfoBox.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.InfoBox.Text = text;
        }
    }
    SimpleTcpServer server;
    List<string> ClientList = new List<string>();
    string txt;
    private void StartButton_Click(object sender, EventArgs e)
    {
        server = new SimpleTcpServer(IPInput.Text);
        server.Start();
        StartButton.Enabled = false;
        IPInput.Enabled = false;
        txt += $"Starting server{Environment.NewLine}";
        SetText(txt);
        server.Events.ClientConnected += Events_ClientConnected;
        server.Events.ClientDisconnected += Events_ClientDisconnected;
        server.Events.DataReceived += Events_DataReceived;
    }



    private void Events_DataReceived(object sender, DataReceivedEventArgs e)
    {
        foreach (string IpPort in ClientList) {
            server.Send(IpPort, $"{e.IpPort}: {Encoding.UTF8.GetString(e.Data)}");
        }
        txt += $"{e.IpPort}: {Encoding.UTF8.GetString(e.Data)}{Environment.NewLine}";
        SetText(txt);
    }

    private void Events_ClientDisconnected(object sender, ClientDisconnectedEventArgs e)
    {
        ClientList = ClientList.Where((source, index) => source != e.IpPort).ToList();
        txt += $"{e.IpPort} disconnected.{Environment.NewLine}";
        SetText(txt);
    }

    private void Events_ClientConnected(object sender, ClientConnectedEventArgs e)
    {
        txt += $"{e.IpPort} connected.{Environment.NewLine}";
        SetText(txt);
        ClientList.Add(e.IpPort);
    }

}`

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jchristncommented, Jul 8, 2021

Anytime @LeoTheMoldyLemon have a great day!

1reaction
LeoTheMoldyLemoncommented, Jul 8, 2021

Yea, thank You very much for fixing the server issue, it seems I have xamarin and visual studio related problems, which I will try yo fix myself.

Thanks again and sorry for taking your time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SerialPort.DataReceived Event (System.IO.Ports)
Indicates that data has been received through a port represented by the SerialPort object.
Read more >
How to fix or what is the cause of "Disconnecting connId=0 ...
Unfortunately the only correct answer is, the server encountered an error when parsing data received from the client.
Read more >
c# - SerialPort fires DataReceived event after close
I'm experiencing a weird behavior while trying to stop a SerialPort: the DataReceived event continues to fire after unsubscribing and after ...
Read more >
Thread: [2005] Serial port DataReceived event
I have a problem: The DataReceived event of the serial port is raised on a different ... Disconnected, AddressOf OnDisconnected client.
Read more >
RoomEvent | LiveKit JS Client SDK - v1.12.3
Events are the primary way LiveKit notifies your application of changes. ... This fires when room.disconnect() is called or when an unrecoverable connection...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found