【问题标题】:tcp/ip client server not working over internettcp/ip 客户端服务器无法通过 Internet 工作
【发布时间】:2014-08-12 18:16:27
【问题描述】:

我打算在 TCP/IP 模式下设置一个小型客户端/服务器服务器,我使用 VS2010,C# 来开发我的应用程序,我在 google 上搜索了很多,可以找到一些源代码,但没有一个可以工作互联网,我可以在我自己的本地系统中得到一些答案,即我运行我的服务器,然后监听我自己的本地主机(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 上测试它,我做错了什么?我想从一个随机系统(使用随机 IP)向我的服务器(我知道它的 IP)发送一些 http 数据包,我该怎么办? tcp/ip 有可能还是我应该做点别的? 它与静态IP有关吗?我当然应该有静态IP吗?我的 Web 服务器有一个静态 IP,但我的客户端没有,这是一个问题吗? 我想我在定义端口和 IP 时遇到了一些问题,我应该如何设置它们?我的服务器有一个特定的IP,但我不知道我的客户的IP,请你一步一步解释给我好吗? 谢谢

【问题讨论】:

    标签: c# tcp


    【解决方案1】:

    此场景中最常见的两个问题:

    1. 确保服务器的路由器使用port forwarding 将HTTP 请求从路由器转发到服务器。
    2. 确保您连接到服务器的public IP address,而不是其本地网络地址。

    【讨论】:

    • 谢谢,但是我怎么知道服务器路由器呢?我可以使用自己的 PC 作为测试服务器吗?这样我该如何测试路由器?服务器公共IP地址是什么意思?我怎样才能找到它?
    • 谢谢,但我找不到明确的答案,是否有一个很好的教程/示例来展示在网络上工作的东西?
    猜你喜欢
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多