MORPG Server not catching disconnection - c#

Ok, simple problem hopefully.
I have started to make a simple server for a simple MORPG game im making, the client connected, and then disconnects, simple, the server catches the new client, but doesn't catch when it disconnects, whats wrong?
Im hoping its something stupidly obvious.
Here's my server code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace ChaWilPeiBle
{
public partial class MainForm : Form
{
public bool ServerON = false;
public TcpListener ServerL;
public int ServerLoad = 2;
public string ServerINFtxt = "";
public ASCIIEncoding AE = new ASCIIEncoding();
public MainForm()
{
InitializeComponent();
}
private void LBL_SS_Click(object sender, EventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
LBL_SS.ForeColor = Color.Red;
}
public void AddH(string S)
{
ServerINFtxt += S;
}
public void Server()
{
try
{
ServerL = new TcpListener(47);
ServerL.Start();
AddH("\nServer Started On Port 47");
}
catch(Exception e)
{
MessageBox.Show("Error starting Server.__________________\n"+e.ToString());
}
while (true)
{
TcpClient TCPC;
TCPC = ServerL.AcceptTcpClient();
if (ServerLoad < 100)
{
Thread T = new Thread(HandleClient);
T.Start((object)TCPC);
}
else
{
byte[] BYTES = AE.GetBytes("NoAccess:Full");
NetworkStream NS = TCPC.GetStream();
NS.Write(BYTES, 0, BYTES.Length);
}
}
}
public void HandleClient(object C)
{
ServerLoad++;
TcpClient Client = (TcpClient)C;
NetworkStream NS = Client.GetStream();
AddH("Client Connected.\nServer Load: " + ServerLoad);
Thread T = new Thread(ReadClient);
T.Start(C);
try
{
while (true) { NS.Write(AE.GetBytes(""), 0, AE.GetBytes("").Length); }
}
catch { }
ServerLoad--;
AddH("Client Disconnected.\nServer Load: " + ServerLoad);
}
public void ReadClient(object C)
{
//TcpClient Client = (TcpClient)C;
}
private void startStopToolStripMenuItem_Click(object sender, EventArgs e)
{
LBL_SS.Text = "Server On";
LBL_SS.ForeColor = Color.Green;
if (ServerON == false)
{
Thread T = new Thread(Server);
T.Start();
}
ServerON = true;
}
private void Update_Tick(object sender, EventArgs e)
{
ServerINF.Text = ServerINFtxt;
PB_SL.Value = ServerLoad;
}
}
}

If you make a blocking call to read on a network stream and it returns 0 bytes as the length that it read it means that your client has disconnected. You will not get an exception until you try write to this connection. That's my best guess as to what is happening.

Related

Why is the TcpListener server not starting?

The TcpListener server does not start, I want to take readings from a humidity sensor connected to ESP8266, which should connect to it, an error related to null comes out. The server is being raised on the Xamarin application.
I have already tried to register server initialization in different parts of the code, but still nothing comes out. Tell me what the error is.
using System;
using Xamarin.Forms;
using System.Net.Sockets;
using System.Net;
using Android.Widget;
using System.Text;
using System.Collections.Generic;
namespace Teplica
{
public partial class MainPage : ContentPage
{
private string ip = "192.168.1.2";
private int port = 8545;
private bool door = true;
TcpListener server;
TcpClient client = null;
NetworkStream stream = null;
private bool Timer = true;
private byte[] data = null;
StringBuilder builder = null;
private List<string> list = new List<string>();
public MainPage()
{
InitializeComponent();
server = new TcpListener(IPAddress.Parse(ip), 8545);
server.Start();//NullReferencesException
client = server.AcceptTcpClient();
stream = client.GetStream();
Device.StartTimer(TimeSpan.FromSeconds(1), TeplicaTimer);
}
private bool TeplicaTimer()
{
data = new byte[256];
builder = new StringBuilder();
int bytes = stream.Read(data,0, data.Length);
builder.Append(Encoding.UTF8.GetString(data, 0, bytes));
list.Add(Convert.ToString(builder));
DataVlazhnost.ItemsSource = list;
return Timer;
}
private void OpenDoor_Clicked(object sender, EventArgs e)
{
if (door)
{
Toast.MakeText(Android.App.Application.Context, "Открытие двери", ToastLength.Short).Show();
OpenDoor.Text = "Закрыть";
door = false;
}
else
{
Toast.MakeText(Android.App.Application.Context, "Закрытие двери", ToastLength.Short).Show();
OpenDoor.Text = "Открыть";
door = true;
}
}
private void Water_Clicked(object sender, EventArgs e)
{
Toast.MakeText(Android.App.Application.Context, "Подача воды", ToastLength.Short).Show();
}
}
}

How can I connect to Laravel broadcasting from c# desktop app?

I have laravel chat application working through laravel broadcasting on laravel-echo-server. On front end I subscribe to channels and listen events using laravel-echo npm package, but how can I subscribe to channels in desktop app written on c# ?
I expect to have something like this written on c#:
Echo.private('SomeChannel')
.listen('SomeEvent', (response) => {});
install https://github.com/doghappy/socket.io-client-csharp package
using Newtonsoft.Json;
using SocketIOClient.WebSocketClient;
using SocketIOClient;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
namespace c_sharp_demo
{
class Program
{
static async Task Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
var uri = new Uri("http://localhost:6001");
var socket = new SocketIO(uri, new SocketIOOptions
{
Query = new Dictionary<string, string>
{
{"token", "io" }
}
});
socket.OnConnected += Socket_OnConnected;
socket.OnPing += Socket_OnPing;
socket.OnPong += Socket_OnPong;
socket.OnDisconnected += Socket_OnDisconnected;
socket.OnReconnecting += Socket_OnReconnecting;
try
{
await socket.ConnectAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
socket.On("event-name", response =>
{
Console.WriteLine($"server: {response}");
});
Console.ReadLine();
}
private static async void Socket_OnConnected(object sender, EventArgs e)
{
Console.WriteLine("Socket_OnConnected");
var socket = sender as SocketIO;
await socket.EmitAsync("subscribe", new
{
channel = "channelName",
auth = ""
});
}
private static void Socket_OnPing(object sender, EventArgs e)
{
Console.WriteLine("Ping");
}
private static void Socket_OnPong(object sender, TimeSpan e)
{
Console.WriteLine("Pong: " + e.TotalMilliseconds);
}
private static void Socket_OnDisconnected(object sender, string e)
{
Console.WriteLine("disconnect: " + e);
}
private static void Socket_OnReconnecting(object sender, int e)
{
Console.WriteLine($"Reconnecting: attempt = {e}");
}
}
}
You can use pusher/pusher-websocket-dotnet. I have an equivalent solution here: https://stackoverflow.com/a/71869451/10875215

C# WPF - Modify a ComboBox from a different class

I'm writing a small WPF application in C# using Kinect and COMs to communicate to an arduino.
I have a combobox where the user can choose the port that he wants to use, I have a watcher on my COMs and everytime a device is Plugged/Unplugged I want to refresh my combobox.
My problem is that the calling to the function is in another class so I have a thread error
Here is the portion of code that doesn't work :
namespace Microsoft.Samples.Kinect.SkeletonBasics
{
using System.IO;
using System.IO.Ports;
using System;
using System.Management;
using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Controls;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
public partial class MainWindow : Window
{
private WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
internal static ComboBox comboBox; //Internal static variable so it can be used in static method
public MainWindow()
{
InitializeComponent();
}
private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
using (receiveEvent) {}
}
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
comboBox = Com_ComboBox;
string[] ports = SerialPort.GetPortNames();
if (ports.Length == 0)
{
Default_Text.Content = "Aucun port détecté";
comboBox.IsHitTestVisible = false;
comboBox.Focusable = false;
}
else Default_Text.Content = "Arduino sur le port :";
comboBox.SelectedIndex = 0;
foreach (string port in ports)
{
comboBox.Items.Add(port);
}
}
internal static void Refresh_Coms() //I'm trying to call this function from the other class but I get a thread error
{
comboBox.Items.Clear();
ComboBoxItem Default_Text = (ComboBoxItem) comboBox.ItemContainerGenerator.ContainerFromIndex(0);
string[] ports = SerialPort.GetPortNames();
if (ports.Length == 0)
{
Default_Text.Content = "Aucun port détecté";
comboBox.IsHitTestVisible = false;
comboBox.Focusable = false;
comboBox.SelectedIndex = 0;
}
else Default_Text.Content = "Arduino sur le port :";
foreach (string port in ports)
{
comboBox.Items.Add(port);
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
public class WMIReceiveEvent : IDisposable
{
private ManagementEventWatcher watcherAttach;
private ManagementEventWatcher watcherDetach;
public WMIReceiveEvent()
{
try
{
watcherAttach = new ManagementEventWatcher();
watcherAttach.EventArrived += Attaching;
watcherAttach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
watcherAttach.Start();
watcherDetach = new ManagementEventWatcher();
watcherDetach.EventArrived += Detaching;
watcherDetach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
watcherDetach.Start();
return;
}
catch (ManagementException err)
{
MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
}
}
public void Dispose()
{
try
{
watcherAttach.Stop();
watcherDetach.Stop();
watcherAttach.Dispose();
watcherDetach.Dispose();
}
catch
{
MessageBox.Show("An error occurred while trying to close COM event Handler");
}
}
void Attaching(object sender, EventArrivedEventArgs e)
{
if (sender != watcherAttach) return;
Console.WriteLine("Attaching");
MainWindow.Refresh_Coms(); //I can call the function but the thread can't modify the ComboBox
}
void Detaching(object sender, EventArrivedEventArgs e)
{
if (sender != watcherDetach) return;
Console.WriteLine("Detaching");
MainWindow.Refresh_Coms();
}
~WMIReceiveEvent()
{
this.Dispose();
}
}
}
I'm a little new to C#, I've searched on the web but I can't find an easy solution for doing that, can somebody help me please ?
Add SynchronizationContext to Your WMIReceiveEvent class, like:
public class WMIReceiveEvent : IDisposable
{
private readonly SynchronizationContext _syncContext;
public WMIReceiveEvent(SynchronizationContext syncContext)
{
_cyncContext = syncContext;
}
}
and wrap of calling MainWindow.Refresh_Coms(); with Send method of SynchronizationContext:
_syncContext.Send(o => MainWindow.Refresh_Coms(), null);
And last thing is creating Your WMIReceiveEvent class:
private WMIReceiveEvent receiveEvent = new WMIReceiveEvent(SynchronizationContext.Current);
More about SynchronizationContext.

C# .net Remoting Client/Server multithreading Chatroom

I am trying to get my Chat system to work. I have a Client/Server launch per an app. I have Client1 talking to Server 2 and Client2 to Server1 but I cannot figure out how to print the information being sent to the servers to the chat windows of their corresponding Clients.
ChatRoom
using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace ChatRoom
{
public class Class1:MarshalByRefObject
{
// initialize Arraylist to store users signed into chat room
private static ArrayList userNames = new ArrayList();
private static string chatWindow; // fuck u, now ye be a static var
public void sendMessage(string message)
{
Console.WriteLine(message);
//if (message != null)
// {
//add to chat window
chatWindow += message + Environment.NewLine;
//}
}
//public void test()
//{
// Console.WriteLine("pizza");
//}
public void addUserName(string UserName)
{
String test = UserName;
Console.WriteLine(test);
userNames.Add(test);
Console.WriteLine("0");
}
public void deleteUserName(string userName)
{
if (userName != null)
{
userNames.Remove(userName);
}
}
public ArrayList userList()
{
return userNames; // userNames
}
public string OutputWindow()
{
return chatWindow;
}
}
}
SERVER
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using ChatRoom;
using System.Runtime.Remoting.Channels.Http;
using RemoteServer;
namespace RemoteServer
{
public partial class Form1 : Form
{
private int channelNum;
private static string temp;
public Form1()
{
InitializeComponent();
}
private void buttonConfirm_Click(object sender, EventArgs e)
{
temp = textBoxPort.Text;
channelNum = Convert.ToInt32(temp);
HttpChannel ServerChannel = new HttpChannel(channelNum);
Console.WriteLine("httpChannel Set");
this.Close(); // close GUI
ChannelServices.RegisterChannel(ServerChannel, false);
Console.WriteLine("channel registered");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Class1), "ChatRoom",
WellKnownObjectMode.SingleCall);
Console.WriteLine("working");
Console.WriteLine("Port number is " + channelNum);
Console.ReadKey();
}
}
}
CLIENT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChatRoom;
using RemoteServer;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Threading;
using System.Diagnostics;
using System.Timers;
namespace ClientServerGui
{
public partial class Form1 : Form
{
private Class1 server;
private Thread thread;
//private Thread thread2;
//private HttpChannel ClientHttpChannel;
private string UserName;
public Form1()
{
InitializeComponent();
}
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void ConfigureServer(string port)
{
HttpClientChannel ClientChannel = new HttpClientChannel();
ChannelServices.RegisterChannel(ClientChannel, false);
RemotingConfiguration.RegisterWellKnownClientType(typeof(Class1), "http://localhost:"
+ textBoxPort.Text + "/ChatRoom");
}
private void buttonSend_Click(object sender, EventArgs e)
{
//ArrayList displayUsers = server.userList();
//string send = textBoxSend.Text;
//textBoxChatWindowDisplay.Text += send;
//combine User's name with input text
string send = UserName + ": " + textBoxSend.Text;
while( textBoxSend.Text != "")
{
//send message
server.sendMessage(send);
//Clear and focus to start new message
textBoxSend.Clear();
buttonSend.Focus();
}
textBoxChatWindowDisplay.Text += send;
textBoxChatWindowDisplay.Text = server.OutputWindow();
//textBoxChatWindowDisplay.Text += "\n";
//listBoxUserList.Items.Clear();
// foreach (string x in displayUsers)
// {
// listBoxUserList.Items.Add(displayUsers);
// }
}
public void chatReloadTimer_Tick(object sender, EventArgs e)
{
textBoxChatWindowDisplay.Text = server.OutputWindow();
StartServer();
}
private void buttonConnect_Click(object sender, EventArgs e)
{
//RemoteServer.Form1 port = new RemoteServer.Form1();
//string Serverport = port.ServerPort();
if (textBoxUserName.Text != "")
{
UserName = textBoxUserName.Text;
//ClientHttpChannel = new HttpChannel();
//ChannelServices.RegisterChannel(ClientHttpChannel, false);
if (textBoxPort.Text == "")
{
textBoxPort.Text = "9000";
}
//server1
ConfigureServer(textBoxPort.Text);
//server2
// ConfigureServer(Serverport);
server = new Class1();
//add user
server.addUserName(UserName);
//server.test();
//new thread
thread = new Thread(new ThreadStart(StartServer));
//start thread
thread.Start();
//new thread
//thread2 = new Thread(new ThreadStart(StartServer2));
//start thread
//thread2.Start();
buttonSend.Focus();
System.Windows.Forms.Timer chatReloadTimer = new System.Windows.Forms.Timer();
chatReloadTimer.Interval = (100); // .1 seconds refresh
chatReloadTimer.Tick += new EventHandler(chatReloadTimer_Tick);
chatReloadTimer.Start();
}
}
private void StartServer()
{
string currentChatWindow;
//set array list to capture people joining server
ArrayList usersArrayList = server.userList();
//** BAD FOR CHATROOM STYLE****
//if (listBoxUserList.ItemHeight > 0)//if not enmpy kill all
//{
// //empty listbox
this.Invoke(new MethodInvoker(delegate() { listBoxUserList.Items.Clear(); }));
//}
// Add all users to listbox
//while (usersAL != null)
//{
foreach (string x in usersArrayList)
{
//listBoxUserList.Items.Add(UserName);
this.Invoke(new MethodInvoker(delegate() { listBoxUserList.Items.Add(x); }));
}
//}
//display data to user
currentChatWindow = server.OutputWindow();
//textBoxChatWindowDisplay.Text = currentChatWindow;
this.Invoke(new MethodInvoker(delegate() { textBoxChatWindowDisplay.Text = currentChatWindow; }));
//listBoxUserList.Items.Add(
}
private void Form1_Load(object sender, EventArgs e)
{
//Boolean on = true;
Process start = new Process();
start.StartInfo.FileName = "RemoteServer";
start.Start();
MessageBox.Show("Continue?", "Continue", MessageBoxButtons.OKCancel);
//while (on == true)
//{
// Thread.Sleep(1);
// textBoxChatWindowDisplay.Text = server.OutputWindow();
//}
buttonSend.Focus();
}
}
}

Why I can't recieved messages from Chat Server?

Why I can't recieved messages from Chat Server ?
I developed Chat Client in Visual Studio C# with mono for android.
I want to receive mesagens from Chat server they are sent but he Chat Client may be receiving them and i can not seem to show in Text1.Text
The Source Code Chat Client for receiving messages:
//Criado por EcoDuty, Frederico Vaz
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
//
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
namespace ChatClient_Android
{
[Activity(Label = "ChatClient_Android", MainLauncher = true, Icon = "#drawable/icon")]
public class MainChat : Activity
{
public delegate void OnRecievedMessage(string message);
public MainChat form;
const int WM_VSCROLL = 0x115;
const int SB_BOTTOM = 7;
TextView text1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button ligar = FindViewById<Button>(Resource.Id.btligar);
text1 = FindViewById<TextView>(Resource.Id.text1);
//Conexão com o servidor
ligar.Click += delegate
{
Connect();
ligar.Enabled = false;
};
}
//Função Actualizar a Caixa de Entrada de Mensagens
private void UpdateTextbox(string text)
{
text1.Text += "\r\n";
text1.Text += text;
}
//Recieved Mesages
public void RecievedMessage(string message)
{
UpdateTextbox(message);
}
//TCP Connection
public StreamWriter Outgoing;
private StreamReader Incoming;
private TcpClient Connection;
private Thread Messages;
private IPAddress IP;
//public string host;
//public string nick;
//MainChat m_ParentForm;
bool isConnected;
//Função Conectar
public void Connect()
{
try
{
IP = IPAddress.Parse("10.0.2.2");
Connection = new TcpClient();
Connection.Connect(IP, 1986);
Outgoing = new StreamWriter(Connection.GetStream());
Outgoing.WriteLine("EcoDuty");
Outgoing.Flush();
//m_ParentForm.Vis(true);
Messages = new Thread(new ThreadStart(this.Communication));
Messages.Start();
}
catch (Exception e) { Disconnected(e.Message); }
}
private void Communication()
{
Incoming = new StreamReader(Connection.GetStream());
string check = Incoming.ReadLine();
if (check[0] == '1')
{
//Vis(true);
isConnected = true;
}
else
{
string Reason = "Disconnected: ";
Reason += check.Substring(2, check.Length - 2);
Disconnected(Reason);
return;
}
while (isConnected == true)
{
try
{
ServerMessage(Incoming.ReadLine());
}
catch (Exception e)
{
if (isConnected == true)
{
Disconnected("Connection to server lost");
Console.WriteLine("Connection Lost: " + e.ToString());
}
break;
}
}
}
private void ServerMessage(string message)
{
try
{
RecievedMessage(message);
}
catch { }
}
public void CloseConnection()
{
isConnected = false;
Incoming.Close();
Outgoing.Close();
Connection.Close();
Messages.Abort();
}
public void SendMessage(string message)
{
Outgoing.WriteLine(message);
Outgoing.Flush();
}
}
}
You seem to be trying to update the text from a non UI thread (if you follow the calls stack you see that the method is triggered from a dedicated thread you spawn):
private void UpdateTextbox(string text)
{
text1.Text += "\r\n";
text1.Text += text;
}
Instead use RunOnUiThread() to schedule the text change to run on the UI thread:
private void UpdateTextbox(string text)
{
RunOnUiThread(() =>
{
text1.Text += "\r\n";
text1.Text += text;
});
}
Also you should get rid of the empty exception catching you do along the way - this most likely masked the problem.
Also always check the catlog for exceptions they usually are a good indicator.

Categories