C
C#15mo 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
Bailey15mo ago
Hi, private static async void OnClientConnected() is a async method which will run async. you should call is using: await OnClientConnected();
Slycex
SlycexOP15mo 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ß15mo ago
@slycex correct you wanna go async all the way up the calling stack
Slycex
SlycexOP15mo ago
Yup !
Slycex
SlycexOP15mo ago
No description
Slycex
SlycexOP15mo ago
Here is the final program and it works perfectly! Thanks for all
Florian Voß
Florian Voß15mo ago
remember to /close when this issue has been solved @slycex
Slycex
SlycexOP15mo ago
Oh ok !
Want results from more Discord servers?
Add your server