A
A
Alexander Ivanov2020-07-24 18:50:20
C++ / C#
Alexander Ivanov, 2020-07-24 18:50:20

How to run a server in C# on a VPS?

I created a server and a client on my computer - everything works fine. I decided to upload my application server to a virtual server on Windows Server 2016, but my server does not work on it. But the fact is that the application port (18888, tried others) does not open, what I tried:
1. Turning off the firewall completely - did not help 2. Turned the
firewall back on and allowed its port in incoming and outgoing connections - did not help
that is, it cannot block the application from running.
netstat -a gives the following information:
5f1b026911e21340638278.png
this is provided that: IPHostEntry iPHost = Dns.GetHostEntry("localhost");
tried instead of localhost, enter the ip-address of the server - did not help
The client of my application cannot be connected from other computer to the server. The site 2ip.ru also writes that port 18888 is closed? How to solve the problem?
===
5f1b1d25cbf7d049581158.png
Current server code:

static void Main(string[] args)
        {
            TcpListener server = null;
            try
            {
                int MaxThreadsCount = Environment.ProcessorCount * 4;
                ThreadPool.SetMaxThreads(MaxThreadsCount, MaxThreadsCount);
                ThreadPool.SetMinThreads(2, 2);
                Int32 port = 18888;
                IPAddress ipAddr = Dns.Resolve(IPAddress.Any.ToString()).AddressList[0];
                int counter = 0;
                server = new TcpListener(ipAddr, port);

                Console.WriteLine("Конфигурация многопоточного сервера:");
                Console.WriteLine(" Порт: " + port.ToString());
                Console.WriteLine(" Потоки: " + MaxThreadsCount.ToString());
                Console.WriteLine("\nСервер запущен\n");
                server.Start();
                while (true)
                {

                    Console.Write("\nОжидаем соединения");
                    ThreadPool.QueueUserWorkItem(ClientProcessing, server.AcceptTcpClient());
                    counter++;
                    Console.Write("\nСоединение №" + counter.ToString() + "!");
                }
            }catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                server.Stop();
            }

            Console.WriteLine("\nНажмите Enter...");
            Console.Read();
        }

UPD. As I understand it, you need to somehow change the network interface from ipv6 to one of those that are highlighted in red boxes, but I don’t understand how to do this. The manipulations described above did not help, respectively. Who faced similar problem, help plz

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-07-24
@firedragon

Everything is written in the docs, in theory you need to register the binding port in application.json
After the appearance of the source code, everything became clearer.
// this is enough.
server = new TcpListener(IPAddress.Any, port);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question