Search
Latest topics
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
Page 1 of 1
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
Đố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#
2. Delphi
3. VB.NET
4. C
5. Java
6. ASP.NET
8. 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");
}
- Code:
$ip=$_SERVER['REMOTE_ADDR'];
8. ASP
- Code:
<% Response.Write Request.ServerVariables("REMOTE_ADDR") %>
Similar topics
» Lập trình hướng đối tượng trong Java
» Hướng dẫn sử dụng công cụ eclipse lập trình java
» Hướng đối tượng trong Java
» Mảng trong ngôn ngữ lập trình java
» Giáo trình Java - Core Java
» Hướng dẫn sử dụng công cụ eclipse lập trình java
» Hướng đối tượng trong Java
» Mảng trong ngôn ngữ lập trình java
» Giáo trình Java - Core Java
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Thu Aug 23, 2012 5:38 am by Admin
» Tuyệt kỹ cua giai
Thu Aug 23, 2012 5:36 am by Admin
» NETCAT.........
Mon Aug 13, 2012 6:35 am by Admin
» Bảo mật CSDL bằng phương pháp mã hóa.
Tue Apr 17, 2012 10:04 pm by Admin
» Hàm mã hóa MD5 bằng JavaScript
Tue Apr 17, 2012 10:03 pm by Admin
» Giá của món quà
Fri Apr 13, 2012 6:01 am by Admin
» Sẽ chỉ yêu ai?
Fri Apr 13, 2012 6:01 am by Admin
» Cách đọc bảng chữ cái!
Thu Apr 12, 2012 10:37 pm by Admin
» Gắn trojan, keylog, virus vào website, forum
Tue Apr 10, 2012 1:14 am by Admin