【问题标题】:TCP/IP client server Communication over different network in c#TCP / IP客户端服务器在c#中通过不同网络进行通信
【发布时间】:2021-03-12 11:03:35
【问题描述】:

我能够使用 TCP/IP 在客户端和服务器之间建立通信。客户端和服务器只能在服务器和客户端在同一网络中时才能相互发送和接收消息,如果两者都在不同的网络中,则客户端不连接server. 当他们在不同的网络时应该怎么做?请帮忙。这是服务器和客户端代码。谢谢。

//Server Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {

        try
        {
            IPAddress ipAd = IPAddress.Parse("192.168.0.110"); //use local       m/c IP address, and use the same in the client

            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, 8001);

            /* Start Listeneting at the specified port */
            myList.Start();

            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is  :" +   myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");

            Socket s = myList.AcceptSocket();
            Console.WriteLine("Connection accepted from " +  s.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = s.Receive(b);
            Console.WriteLine("Recieved...");
            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(b[i]));
            Console.WriteLine("Enter the string to be strasmitted");

            String str = Console.ReadLine();

            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes(str));
            Console.WriteLine("\nSent Acknowledgement");
            /* clean up */
            s.Close();
            myList.Stop();

        }
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
        Console.ReadLine();
     }

    }
   }

   //Client Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace ConsoleApplication4
{
class Program
{
    static void Main(string[] args)
    {
        try
        {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");

            tcpclnt.Connect("192.168.0.110", 8001); // use the ipaddress as   in the server program

            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");

            String str = Console.ReadLine();
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");

            stm.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int k = stm.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(bb[i]));

            tcpclnt.Close();
        }

        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
        Console.ReadLine();
     }
    }
 }

【问题讨论】:

    标签: c# sockets networking tcp-ip


    【解决方案1】:

    您的服务器正在侦听来自192.168.0.110 端口8001 的连接

    IPAddress ipAd = IPAddress.Parse("192.168.0.110");
    TcpListener myList = new TcpListener(ipAd, 8001);
    

    客户端连接到服务器的 IP 是:192.168.0.110。服务器在同一个网络上。因此,如果服务器已打开并正在侦听连接,这将起作用。

    tcpclnt.Connect("192.168.0.110", 8001);
    

    所以一切都很好,但是如果您的客户端在不同的网络上,它当然找不到服务器,因为客户端会在本地网络中查找 IP:192.168.0.110:8001(本地服务器 IP)并且没有服务器那会听那个的。

    因此,要完成这项工作,您必须在服务器网络上端口转发您的路由器。在路由器中设置端口:8001 上的每个传入连接都将重定向到 192.168.0.110:8001(本地服务器 IP)。

    例如,如果您的公共 IP 是 10.10.10.10,那么您应该让您的客户端像这样连接:

    tcpclnt.Connect("10.10.10.10", 8001);
    

    因此,如果您的客户端连接到10.10.10.10:8001,路由器会将其重定向到本地网络上的服务器,并且客户端将能够连接到服务器。

    /*Server example for listening. (IPAddress.any) means any IP will be allowed to connect*/
    TcpListener myList = new TcpListener(IPAddress.Any,8001);
    

    !!请注意,每个路由器的端口转发方式都不同,因此在谷歌上快速搜索将帮助您如何做到这一点。

    【讨论】:

    • 我的错误是我在客户端和服务器代码中使用了相同的 ip,但错误地发布了。我刚刚纠正了我的问题。尽管我会尝试你给定的解决方案。
    • 没问题,我已经更新了我的代码。但是这个解决方案应该可以工作,但我以同样的方式做到了,但与 socked 类不同。所以我会很好奇它是否有效。
    • @Timson Post 仍然无法正常工作。我没有按照您所说的进行端口转发部分,因为我无法访问我的路由器。除了端口转发之外还有其他方法吗,比如在 Windows 中进行一些更改主机文件。
    • @ValarDohaeris stackoverflow.com/questions/26523935/…。这也是关于这个主题的一个问题。来自本地网络之外的人不能简单地连接到或查看本地网络上不安全的本地 IP。做到这一点的一种方法是端口转发使端口可从外部启用并将其指向您的服务器 IP。解决此问题的一种方法是要求您的网络管理员为您打开一个端口。不幸的是,您的主机文件中的更改也不会起作用。
    猜你喜欢
    • 2013-01-28
    • 1970-01-01
    • 2012-06-29
    • 2021-07-29
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多