none
TCP Connection Socket Error RRS feed

  • Question

  • Hello Everyone,

    I am working on a messaging client with some friends as an interesting project for spring break.

    As I'm working on the project I've found that the server responds locally on my computer but when I open it to the internet through port 65500, it errors out with "System.Net.Sockets.SocketException: 'An attempt was made to access a socket in a way forbidden by its access permissions" and I have no idea how to fix this issue.

    My client code:

    public static String RecentMessage = null;
    
            static void Main(string[] args)
            {
                TcpClient client = new TcpClient();
                client.Connect("xxx.xxx.xxx.xxx", 65500);
                Console.WriteLine("Client Connected!");
                NetworkStream ns = client.GetStream();
                Thread thread = new Thread(o => ReceiveData((TcpClient)o));
                thread.Start(client);
    
                while (!string.IsNullOrEmpty(RecentMessage = Console.ReadLine()))
                {
                    byte[] buffer = Encoding.ASCII.GetBytes(RecentMessage);
                    ns.Write(buffer, 0, buffer.Length);
                }
    
                client.Client.Shutdown(SocketShutdown.Send);
                thread.Join();
                ns.Close();
                client.Close();
                Console.WriteLine("Disconnected From Server");
                Console.ReadKey();
            }
    
            static void ReceiveData(TcpClient client)
            {
                NetworkStream ns = client.GetStream();
                byte[] receivedBytes = new byte[1024];
                int byte_count;
    
                while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0)
                {
                    String Recieved = Encoding.ASCII.GetString(receivedBytes, 0, byte_count).TrimEnd();
                    if (!RecentMessage.Contains(Recieved))
                    {
                        Console.Write(">> " + Recieved);
                    }
                }
            }

    My Server Code:

    class Program
        {
            public static List<Client> Clients = new List<Client>();
            public static List<Chat> Chats = new List<Chat>();
    
            static void Main(string[] args)
            {
                int counter = 1;
    
                TcpListener ServerConnection = new TcpListener(IPAddress.Any, 65500);
                ServerConnection.Start();
    
                while (true)
                {
                    TcpClient client = ServerConnection.AcceptTcpClient();
                    Clients.Add(new Client(counter.ToString(), client));
                    counter++;
                }
            }
        }
    
        class Client
        {
            public String ID { get; set; }
            public String Username { get; set; }
            public TcpClient Connection {get; set;}
            public List<Chat> Chats { get; set; }
    
            public Client(String id, TcpClient Connect)
            {
                ID = id;
                Connection = Connect;
                Chats = new List<Chat>();
                Thread Start = new Thread(new ThreadStart(ClientControl));
                Start.Start();
            }
    
            public void ClientControl()
            {
                ClientManager.ManageClient(this);
            }
    
        }
    
    class ClientManager
        {
            public static void ManageClient(Client C)
            {
                while (true)
                {
                    try
                    {
                        NetworkStream stream = C.Connection.GetStream();
                        byte[] buffer = new byte[2048];
                        int byte_count = stream.Read(buffer, 0, buffer.Length);
    
                        if (byte_count == 0)
                        {
                            break;
                        }
    
                        string data = Encoding.ASCII.GetString(buffer, 0, byte_count);
                        broadcast(data);
                        Console.WriteLine(data);
                    }
                    catch
                    {
                        C.Connection.Close();
                        Program.Clients.Remove(C);
                    }
                }
    
    
            }
    
            public static void broadcast(string data)
            {
                byte[] buffer = Encoding.ASCII.GetBytes(data + Environment.NewLine);
    
                foreach (Client C in Program.Clients)
                {
                    NetworkStream Stream = C.Connection.GetStream();
                    Stream.Write(buffer, 0, buffer.Length);
                }
            }
        }
    
    Please let me know what I've done wrong and what I can do to fix it.

    Also, I've port forwarded the port on the router and the server has the firewall rules needed to accept the connection.

    Thanks in advance for the responses,

    Nate


    Monday, March 25, 2019 3:19 PM

Answers

  • Hi

    Thank you for posting here.

    For your question, you want to solve the error that ’ An attempt was made to access a socket in a way forbidden by its access permissions’.

    I have tested your code, it works fine.

    Based on my search, the application is trying to open a port that is already used – One of the most common reasons why this error occurs is if you try to use an application that is configured to open a port that is already opened and actively used by a different service or application.

    So I suggest that you don’t open a port that is already used when you going on the TCP project.

    Hope my explanation could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by NHastings25 Tuesday, October 22, 2019 8:36 PM
    Tuesday, March 26, 2019 8:34 AM
    Moderator

All replies

  • Port numbers above 49,152 are used for a special purpose.  Does this work if you use port 48000 instead?

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, March 26, 2019 6:07 AM
  • Hi

    Thank you for posting here.

    For your question, you want to solve the error that ’ An attempt was made to access a socket in a way forbidden by its access permissions’.

    I have tested your code, it works fine.

    Based on my search, the application is trying to open a port that is already used – One of the most common reasons why this error occurs is if you try to use an application that is configured to open a port that is already opened and actively used by a different service or application.

    So I suggest that you don’t open a port that is already used when you going on the TCP project.

    Hope my explanation could be helpful.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by NHastings25 Tuesday, October 22, 2019 8:36 PM
    Tuesday, March 26, 2019 8:34 AM
    Moderator