獲取伺服器ip地址java
A. java裡面如何獲取伺服器的ip地址,幫幫忙
獲取本機ip地址
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString;
B. JAVA怎麼獲取伺服器IP
首先IP為一個字元串,例如:
class test{
static void Split(string ip,out string str1)
{
int i=ip.length;
while(i>0)
{
char ch=ip[i-1];
if(ch==':')
break;
i--;
}
str1=ip.Substring(0,i);
}
static void Main()
{
string str1;
Split("192.168.0.255:8080",out str1)
Console.WriteLine("{0}",str1);
}
}
str1中保存的就是你的ip,192.168.0.255
C. JAVA獲取IP地址
public static void main(String[] args) { try { // 獲取計算機名 String name = InetAddress.getLocalHost().getHostName(); // 獲取IP地址 String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("計算機名:"+name); System.out.println("IP地址:"+ip); } catch (UnknownHostException e) { System.out.println("異常:" + e); e.printStackTrace(); } }
是否可以解決您的問題?
D. JAVA怎麼獲取IP地址
java代碼獲取ip地址方法是
調用java.net包下面的的InetAddress類獲取。
E. java編程,獲取區域網內伺服器端的ip地址
socket.connect(new InetSocketAddress(ip, port), timeout)
看有沒有拋異常 沒異常就是已經連接上了
想獲取伺服器名稱 可以用ARP協議 或者測試連接的時候伺服器回應一個名稱
package;
importjava.io.IOException;
importjava.net.InetSocketAddress;
importjava.net.Socket;
publicclassClient{
publicstaticvoidmain(String[]args){
/**
*埠號
*/
intport=10000;
/**
*連接延時
*/
inttimeout=300;
System.out.println("ScannerStart...");
Socketsocket;
/**
*掃描
*/
for(inti=1,k=254;i<k;i++){
if((socket=isOnLine("192.168.1."+i,port,timeout))!=null){
System.out.println("Server:"
+socket.getInetAddress().getHostAddress()
+":"+socket.getPort()+"IsWaiting...");
}
/**
*關閉連接
*/
if(socket!=null&&!socket.isClosed()){
try{
socket.close();
}catch(IOExceptione){
socket=null;
}
}
}
System.out.println("Scannerend...");
}
/**
*測試連接伺服器,返回連接成功後的Socket
*
*@paramip
*伺服器Ip
*@paramport
*伺服器埠號
*@paramtimeout
*連接延時
*@return返回連接成功後的Socket
*/
privatestaticSocketisOnLine(Stringip,intport,inttimeout){
Socketsocket=newSocket();
try{
socket.connect(newInetSocketAddress(ip,port),timeout);
}catch(IOExceptione){
returnnull;
}
returnsocket;
}
}
F. 怎麼編寫一個Java程序來獲取ip地址
import java.util.*;
import java.net.*;public class getMyIP
{
public static void main(String[] args)
{
String IP = null;
String host = null;
try
{
InetAddress ia = InetAddress.getLocalHost();
host = ia.getHostName();//獲取計算機名字
IP= ia.getHostAddress();//獲取IP
}
catch(UnknownHostException e)
{
e.printStackTrace();
}
System.out.println(host);
System.out.println(IP);
}
}
G. java里,怎麼取得IP地址
package A;
import javax.servlet.http.HttpServletRequest;
public class a {
public String getRemortIP(HttpServletRequest request)
{
if (request.getHeader("x-forwarded-for") == null)
{
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");
}
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
接著有些了個jsp來調用:
<%@ page language="java" import="java.util.*,A.*" pageEncoding="utf-8"%>
<jsp:useBean id="test" class="A.a" scope="request" > </jsp:useBean>
<%
out.println("request.getRemoteAddr(): " + request.getRemoteAddr()); //這里輸出的不是真正的ip
test.getIpAddr(); //這里的參數怎麼寫?
%>
H. java怎麼獲取ip地址
java獲取ip地址
public static void main(String[] args) { try { // 獲取計算機名 String name = InetAddress.getLocalHost().getHostName(); // 獲取IP地址 String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("計算機名:"+name); System.out.println("IP地址:"+ip); } catch (UnknownHostException e) { System.out.println("異常:" + e); e.printStackTrace(); } }
I. java 調用介面獲取伺服器IP和埠號
<%=
request.getservername()%>
伺服器的域名
<%=
java.net.inetaddress.getlocalhost().gethostaddress()
%>
伺服器的ip地址
<%=request.getserverport()%>
伺服器正在運行的埠
J. java 調用介面獲取伺服器IP和埠號
package test5;
public interface A {
void setAll(int ID,String IP);
String getAll();
}
package test5;
public class B implements A{
String IP;
int ID;
public void setAll(int ID,String IP) {
this.ID=ID;
this.IP=IP;
}
public String getAll() {
return "ID為: "+ID+"\t\t"+"IP為: "+IP;
}
public static void main(String[] args) {
B b=new B();
b.setAll(1, "192.168.1.1");
System.out.println(b.getAll());
}
}