Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
himadrinath opened this issue May 17, 2024 · 3 comments
Open

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

himadrinath opened this issue May 17, 2024 · 3 comments

Comments

@himadrinath
Copy link

himadrinath commented May 17, 2024

@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

@himadrinath himadrinath changed the title no exception raised no running both server in same port no exception raised on running both server in same port May 17, 2024
@burakoner
Copy link
Owner

Inspecting...

@burakoner
Copy link
Owner

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
Copy link
Author

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");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants