0

こんにちは、私は 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 アドレスを入力すると、接続できません。

この問題では提案が必要です。

ありがとうございました。

4

1 に答える 1

0

クライアント コンピューターで Wireshark を使用し、任意の tcp パケットを検索して、メッセージがサーバーに送信されたことを確認できます。

于 2013-11-23T13:09:48.700 に答える