C
C#6mo ago
SY

✅ Stack overflow.

Im doing a server in c# here is the main
c#
public static void Main(string[] args)
{
while (true)
{
TcpClient Client = Server.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClient));
clientThread.Start(Client);
}
}
c#
public static void Main(string[] args)
{
while (true)
{
TcpClient Client = Server.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClient));
clientThread.Start(Client);
}
}
c#
public static void HandleClient(object Client)
{
TcpClient tcpClient = (TcpClient)Client;


NetworkStream stream = tcpClient.GetStream();

try
{
byte[] buffer = new byte[1024];
int bytesRead;

while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
string message = Encoding.UTF8.GetString(buffer, 0, bytesRead);

if (message == "")
return;


HandleMessage(message, tcpClient);
}
}
}
c#
public static void HandleClient(object Client)
{
TcpClient tcpClient = (TcpClient)Client;


NetworkStream stream = tcpClient.GetStream();

try
{
byte[] buffer = new byte[1024];
int bytesRead;

while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
string message = Encoding.UTF8.GetString(buffer, 0, bytesRead);

if (message == "")
return;


HandleMessage(message, tcpClient);
}
}
}
c#
private static void HandleMessage(string message, TcpClient client)
{
// some logic
Console.WriteLine("{DATA IN}: " + message);
HandleJsonMessage(msg, client);
}
c#
private static void HandleMessage(string message, TcpClient client)
{
// some logic
Console.WriteLine("{DATA IN}: " + message);
HandleJsonMessage(msg, client);
}
c#
private static void HandleJsonMessage(string msg, TcpClient client)
{
if (msg == "ready") {
// some logic and check result after 7 seconds
ThreadPool.QueueUserWorkItem(o =>
{
Thread.Sleep(7000);


Packet.sendMsg("%xt%getResult%-1%.", p1.Client);
Packet.sendMsg("%xt%getResult%-1%.", p2.Client);
});
}
}
c#
private static void HandleJsonMessage(string msg, TcpClient client)
{
if (msg == "ready") {
// some logic and check result after 7 seconds
ThreadPool.QueueUserWorkItem(o =>
{
Thread.Sleep(7000);


Packet.sendMsg("%xt%getResult%-1%.", p1.Client);
Packet.sendMsg("%xt%getResult%-1%.", p2.Client);
});
}
}
Ok so the problem is Console.WriteLine("{DATA IN}: " + message); in HandleMessage function is getting spammed and server crashes
3 Replies
SY
SY6mo ago
Also tried to use async in this way I tried to but still crash
c#
private async static void HandleJsonMessage(string msg, TcpClient client)
{
if (msg == "ready") {
// some logic and check result after 7 seconds

await Task.Delay(7000);

Packet.sendMsg("%xt%getResult%-1%.", p1.Client);
Packet.sendMsg("%xt%getResult%-1%.", p2.Client); // this is going to call mv

}
if (msg == "mv") {
//anything here makes server crash
}
}
c#
private async static void HandleJsonMessage(string msg, TcpClient client)
{
if (msg == "ready") {
// some logic and check result after 7 seconds

await Task.Delay(7000);

Packet.sendMsg("%xt%getResult%-1%.", p1.Client);
Packet.sendMsg("%xt%getResult%-1%.", p2.Client); // this is going to call mv

}
if (msg == "mv") {
//anything here makes server crash
}
}
still crashed when mv gets called
WEIRD FLEX
WEIRD FLEX6mo ago
i would just use async everywhere and avoid threads if there's no need for it so, what is the error
SY
SY6mo ago
The error is That the client is going to send a json which contain ready msg. What i want to do is that server should do some logic, wait for 7 seconds and then send result packet to clients. so when adding Thread.Sleep(7000); after the 7 seconds pass it start spamming as shown in picture i tried to make HandleJson function async and i did await Task.Delay(7000); It stopped spam after sending the result packet. But the problem is after the result packet client will send mv to the server, if i write any code inside mv it will start spamming same thing i did everything async still same thing removing await wont crash the server Nvm i figured it out thx
Want results from more Discord servers?
Add your server
More Posts