2011-01-27 35 views
0

私はローカルにホストされているサーバに接続するクライアントを作り、サーバから在庫番号を取得しています。プログラムは、私は以下のこのコードを使用する場合は動作しますが、それが動作する方法は理論的にはwww.website.comを取るので、DNS名を取得することですと私はそれを得る方法を私は127.0の通常のIPを認識することができます。 0.1またはローカルホストIP:添付ホスト名でIPアドレスを取得する方法は?

IPHostEntry ipHostInfo = Dns.GetHostEntry("www.website.com"); 
      IPAddress ipAddress = ipHostInfo.AddressList[0]; 
      IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 

が、これは、IPを解決するために得るために、私attempですが、私は完全なコードはここで見ることができ、この権利に近づいていますと思ういけない:StockReader Client Code

public class AsynchronousClient 
{ 

    private const int port = 21; 

    // ManualResetEvent instances signal completion. 
    private static ManualResetEvent connectDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent sendDone = 
     new ManualResetEvent(false); 
    private static ManualResetEvent receiveDone = 
     new ManualResetEvent(false); 

    // The response from the remote device. 
    private static String response = String.Empty; 

    private static void StartClient() 
    { 
     // Connect to a remote device. 
     try 
     { 
      // Establish the remote endpoint for the socket. 
      // The name of the 

     //******************ISSUE BEGINS HERE********************************* 
      string sHostName = Dns.GetHostName(); 
      IPHostEntry ipHostInfo = Dns.GetHostEntry(sHostName); 
      IPAddress [] ipAddress = ipHostInfo.AddressList; 
      IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 

      // Create a TCP/IP socket. 
      Socket client = new Socket(AddressFamily.InterNetwork, 
       SocketType.Stream, ProtocolType.Tcp); 

      // Connect to the remote endpoint. 
      client.BeginConnect(remoteEP, 
       new AsyncCallback(ConnectCallback), client); 
      connectDone.WaitOne(); 

      // Send test data to the remote device. 
      Send(client, "This is a test<EOF>"); 
      sendDone.WaitOne(); 

      // Receive the response from the remote device. 
      Receive(client); 
      receiveDone.WaitOne(); 

      // Write the response to the console. 
      Console.WriteLine("Response received : {0}", response); 

      // Release the socket. 
      client.Shutdown(SocketShutdown.Both); 
      client.Close(); 

     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 
    } 

答えて

0

ますipAddressの値を設定すると、IPAddress.Parseメソッドを使用して文字列を渡し、IPAddress objを取得できます電気ショック療法の代わりにドメイン名を使用する:

string ip = "127.0.0.1"; 
IPAddress address = IPAddress.Parse(ipAddress); 
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 
0

を使用してみてくださいstring sHostName = "localhost";

0
// Establish the remote endpoint for the socket. 
IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); 
IPEndPoint remoteEP = new IPEndPoint(ipAddress, portnumber); 

// Create a TCP/IP socket. 
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
s.Connect(remoteEP); 
関連する問題