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;
}

}

❷ 在Java中如何得到本地IP和伺服器的機器名

import java.net.*; public class catchserverip{ InetAddress ServerIPaddress=null; //取得Server的IP地址 public InetAddress getServerIP() { try { ServerIPaddress=InetAddress.getLocalHost();} catch (UnknownHostException e) {} return (ServerIPaddress);}}就可以了在jsp中寫 <jsp:useBean id=catchipbean scope=page class=catchip / String servername=catchipbean.getServerIP().getHostName(); ---------------------------------------------------------------

❸ java 如何把伺服器獲取的ip地址和主機名寫入TXT文件

/**
* @param host 主機名稱
* @param id id地址
* @param path txt文件的路徑
* @throws Exception
*/
public void save(String host,String id,String path) throws Exception{
BufferedWriter bw = new BufferedWriter(new FileWriter(path)); //建立輸出流
bw.append(host + "\r\n" + id); //輸出內容
bw.close(); //關閉流
}

❹ java伺服器端「/「文件路徑如何書寫

在java中路徑都這么寫:\\ , 這個就是路徑分隔符,以後就不用「/」了吧
不管是實在內windows還是在linux中,容你都用「\\」作為路徑分隔符就對了
File.separator()這個方法是沒什麼問題,但是,你如果傳入字元串後,
使用File.separator來split,可能會出錯的哦,你在所有的地方,都用「\\」,
肯定是不會錯的啦。

至於你說的json中也是,在java中就是用\\,這與json無關,而是轉義導致的

❺ JAVA中怎麼獲取指定域名」www.baidu.com」的伺服器地址

先調用PING命令,解析返回信息,獲取IP。

❻ java 文件在伺服器中定位絕對路徑

類名.class.getResource("");
java.lang.Class.getResource() 查找給定名字的資源
import java.net.URL;import java.lang.*;public class ClassDemo {

public static void main(String[] args) throws Exception {

ClassDemo c = new ClassDemo();
Class cls = c.getClass();

// finds resource relative to the class location
URL url = cls.getResource("file.txt");
System.out.println("Value = " + url);

// finds resource relative to the class location
url = cls.getResource("newfolder/a.txt");
System.out.println("Value = " + url);
}}
結果:
Value = file:/C:/Program%20Files/Java/jdk1.6.0_06/bin/file.txt
Value = null

❼ java如何知道PPP0的IP地址和伺服器地址

開始--運行--cmd--ipconfig/all試試 只是建議

如果想知道路由器地址,可以試著在cmd窗口下敲入tracert www..com出現的第一個地址便是你連接的路由器地址

或者第一個方法中找default gateway 該地址便是路由器的地址了

❽ 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

❾ java程序通過IP地址選擇伺服器

你這個問的 有點蒙! 問的范圍很大!
如果你有很多伺服器:根據就近原則! 最好的方式 !