burakoner / TcpSharp

Faster TCP Server and Client tools using low-level implementations
MIT License
15 stars 4 forks source link

no exception raised on running both server in same port #8

Open himadrinath opened 3 months ago

himadrinath commented 3 months ago

@burakoner started server on 8080 one in on debug mode and other one in without debug mode.

but it didn't raised any exception that telling me that the port is busy or something.

is this right behavior

burakoner commented 3 months ago

Inspecting...

burakoner commented 3 months ago

I got an exception as below

System.Net.Sockets.SocketException: 'Only one usage of each socket address (protocol/network address/port) is normally permitted.'

Screenshot 2024-05-17 175449

himadrinath commented 3 months ago

from your example code

i tried out one without debug and then next one with debug no exception

using System.Text;
using TcpSharp;

internal class Program
{
    static TcpSharpSocketServer server;
    static void Main(string[] args)
    {
        server = new TcpSharpSocketServer(8080);
        server.OnStarted += Server_OnStarted;
        server.OnStopped += Server_OnStopped;
        server.OnConnectionRequest += Server_OnConnectionRequest;
        server.OnConnected += Server_OnConnected;
        server.OnDisconnected += Server_OnDisconnected;
        server.OnDataReceived += Server_OnDataReceived;
        server.OnError += Server_OnError;
        server.StartListening();

        Console.WriteLine("TCP Server is listening on port " + server.Port);

        System.Timers.Timer timer = new System.Timers.Timer(1000);
        timer.Elapsed += Timer_Elapsed;
        //timer.Enabled = true;

        Console.ReadLine();
    }

    private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine($"Received Bytes: {bytesReceived}");
    }

    private static void Server_OnStarted(object sender, OnServerStartedEventArgs e)
    {
        Console.WriteLine("Server_OnStarted");
    }

    private static void Server_OnStopped(object sender, OnServerStoppedEventArgs e)
    {
        Console.WriteLine("Server_OnStopped");
    }
    private static void Server_OnConnectionRequest(object sender, OnServerConnectionRequestEventArgs e)
    {
        Console.WriteLine($"Server_OnConnectionRequest. IPEndPoint: {e.IPEndPoint} Address {e.IPAddress}:{e.Port}");
        //e.Accept = false;
    }

    private static void Server_OnConnected(object sender, OnServerConnectedEventArgs e)
    {
        Console.WriteLine($"Server_OnConnected. ConnectionId: {e.ConnectionId} Address {e.IPAddress}:{e.Port}");
    }

    private static void Server_OnDisconnected(object sender, OnServerDisconnectedEventArgs e)
    {
        Console.WriteLine("Server_OnDisconnected");
    }

    static long bytesReceived = 0;
    private static void Server_OnDataReceived(object sender, OnServerDataReceivedEventArgs e)
    {
        // bytesReceived += e.Data.Length;
        // server.SendBytes(e.ConnectionId, Encoding.UTF8.GetBytes("Sana da selam!"));
        // Console.WriteLine("Server_OnDataReceived: "+ Encoding.UTF8.GetString(e.Data));
        // Console.WriteLine("Server_OnDataReceived: Packet Size: "+ e.Data.Length);
        if (e.Data.Length < 20)
        {
            var data = Encoding.UTF8.GetString(e.Data);
            Console.WriteLine("Server_OnDataReceived: " + data);
            server.SendString(e.ConnectionId, "Echo: " + data);
        }
    }

    private static void Server_OnError(object sender, OnServerErrorEventArgs e)
    {
        Console.WriteLine("Server_OnError");
    }
}