tcp/ip客户服务器在互联网上无法工作[英] tcp/ip client server not working over internet

本文是小编为大家收集整理的关于tcp/ip客户服务器在互联网上无法工作的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我将在TCP/IP模式下设置一个小客户端/服务器,我使用VS2010,C#开发我的应用程序,我已经搜索了很多搜索并可以找到一些源代码,但是它们都没有工作互联网,我可以在自己的本地系统中获得一些答案,即我运行服务器,然后收听我自己的Localhost(127.0.0.1),然后发送一些数据(例如使用Telnet),它可以正常工作,但是当我这样做时,在互联网上我什么都没有!我想使用端口80,因为我想发送/接收http数据,我已经测试了几个源代码,这是我使用的最后一个代码(它在localhost上使用telnet都可以使用)

//服务器代码:

form_load()
    IPAddress localAddress = IPAddress.Parse("127.0.0.1");
                Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 80);

            // Bind the socket to the end point
            listenSocket.Bind(ipEndpoint);

            // Start listening, only allow 1 connection to queue at the same time
            listenSocket.Listen(1);
            listenSocket.BeginAccept(new AsyncCallback(ReceiveCallback), listenSocket);
            Console.WriteLine("Server is waiting on socket {0}", listenSocket.LocalEndPoint);

            // Start being important while the world rotates
            while (true)
            {
                 Console.WriteLine("Busy Waiting....");
                Thread.Sleep(2000);
            }

        public static void ReceiveCallback(IAsyncResult AsyncCall)
    {
         System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        Byte[] message = encoding.GetBytes("I am a little busy, come back later!");

        Socket listener = (Socket)AsyncCall.AsyncState;
        Socket client = listener.EndAccept(AsyncCall);

        Console.WriteLine("Received Connection from {0}", client.RemoteEndPoint);
        client.Send(message);

        Console.WriteLine("Ending the connection");
        client.Close();
        listener.BeginAccept(new AsyncCallback(ReceiveCallback), listener);
    }

发送数据(客户端),我当然没有使用此代码,对吗?

        public static string SendData()
    {
        TcpClient client = new TcpClient();
        client.Connect(IP, 80);
        StreamWriter sw = new StreamWriter(client.GetStream());
        StreamReader sr = new StreamReader(client.GetStream());

        //if statement evalutes to see if the user has selected to update the server
        //" " = update server
        //"" = do not update the server
        //if (updateData.Equals(""))
        //{
        //    space = "";
        //}
        //else if (!updateData.Equals(""))
        //{
        //    space = " ";
        //}
        //Refrences stream writer, username variable passed in from GUI
        //space variable provides update function: "" = dont update. " " = update database.
        sw.WriteLine("h");
        sw.Flush();
        //data send back from the server assigned to string variable 
        //string recieved = sr.ReadLine();
        return "";

    }

我将在服务器中使用服务器代码(WinServer 2008R2),但目前在普通PC中对其进行测试,我在做什么错?我想将一些HTTP数据包从随机系统(带有随机IP)发送到我的服务器(我知道它的IP),该怎么办? TCP/IP是否可以做其他事情? 它与静态IP有关吗?我当然应该有静态IP吗?我的Web服务器具有静态IP,但我的客户端没有,这是问题吗? 我认为我在定义端口和IP时有一些问题,该如何设置它们?我的服务器有一个特定的IP,但我不知道我的客户端,您能逐步向我解释吗? 谢谢

推荐答案

在这种情况下最常见的两个问题:

  1. 确保服务器的路由器正在使用端口转发将http请求从路由器转发到服务器.
  2. 确保您连接到服务器的公共IP地址而不是其本地网络地址.

本文地址:https://www.itbaoku.cn/post/2339532.html

问题描述

I'm going to setup a small client/server server in TCP/IP mode, I use VS2010,C# to develop my apps, I've googled a lot and could find some source codes, but none of them work in internet, I can get some answers in my own local system, i.e. I run my server, then listen for my own localhost (127.0.0.1) then send some data (for example using telnet), it works fine but when I do the same over internet I get nothing! I want to use port 80, as I want to send/receive http data, I have tested several source codes, here is the last code I have used (and it works on localhost with telnet)

//server code:

form_load()
    IPAddress localAddress = IPAddress.Parse("127.0.0.1");
                Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 80);

            // Bind the socket to the end point
            listenSocket.Bind(ipEndpoint);

            // Start listening, only allow 1 connection to queue at the same time
            listenSocket.Listen(1);
            listenSocket.BeginAccept(new AsyncCallback(ReceiveCallback), listenSocket);
            Console.WriteLine("Server is waiting on socket {0}", listenSocket.LocalEndPoint);

            // Start being important while the world rotates
            while (true)
            {
                 Console.WriteLine("Busy Waiting....");
                Thread.Sleep(2000);
            }

        public static void ReceiveCallback(IAsyncResult AsyncCall)
    {
         System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        Byte[] message = encoding.GetBytes("I am a little busy, come back later!");

        Socket listener = (Socket)AsyncCall.AsyncState;
        Socket client = listener.EndAccept(AsyncCall);

        Console.WriteLine("Received Connection from {0}", client.RemoteEndPoint);
        client.Send(message);

        Console.WriteLine("Ending the connection");
        client.Close();
        listener.BeginAccept(new AsyncCallback(ReceiveCallback), listener);
    }

send data (client), of course I haven't used this code, is it right?

        public static string SendData()
    {
        TcpClient client = new TcpClient();
        client.Connect(IP, 80);
        StreamWriter sw = new StreamWriter(client.GetStream());
        StreamReader sr = new StreamReader(client.GetStream());

        //if statement evalutes to see if the user has selected to update the server
        //" " = update server
        //"" = do not update the server
        //if (updateData.Equals(""))
        //{
        //    space = "";
        //}
        //else if (!updateData.Equals(""))
        //{
        //    space = " ";
        //}
        //Refrences stream writer, username variable passed in from GUI
        //space variable provides update function: "" = dont update. " " = update database.
        sw.WriteLine("h");
        sw.Flush();
        //data send back from the server assigned to string variable 
        //string recieved = sr.ReadLine();
        return "";

    }

I'm going to have the server code in my Server (winserver 2008R2) but currently test it in normal PCs, what am I doing wrong? I want to send some http packet from a random system (with a random IP) to my server (which I know its IP), what should I do? is it possible with tcp/ip or I should do something else? is it related to static IP? should I certainly have static IP? my web server has a static IP but my clients do not, is it a problem? I think I have some problem in defining ports and IPs, how should I set them? my server has a specific IP, but I don't know IP of my clients, would you please explain it to me step by step? thanks

推荐答案

The two most common problems in this scenario:

  1. Ensure your server's router is using port forwarding to forward HTTP requests from the router to the server.
  2. Ensure you are connecting to the server's public IP address, not its local network address.