HACKIS - Hacking Internet Security
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
» Tuyệt Kỹ Đong Giai Chân Kinh (tuyệt Kỹ cua trai)
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyThu Aug 23, 2012 5:38 am by Admin

» Tuyệt kỹ cua giai
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyThu Aug 23, 2012 5:36 am by Admin

» NETCAT.........
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyMon Aug 13, 2012 6:35 am by Admin

» Bảo mật CSDL bằng phương pháp mã hóa.
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyTue Apr 17, 2012 10:04 pm by Admin

» Hàm mã hóa MD5 bằng JavaScript
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyTue Apr 17, 2012 10:03 pm by Admin

» Giá của món quà
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyFri Apr 13, 2012 6:01 am by Admin

» Sẽ chỉ yêu ai?
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyFri Apr 13, 2012 6:01 am by Admin

» Cách đọc bảng chữ cái!
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyThu Apr 12, 2012 10:37 pm by Admin

» Gắn trojan, keylog, virus vào website, forum
Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP EmptyTue Apr 10, 2012 1:14 am by Admin

Affiliates
free forum


Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP

Go down

Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP Empty Hướng dẫn - Lấy địa chỉ IP trong lập trình C#,VB.NET,ASP.NET,C,C++,Dephi,VB,JAVA,ASP

Post  Admin Sun Feb 27, 2011 7:47 am

Đối với các lập trình viên ít nhất một lần cũng phải có nhu cầu lấy địa chỉ IP (Internet Protocol) cho việc phát triển ứng dụng. Ví dụ để phục vụ cho việc ghi log truy cập, chống đăng nhập nhiều tài khoản cùng một IP, chặn truy cập thông qua IP,… Thông qua những đoạn mã tổng hợp dưới đây, tôi hy vọng nó sẽ hữu ích trong việc tra cứu của các bạn khi cần. Một số ngôn ngữ như là C#, Delphi, VB.NET, VB, C, Java, ASP.NET, PHP, ASP.

1.C#
Code:
using System;
using System.Net;
 
namespace GetIPCS
{
 ///
 /// Gets IP addresses of the local machine
 ///
 class classGetIPCS
 {
  ///
  /// Gets IP addresses of the local machine
  ///
  [STAThread]
  static void Main(string[] args)
  {
  // Get host name
  String strHostName = Dns.GetHostName();
  Console.WriteLine("Host Name: " + strHostName);
 
  // Find host by name
  IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
 
  // Enumerate IP addresses
  int nIP = 0;
  foreach(IPAddress ipaddress in iphostentry.AddressList)
  {
    Console.WriteLine("IP #" + ++nIP + ": " +
                      ipaddress.ToString());
  }
  }
 }
}

2. Delphi

Code:
uses  Winsock;
 
function getIPs: Tstrings;
type
  TaPInAddr = array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  Buffer: array[0..63] of Char;
  I: Integer;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := TstringList.Create;
  Result.Clear;
  GetHostName(Buffer, SizeOf(Buffer));
  phe := GetHostByName(buffer);
  if phe = nil then Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  I    := 0;
  while pPtr^[I] <> nil do
  begin
    Result.Add(inet_ntoa(pptr^[I]^));
    Inc(I);
  end;
  WSACleanup;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(GetIps.text);
end;

3. VB.NET

Code:
Private Sub IPAddress()
'To get local address
    Dim LocalHostName As String
    Dim i As Integer
    LocalHostName = Dns.GetHostName()
    Dim ipEnter As IPHostEntry = Dns.GetHostByName(LocalHostName)
    Dim IpAdd() As IPAddress = ipEnter.AddressList
    For i = 0 To IpAdd.GetUpperBound(0)
    Next
End Sub

4. C

Code:
// Borland C++ 5.0: bcc32.cpp getlocalip.cpp
// Visual C++ 5.0: cl getlocalip.cpp wsock32.lib
//
// This sample program is hereby placed in the public domain.
 
#include
#include
 
int doit(int, char **)
{
    char ac[80];
    if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
        cerr < < "Error " << WSAGetLastError() <<
                " when getting local host name." << endl;
        return 1;
    }
    cout << "Host name is " << ac << "." << endl;
 
    struct hostent *phe = gethostbyname(ac);
    if (phe == 0) {
        cerr << "Yow! Bad host lookup." << endl;        return 1;    }    for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
        struct in_addr addr;
        memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
        cout < < "Address " << i << ": " << inet_ntoa(addr) << endl;
    }
 
    return 0;
}
 
int main(int argc, char *argv[])
{
    WSAData wsaData;
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        return 255;
    }
 
    int retval = doit(argc, argv);
 
    WSACleanup();
 
    return retval;
}
}

5. Java

Code:
import java.net.*;
import java.io.*;
 
public class GetIPAddress
{
    public static void main(String [] args)
    {
        try
        {
            InetAddress thisIp =InetAddress.getLocalHost();
            System.out.println("IP:"+thisIp.getHostAddress());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

6. ASP.NET

Code:
string ip;
ip=Request.ServerVariables("HTTP_X_FORWARDED_FOR");
if(ip==string.Empty)
{
ip=Request.ServerVariables("REMOTE_ADDR");
}
7. PHP

Code:
$ip=$_SERVER['REMOTE_ADDR'];

8. ASP

Code:
<% Response.Write Request.ServerVariables("REMOTE_ADDR") %>
Admin
Admin
Admin

Tổng số bài gửi : 782
Join date : 2009-08-15

https://hackis.forumvi.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum