簡體   English   中英

無法連接到C#中的另一台計算機

[英]unable to connect to another computer in C#

嗨,伙計們,我用C#編寫了一個簡單的程序,將程序從服務器發送到客戶端。

現在,我已經成功測試了在同一台計算機上運行的兩個程序。 但是,當我嘗試連接不同網絡上的2台不同計算機時,它向我發送了無法連接的消息。

這里是服務器代碼。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace chat_client_console
{
    class Program
    {
        static TcpListener listener;
        static void Main(string[] args)
        {
            /*
            string name = Dns.GetHostName();
            IPAddress[] address = Dns.GetHostAddresses(name);


            foreach(IPAddress addr in address)
            {
                Console.WriteLine(addr);
            }

            Console.WriteLine(address[2].ToString());*/
            Console.WriteLine("server");
            listener = new TcpListener(IPAddress.Any, 2055);

            listener.Start();

            Socket soc = listener.AcceptSocket();
            Console.WriteLine("Connection successful");
            Stream s = new NetworkStream(soc);

            StreamReader sr = new StreamReader(s);
            StreamWriter sw = new StreamWriter(s);

            sw.AutoFlush = true;
            sw.WriteLine("A test message");
            sw.WriteLine("\n");
            Console.WriteLine("Test message delivered. Now ending the program");

            /*
            string name = Dns.GetHostName();
            Console.WriteLine(name);
            //IPHostEntry ip = Dns.GetHostEntry(name);
            //Console.WriteLine(ip.AddressList[0].ToString());
            IPAddress[] adr=Dns.GetHostAddresses(name);
            foreach (IPAddress adress in adr)
            {
                Console.WriteLine(adress);
            }
            */
            Console.ReadLine();
        }
    }
}

這里是客戶端代碼。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;

namespace chat_client_console_client
{
    class Program
    {
        static void Main(string[] args)
        {
            string host_ip_address;
            Console.WriteLine("Enter server ip address");
            host_ip_address=Console.ReadLine();
            string display;
            TcpClient client = new TcpClient(host_ip_address, 2055);
            Stream s = client.GetStream();
            Console.WriteLine("Connection successfully received");

            StreamWriter sw = new StreamWriter(s);
            StreamReader sr = new StreamReader(s);
            sw.AutoFlush = true;
            /*while (true)
            {

                display = sr.ReadLine();

                if (display == "")
                {
                    Console.WriteLine("breaking stream");
                    break;
                }

            }*/

            display = sr.ReadLine();

            Console.WriteLine(display);
            Console.ReadLine();
        }
    }
}

現在,當我在客戶端程序中輸入127.0.0.1時,它成功連接到服務器並收到了消息。

但是,當我在另一台計算機上運行的客戶端程序中輸入我的外部IP地址時,我無法連接。

在此問題上需要建議。

謝謝。

您可以在客戶端計算機中使用Wireshark並查找任何tcp數據包,以確保將消息發送到服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM