|  | 
	                
  
    |  |  
    | 
        
        ASP.NET C# ถามเกี่ยวกับการ Check IP Address ของ เครื่องที่เข้าใช้งาน     |  
    |  |  
 
              
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ผมใช้ตัวนี้อ่ะครับ 
 Code (VB.NET)
 
 Dim strClientIP As String = ""
strClientIP = Request.UserHostAddress()
 
 น่าจะประยุกต์ใช้ได้ไหมครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-14 11:03:14 | By :
                            golfkub |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ลองแล้วครับ 
 ออก เป็น 192.0.1.99 ครับ
 
 คือ SEVER กับ เครื่อง Client  คนละ วง Router อะครับ
 
 เวลามันติดต่อกัน มันจะผ่าน 192.0.1.99  นี้ อะครับ มันเลยแสดงเป็น 192.0.1.99  หมดเลย
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-14 11:08:04 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (C#) 
 public string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;
}
 ใช่ตามนี้ ก็
 
 192.0.1.99 อะครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-14 12:27:52 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ปัญหาเดียวกันครับ  แต่ปัญหาของผมมันยิ่งใหญ่ จนผมไม่ละความพยายาม (ณ.เวลานี้ผมก็ยังทำไม่ได้) 
 การหา IPAddress นี่เป็น SubSet (ส่วนหนึ่งของปัญหาของผมไปเลยครับ)
 
 
 Real time .
 
 อุปกรณ์ A    ---> RS232/485/USB/etc  ---> ComA ----> LocalNetWork ----> Internet ----> อุปกรณ์ A
 อุปกรณ์ B    ---> RS232/485/USB/etc ----> ComA ----> LocalNetWork ----> Internet ----> อุปกรณ์ B
 อุปกรณ์ C    ---> RS232/485/USB/etc ----> ComZ ----> LocalNetWork ----> Internet ----> อุปกรณ์ A
 ...
 ...
 ...
 Real time .
 
 [x] อุปกรณ์หมายความว่า เป็นอะไรก็ได้ที่ไม่มีชีวิต และหายใจเองไม่ได้ แต่เรา Programming มันได้
 เช่น HUB, Router, Modem, Printer, PCL, MCU, PIC, EEPROME, etc
 ผมก็อธิบายไม่ถูก เอาแบบเข้าใจได้ง่ายฯ ก็ SCADA  นั่นแหละครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-14 19:37:07 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ASP.NET C# ถามเกี่ยวกับการ Check IP Address ของ เครื่องที่เข้าใช้งาน ----- คือ เครื่องผม IP 10.0.1.5
 ----- เครื่อง SERVER IP 192.0.1.40
 ----- ถ้าให้ผมเดา น่าจะเป็น Local NetWork หรือ Intranet
 
 ผมขออนุญาติขอถามกลับว่า "สมมุติว่า ได้ IP Address มาแล้ว และถูกต้องเสมอ"
 ----- ประโยชน์ที่ได้คืออะไร?
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 01:08:48 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ประโยชน์ที่ได้คืออะไร? หมายความว่า ----- What Most Schools Don't Teach
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 01:10:27 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ip class c 192.0.x.x มันแปลกๆ นะ  
 ของจริงเปล่าเนี่ย
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 07:52:18 | By :
                            ห้ามตอบเกินวันละ 2 กระทู้ |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ของจริงครับ  192.0.1.40 
 แล้วที่ถามว่าเอา IP ไปทำไรนั้น คือ ระบบไม่มีการล็อกอิน
 จะทำยังไงให้รู้ว่าใครใช้งาน ระบบนี้ จึงต้องเก็บเป็น IP ของเครื่องนั้น ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 08:52:58 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | แล้วตกลงมันเป็๋น proxy 
 หรือว่าทำ nat
 
 หรือแค่แบ่ง subnet ปกติครับ
 
 แล้ว ip วง 10.0.x.x เป็น fix หรือ obtain จาก dhcp ครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 09:04:01 | By :
                            ห้ามตอบเกินวันละ 2 กระทู้ |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | วง 10 เป็น FIX ครับ 
 แล้ว เป็น SUBNET ปกติครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 09:12:49 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | แสดงว่า 192.0.1.99 เป็น gateway ใช่ไหมครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 09:15:27 | By :
                            ห้ามตอบเกินวันละ 2 กระทู้ |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | 192.0.1.99 เป็น gateway  ใช่แล้วครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 09:31:54 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | งั้นลองใช้ class นี้ดูนะ 
 IPNetworking.cs
 
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
/// <summary>
/// Summary description for IPNetworking
/// </summary>
public class IPNetworking
{
    public static string GetIP4Address()
    {
        string IP4Address = String.Empty;
        foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }
        if (IP4Address != String.Empty)
        {
            return IP4Address;
        }
        foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }
        return IP4Address;
    }
}
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 10:21:10 | By :
                            ห้ามตอบเกินวันละ 2 กระทู้ |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | string IP4Address = String.Empty; 
 foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
 {
 if (IPA.AddressFamily.ToString() == "InterNetwork")
 {
 IP4Address = IPA.ToString();
 break;
 }
 }
 
 if (IP4Address != String.Empty)
 {
 return IP4Address;
 }
 
 foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
 {
 if (IPA.AddressFamily.ToString() == "InterNetwork")
 {
 IP4Address = IPA.ToString();
 break;
 }
 }
 
 lblIPaddress.Text = IP4Address;
 
 
 
 ออกมาเหมือนเดิมครับ 192.0.1.99
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 10:50:39 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | เอ่อ 
 if (IP4Address != String.Empty)
 {
 return IP4Address;
 }
 
 มันข้ามนี่ไปเปล่า ทำไมไม่ใช้เป็น class
 
 หรือเขียนใส่ใน method ก็ยังดี
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 11:04:12 | By :
                            ห้ามตอบเกินวันละ 2 กระทู้ |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอโทษที่ครับ 
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!IsPostBack)
 {
 lblIPaddress.Text  = IpAddress();
 }
 }
 
 public string IpAddress()
 {
 string IP4Address = String.Empty;
 
 foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
 {
 if (IPA.AddressFamily.ToString() == "InterNetwork")
 {
 IP4Address = IPA.ToString();
 break;
 }
 }
 
 if (IP4Address != String.Empty)
 {
 return IP4Address;
 }
 
 foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
 {
 if (IPA.AddressFamily.ToString() == "InterNetwork")
 {
 IP4Address = IPA.ToString();
 break;
 }
 }
 
 return IP4Address;
 
 }
 
 
 แบบนี้เหมือนเดิมครับ
 
 |  
              | 
 ประวัติการแก้ไข
 2013-05-15 11:18:59
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 11:18:38 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | งั้นคงติดที่ router แล้วแหละ 
 จาก server คงติดต่อ client ได้ แต่ไม่สามารถเห็น destination ได้
 
 สงสัยงานนนี้ต้องปรึกษา se แล้วครับ
 
 ให้แก้ secure ตรงนี้
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 11:42:19 | By :
                            ห้ามตอบเกินวันละ 2 กระทู้ |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณครับ  ผมจะลองติดต่อ SE ดู 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 15:16:22 | By :
                            ghosting789 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Save SourceCode ด้านล่างนี้ตั้งชื่อไฟล์ xxx.hta แล้วลองรันดูครับ ขอให้โชคดีครับ
 
 
 
 Code (JavaScript)
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
     <title>Application Executer</title>
     <HTA:APPLICATION ID="oMyApp" 
      APPLICATIONNAME="Application Executer" 
      BORDER="no"
      CAPTION="no"
      SHOWINTASKBAR="yes"
      SINGLEINSTANCE="yes"
      SYSMENU="yes"
      SCROLL="no"
      WINDOWSTATE="normal">
     <script type="text/javascript" language="javascript">
         function RunExcuteFile() {
   WshShell = new ActiveXObject("WScript.Shell");
   WshShell.Run("c:/windows/system32/notepad.exe abc.txt", 1, false);
         }
     </script>
 </head>
 <body>
  <input type="button" value="Run Notepad" onclick="RunExcuteFile();"/>
 </body>
 </html>
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 17:26:25 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Save SourceCode ด้านล่างนี้ตั้งชื่อไฟล์ xxx.hta แล้วลองรันดูครับ 
 Code (JavaScript)
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
     <title>Application Executer</title>
     <HTA:APPLICATION ID="oMyApp" 
      APPLICATIONNAME="Application Executer" 
      BORDER="no"
      CAPTION="no"
      SHOWINTASKBAR="yes"
      SINGLEINSTANCE="yes"
      SYSMENU="yes"
      SCROLL="no"
      WINDOWSTATE="normal">
     <script type="text/javascript" language="javascript">
         function RunExcuteFile() {
         WshShell = new ActiveXObject("WScript.Shell");
         WshShell.Run("c:/windows/system32/notepad.exe abc.txt", 1, false);
         }
     </script>
     <script type="text/vbscript">
     	Function GetIPInformation()
		strComputer = "."
		Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
		Set colNetAdapters = objWMIService.ExecQuery _
			("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=True")
		strDetails = ""
		For Each objAdapter In colNetAdapters
			If IsNull(objAdapter.IPAddress) = False And IsNull(objAdapter.DefaultIPGateway) = False Then
				If Join(objAdapter.IPAddress) <> "0.0.0.0" Then
					If strDetails = "" Then
						strDetails = Join(objAdapter.IPAddress) & ";"
						strDetails = strDetails & Join(objAdapter.IPSubnet) & ";"
						strDetails = strDetails & Join(objAdapter.DefaultIPGateway) & ";"
						strDetails = strDetails & Join(objAdapter.DNSServerSearchOrder, ";")
					End If
				End If
			End If
		Next
		MsgBox strDetails
		GetIPInformation = strDetails
	End Function
     </script>
 </head>
 <body>
  <input type="button" value="Around the world Get IP Address" onclick="GetIPInformation();"/>
 </body>
 </html>
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-15 17:56:15 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณครับ ผมจะลองติดต่อ SE ดู 
 ถ้าอ้างตามทฤษฏี (คุยกันยาว และไม่รู้จบ)
 ----- IP4 แบ่งออกเป็น 3 Class ดังนี้
 -------- Clase A
 -------- Clase B
 -------- Clase C
 ------------ โดยปกติแล้ว (Default) มักจะเข้าใจกันโดยทั่วฯไป เราจะใช้รูปแบบนี้เสมอ 192.168.X.X
 ------------  ยกเว้นกำหนดเป็นอย่างอื่น และแน่นอนว่า มันมีปัญหาซ่อนเร้นอยู่  *****
 ------------  เป็นโชคร้ายของคนที่เจอปัญหานั้น (รอเวลาเมื่อไหร่ก็เท่านั้นเอง)   **********
 
 
 [x] ***** หมายความว่า จะมีใครสักกี่คนที่เข้าใจ แก่นแท้ของ OS อย่างลึกซึ้ง (Windows, Linux, OpenSourceOS, etc)
 [x] ********** หมายความว่า ในทางปฏิบัติแล้ว ยกตัวอย่างเปรียบเทียบ เช่น การเข้าสายแลน (UTP)
 ---------------- มีทั้งหมด 4 คู่ (แปดเส้น) และโดยปกติจะต่อแบบนี้ (ไม่ไขว้สาย)
 ---------------- ขาวส้ม-ส้ม, ขาวเขียว-น้ำเงิน, ขาวน้ำเงิน-เขียว, ขาวน้ำตาล-น้ำตาล
 ---------------- ขาวส้ม-ส้ม, ขาวเขียว-น้ำเงิน, ขาวน้ำเงิน-เขียว, น้ำตาล-ขาวน้ำตาล
 --------------------- เมื่อ 15 ปีที่ผ่านมา ผมเจอปัญหา ข้อมูลมาไม่ครบ อาจจะเป็น 1 อาทิตย์ หรือ 2 อาทิตย์ หรือ เดือนละครั้ง เอาแน่เอานอนไม่ได้
 
 
 [x] เพราะฉะนั้น การกำหนด IP4 Address
 ---- 192.168.X.X <> 192.0.1.X
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-17 14:10:12 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | มันมีอยู่ประโยคหนึ่งที่อยากจะบอกว่า "เหนือฟ้ายังมีฟ้า"
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-17 22:18:39 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | อันนี้คือคำตอบที่ดีที่สุด ณ.เวลานี้ สำหรับหัวข้อกระทู้ที่ #095130 
 ----- ศึกษา SignalR  *****
 ----- ศึกษา Node.js
 ----- ศึกษา Knockout.js
 ----- ศึกษา WebSockets
 
 [x] *****  หมายความว่าุ้ในระหว่างทางที่คุณศึกษานั้น คุณจะได้อะไรมากมาย ผ่านคำว่า Real time.
 ---------- และคุณจะรับรู้ว่า Dynamic Website มันกำลังจะกลายเป็นอดีตไปแล้ว (ยุคน้ำแข็ง)
 ---------- วิธีการ Install SignalR
 ---------- PM> Install-Package Microsoft.AspNet.SignalR
 
 ขอให้โชคดีครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-18 07:12:29 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | #NO24 ผมลืมบอกไปอีกอย่างหนึ่ง ---------- ศึกษา Owin (SignalR มันเรียกใช้งาน Owin)
 
 ตัวอย่าง SignalR & Owin
 
 Code (VB.NET)
 
 Protected Function GetIpAddress() As String
	Dim env = [Get](Of IDictionary(Of String, Object))(Context.Request.Items, "owin.environment")
	If env Is Nothing Then
		Return Nothing
	End If
	Dim ipAddress = [Get](Of String)(env, "server.RemoteIpAddress")
	Return ipAddress
End Function
Private Shared Function [Get](Of T)(env As IDictionary(Of String, Object), key As String) As T
	Dim value As Object
	Return If(env.TryGetValue(key, value), DirectCast(value, T), Nothing)
End Function
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2013-05-18 07:23:04 | By :
                            ผ่านมา |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |