C((xi)プログラミングでIPアドレスを取得する方法の例
本明細書の例は、C〓のプログラミングによるIPアドレスの取得方法を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
1、クライアントIPの取得
IPアドレスホームオンラインクエリツール:
http://tools.jb51.net/aideddesign/ipcha
オンラインIPアドレス/サブネットマスク計算と変換ツール:
http://tools.jb51.net/aideddesign/ip_netcall c
オンラインネットワーク計算機|TCP/IPサブネットマスク計算と換算ツール:
http://tools.jb51.net/aideddesign/ipcalc
C関連の内容についてもっと興味がある読者は、当駅のテーマを見ることができます。「C〓〓〓プログラムの設計のスレッドは技巧を使って総括します。」、「WinFormコントロールの使い方のまとめ」、「C〓中XMLファイルの操作技術のまとめ」、「C〓〓よくあるコントロールの使い方教程」、「C〓〓〓データ構造とアルゴリズム教程」、「Cハハハハ配列操作技術のまとめ」および「C铅対象プログラム設計入門教程」
ここで述べたように、皆さんのC〓プログラムの設計に役に立ちます。
1、クライアントIPの取得
/// <summary>
/// Ip
/// </summary>
/// <returns></returns>
public String GetClientIp()
{
String clientIP = "";
if (System.Web.HttpContext.Current != null)
{
clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(clientIP) || (clientIP.ToLower() == "unknown"))
{
clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
if (string.IsNullOrEmpty(clientIP))
{
clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
}
else
{
clientIP = clientIP.Split(',')[0];
}
}
return clientIP;
}
2、サーバ側がクライアント要求IPとクライアントマシン名を取得する
/// <summary>
/// IP
/// </summary>
public static void GetClientInfo()
{
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
HttpRequestMessageProperty requestProperty = messageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
string clientIp = !string.IsNullOrEmpty(requestProperty.Headers["X-Real-IP"]) ? requestProperty.Headers["X-Real-IP"] : endpointProperty.Address;
string clientName = Environment.MachineName;
Console.WriteLine("ClientIp: " + clientIp + "clientName:" + clientName);
}
PS:ここではIP関連のツールをいくつか紹介します。IPアドレスホームオンラインクエリツール:
http://tools.jb51.net/aideddesign/ipcha
オンラインIPアドレス/サブネットマスク計算と変換ツール:
http://tools.jb51.net/aideddesign/ip_netcall c
オンラインネットワーク計算機|TCP/IPサブネットマスク計算と換算ツール:
http://tools.jb51.net/aideddesign/ipcalc
C関連の内容についてもっと興味がある読者は、当駅のテーマを見ることができます。「C〓〓〓プログラムの設計のスレッドは技巧を使って総括します。」、「WinFormコントロールの使い方のまとめ」、「C〓中XMLファイルの操作技術のまとめ」、「C〓〓よくあるコントロールの使い方教程」、「C〓〓〓データ構造とアルゴリズム教程」、「Cハハハハ配列操作技術のまとめ」および「C铅対象プログラム設計入門教程」
ここで述べたように、皆さんのC〓プログラムの設計に役に立ちます。