DataReceived event disconnects (kind of?) client when it is called.
See original GitHub issueFirst 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:
- Created 2 years ago
- Comments:11 (5 by maintainers)
Top GitHub Comments
Anytime @LeoTheMoldyLemon have a great day!
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.