A
A
Alexander Ivanov2021-08-10 13:37:30
Computer networks
Alexander Ivanov, 2021-08-10 13:37:30

Why can't the client reach the server in a c# application?

There is a simple client-server application written according to the manuals in c#. If you run the server and the client on the same machine, then the client can easily communicate with the server. But if I run the server on a VPS with a dedicated white ip, then the client cannot reach the server. Moreover, the server has a port open for sure and the firewall has all the permissions for incoming and outgoing connections. I tried to run the server also on a physical computer with a dedicated ip and an open port - it still does not work. What is the reason gentlemen? I am attaching the server and client code as they are quite small.

server:

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.Http.Headers;

namespace server
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Однопоточный сервер запущен!");
            IPHostEntry iPHost = Dns.GetHostEntry("localhost");
            IPAddress ipAddr = iPHost.AddressList[0];
            
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 1005);
            


            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            try
            {
                sock.Bind(ipEndPoint);
                sock.Listen(10);
               
                while (true)
                {
                    Console.WriteLine("Слушаем, порт {0}", ipEndPoint);

                    Socket s = sock.Accept();
                    
                    string data = null;
                    byte[] bytes = new byte[1024];
                    int bytesCount = s.Receive(bytes);
                    data += Encoding.UTF8.GetString(bytes, 0, bytesCount);
                    Console.Write("Данные от клиента: " + data + "\n\n");
                    string reply = "Query size: " + data + data.Length.ToString() + " chars";
                    byte[] msg = Encoding.UTF8.GetBytes(reply);
                   


                    if (data.IndexOf("<TheEnd>") > -1)
                    {
                        Console.WriteLine("Соединение завершено.");
                        break;
                    }
                    if (data.IndexOf("<sss>") > -1)
                    {
                        
                        Console.WriteLine("работает");
                        break;
                    }
                    s.Shutdown(SocketShutdown.Both);
                    s.Close();

                    

                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}


client:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace client
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Communicate("195.133.144.88", 1005);


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {

                Console.ReadLine();
            }
        }

        static void Communicate(string hostname, int port)
        {
            byte[] bytes = new byte[1024];
            IPHostEntry ipHost = Dns.GetHostEntry(hostname);
            IPAddress ipAddr = ipHost.AddressList[0];
            IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, port);

            Socket sock = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            sock.Connect(ipEndPoint);
            Console.Write("Введите сообщение: ");
            string message = Console.ReadLine();

            Console.WriteLine("Подключаемся к порту {0} ", sock.RemoteEndPoint.ToString());
            byte[] data = Encoding.UTF8.GetBytes(message);
            int bytesSent = sock.Send(data);

            int bytesRec = sock.Receive(bytes);
            Console.WriteLine("\nОтвет сервера: {0}\n\n", Encoding.UTF8.GetString(bytes, 0, bytesRec));

            if (message.IndexOf("<TheEnd>") == -1)
                Communicate(hostname, port);
            sock.Shutdown(SocketShutdown.Both);
            sock.Close();
        }
    }
}


ip address in the client example is real - this is the ip address of the vps on which the server of this program is currently running

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question