C
C#9mo ago
Slycex

✅ Socket not Listen

Hello when i call OnClientConnected() my console close. Yet, my socket listens well for 100 sec, but the console not wait my client connexion You have an idea ? Thanks you !
class Program
{
static IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
static IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 5555);
static private Socket listener = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

static void Main(string[] args)
{
StartupServer();
OnClientConnected();
}

private static void StartupServer()
{
// Ecoute du serveur
listener.Bind(ipEndPoint);
- listener.Listen(100);
Console.WriteLine("Serveur démarré, en attente du client...");
}

private static async void OnClientConnected()
{
// Connexion du client
- await listener.AcceptAsync();
Console.WriteLine($"Client : \"{ipAddress.MapToIPv4()}\" sur le port : \"{ipEndPoint.Port}\" connecté au serveur.");
}
class Program
{
static IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
static IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 5555);
static private Socket listener = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

static void Main(string[] args)
{
StartupServer();
OnClientConnected();
}

private static void StartupServer()
{
// Ecoute du serveur
listener.Bind(ipEndPoint);
- listener.Listen(100);
Console.WriteLine("Serveur démarré, en attente du client...");
}

private static async void OnClientConnected()
{
// Connexion du client
- await listener.AcceptAsync();
Console.WriteLine($"Client : \"{ipAddress.MapToIPv4()}\" sur le port : \"{ipEndPoint.Port}\" connecté au serveur.");
}
8 Replies
Bailey
Bailey9mo ago
Hi, private static async void OnClientConnected() is a async method which will run async. you should call is using: await OnClientConnected();
Slycex
Slycex9mo ago
Thanks, i do declare my Main method async ? Ok, it working ! I declare my Main method as static async Task Main(string[] args)
Florian Voß
Florian Voß9mo ago
@slycex correct you wanna go async all the way up the calling stack
Slycex
Slycex9mo ago
Yup !
Slycex
Slycex9mo ago
No description
Slycex
Slycex9mo ago
Here is the final program and it works perfectly! Thanks for all
Florian Voß
Florian Voß9mo ago
remember to /close when this issue has been solved @slycex
Slycex
Slycex9mo ago
Oh ok !
Want results from more Discord servers?
Add your server
More Posts
❔ cs-script causes WPF to throw error when creating new form/page```cs public class Module { public string? Name { get; set; } public string?❔ HwndHost not rendering embedded window```cs protected override HandleRef BuildWindowCore(HandleRef hwndParent) { SetParent(_hostedHand❔ Is there any way I can populate arrays with user input?For example I want a user to fill up an array named randomNumbers with well, random numbers, can I d❔ Template not showing up?https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net I have been trying to install this templat✅ Blazor or Razor pagesHi chat, in my roadmap, i have razor pages after Asp.net core mvc, but i see lot of ppl talks about ❔ Blazor: darkmode not workingAnyone familiar with the NDC talk 'Building beautiful Blazor apps with Tailwind CSS' by Chris Sainty✅ How to check user's target framework moniker in my source generator?I'm using `IIncrementalGenerator`. I want to generate different code for the user based on his proje✅ Whats the proper way to access Windows.Gaming.Input in a non-UWP C# project?I'd like to make a winform that uses a racing wheel for kicks and giggles, but the only way I can fi❔ string[]' does not contain a definition for 'FirstOrDefault' and no accessible extension method...This is probably a simple mistake so I apologize in advance. ```c# using System; using System.Diagn✅ Unable to read beyond the end of the stream.Hello, i have error : Unable to read beyond the end of the stream. at line "short hiheader = reade