簡體   English   中英

C#套接字:如何僅獲取服務器的IP地址,而無需在客戶端鍵入或固定IPAddress

[英]C# Socket: How to get the IP addresses of Servers only without typing or fixing IPAddress in client side

在下面的代碼中,服務器正在偵聽端口1214。如果客戶端連接到服務器,則服務器廣播確認消息“ Hi”。

問題是

  • 我不想在客戶端修復或鍵入IP地址
  • 如何掃描在同一局域網內端口“ 1214”上偵聽的服務器的所有IP地址。 並過濾(在列表框中顯示)僅答復“ Hi”消息的IP地址?

請幫我

服務器代碼 -監聽端口1214上的所有傳入連接,連接后回復“ HI”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace PortScanServer
{
    class Program
    {

        static void Main(string[] args)
        {

            string IP_Adress = "";
        IPHostEntry localComputer = Dns.Resolve("KAUNGSAN-PC");
        IPAddress[] localIP = localComputer.AddressList;

        for (int i = 0; i < localIP.Length; i++) {
            IP_Adress = IP_Adress + localIP[i];
        }

        while (true) {
            try {
                IPAddress ipAd = IPAddress.Parse(IP_Adress);
                TcpListener listener1 = new TcpListener(ipAd, 1214);

                listener1.Start();

                Console.WriteLine("Server is running on portt 1214...");

                Socket s = listener1.AcceptSocket();
                Console.WriteLine("Connection accepted from this IP: " + s.RemoteEndPoint);

                byte[] b = new byte[100];
                int k = s.Receive(b);
                Console.WriteLine("Recieved a bunch of byytes:");

                    for (int i = 0; i < k; i++) {
                       Console.Write(Convert.ToChar(b[i]));
                    }

                ASCIIEncoding asen = new ASCIIEncoding();
                // Only send these bytes if you want the scanner to identify that
                // you are not Kazaa
                s.Send(asen.GetBytes("Hi"));
                Console.WriteLine("\nI sent the client aknowledgement");

                s.Close();
                listener1.Stop();

            }

            catch (Exception e) {
                Console.WriteLine("Ah, poo, an error: " + e);
            }
        }

        }
    }
}

客戶端 -要求用戶鍵入IP地址。 如果IP地址是偵聽端口1214的套接字,則會收到確認消息“ HI”。

using System.IO;
using System.Net.Sockets;
using System.Text;

namespace TestPortScan
{
    class Program
    {
        static void Main(string[] args)
        {
            bool sucess;
            String response = "";

            try
            {
                TcpClient myTCPclient = new TcpClient();
                Console.Write("Enter IP of Target: ");
                String IP_Adress = Console.ReadLine(); Console.WriteLine("");
                myTCPclient.Connect(IP_Adress, 1214);
                Stream outputStream1 = myTCPclient.GetStream();

                ASCIIEncoding transEncoded = new ASCIIEncoding();
                byte[] byte1 = transEncoded.GetBytes("Are You Kazaa?");
                outputStream1.Write(byte1, 0, byte1.Length);

                byte[] byte2 = new byte[100];
                int k = outputStream1.Read(byte2, 0, 100);

                for (int j = 0; j < k; j++)
                {
                    response = response + Convert.ToChar(byte2[j]);
                }

                myTCPclient.Close();

                if (response == "Hi")
                {
                    sucess = true;
                }
                else
                {
                    sucess = false;
                }
            }
            catch
            {
                sucess = false;
            }

            if (sucess)
            {
                Console.WriteLine("\nThe Target Reply Hi");
            }
            else
            {
                Console.WriteLine("\nThe Target does not respond");
            }
                String waitForKey = Console.ReadLine();
        }
    }

提前致謝

AFAIK是確定特定IP地址是否正在偵聽特定套接字的唯一方法,是嘗試打開與之的連接。 並且遍歷網絡中的每個可能的IP地址可能……非常耗時。

您可能想看一下ZeroConf,它提供了一種通過DNS進行服務發現的機制。 OS X內置了對ZeroConf和服務發現(Bonjour)的支持。 其他操作系統...可能會更棘手。

更多關於ZeroConf的信息,請訪問

有關其服務發現的更多信息,請訪問:

考慮到您在本地網絡上的限制,另一種方法可能是讓您的服務器通過UDP廣播數據報來通告它們的存在...

但是,幾乎可以肯定,這將受到網絡拓撲和防火牆/路由器規則的限制。

暫無
暫無

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

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