Kaung San
Kaung San

Reputation: 79

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

In the following code, the Server is listening at port 1214. If the client connect to the Server, Server Broadcast acknowledge message "Hi".

The problem is

Please Help me

Server code - listen all the incoming connection at port 1214 and once connected reply "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);
            }
        }

        }
    }
}

Client - ask user to type IP address. If IP address is the socket listening at port 1214, acknowledge message "HI" is received.

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();
        }
    }

Thanks in advance

Upvotes: 0

Views: 2359

Answers (1)

Nicholas Carey
Nicholas Carey

Reputation: 74317

AFAIK, the only way to find out if a particular IP address is listening to a particular socket, is to try to open a connection to it. And iterating over every possible IP address in a network might...time consuming.

You might want to take a look at ZeroConf, which provide a mechanism for service discovery via DNS. OS X comes with built-in support for ZeroConf and service discovery (Bonjour). Other OSes...might be trickier.

More on ZeroConf at

More about its Service Discovery at

Another approach, given your constraint that its on a local network might be to have your server(s) advertising their presence via a UDP broadcast datagrams...

However, that will almost certainly be limited by your network topography and firewall/router rules.

Upvotes: 0

Related Questions