无法连接到C#中的另一台计算机

时间:2012-09-13 14:32:46

标签: c# .net sockets tcp

hello guyz我在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地址时,我无法连接。

此问题需要提出建议。

谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在客户端计算机中使用Wireshark并查找任何tcp数据包以确保将消息发送到服务器。