導航:首頁 > 配伺服器 > java伺服器地址

java伺服器地址

發布時間:2022-03-02 08:21:07

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

}

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

給你一個方法,自己調一下吧.

host就是主機名

ip就是ip,

filepath就是文件路徑

	publicvoidcreateFile(Stringhost,Stringip,Stringfilepath)
{
BufferedWriterbw=null;
try{
bw=newBufferedWriter(newFileWriter(newFile(filepath)));
bw.write("host-name:"+host+",ip:"+ip);
bw.flush();
bw.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}

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

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

4. java裡面如何獲取伺服器的ip地址,幫幫忙

獲取本機ip地址
InetAddress addr = InetAddress.getLocalHost();
ip=addr.getHostAddress().toString;

5. Java編寫socket程序時,怎麼知道伺服器在整個網路上的IP地址

伺服器的IP是固定的,要連接伺服器就得上網,上網就得去電信開個IP就是開通網路,電信會分配給你個IP。

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

樓主可以這樣寫【File.separator是java虛擬機根據當前的操作系統自動識別得到的文件路徑分隔符,例如windows是「」,linux是」/「】:

Filefile=newFile("files"+File.separator+"temp"+File.separator+"test.txt");

7. 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

8. java客戶端與服務端訪問其他地址

  1. JSP,其實是"服務端腳本",因為你知道的,JSP會最終被編譯成class.所以,JSP等於服務端的Servlet.因此,你卸載JSP上,其實等於是在伺服器上執行

  2. JS和html都是在客戶端執行的,是瀏覽器下載到本地之後再在瀏覽器中解釋執行,因此,你可以使用js的ajax方法來解決你這個問題.

懂沒有?

9. 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

10. Java Web伺服器

我現寫了一個,可以訪問到靜態資源。你看情況多給點分吧
另外eclipse還沒有5.5,5.5的貌似是myEclipse吧

其中port指埠,默認是地址是http://127.0.0.1:8088/
其中basePath指資源根路徑,默認是d:
可以通過命令行參數對其進行賦值

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;

public class SimpleHttpServer {
private static int port = 8088;
private static String basePath = "D:/";

public static void main(String[] args) {
if (args.length >= 1) {
basePath = args[0];
}
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
}
System.out.println("server starting:");
System.out.println("base path:" + basePath);
System.out.println("Listening at:" + port);
startServer();
System.out.println("server started succesfully");
}

private static void startServer() {
new Thread() {
public void run() {
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
final Socket s = ss.accept();
new Thread() {
public void run() {
try {
OutputStream socketOs = s.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
String headerLine = null;
while ((line = br.readLine()) != null) {
if (headerLine == null) {
headerLine = line;
}
if ("".equals(line)) {
break;
}
}
String target = headerLine.replaceAll("^.+ (.+) HTTP/.+$", "$1");
String queryString;
String[] tmp = target.split("\\?");
target = URLDecoder.decode(tmp[0], "utf-8");
target = target.endsWith("/") ? target.substring(0, target.length() - 1) : target;
if (tmp.length > 1) {
queryString = tmp[1];
} else {
queryString = "";
}

String filePath = basePath + target;
File f = new File(filePath);

if (!f.exists()) {
StringBuffer content = new StringBuffer();
content.append("<html><head><title>Resource Not Found</title></head><body><h1>The requested resource ")
.append(target).append(" is not found.</h1></body></html>");
StringBuffer toWrite = new StringBuffer();
toWrite.append("HTTP/1.1 404 Not Found\r\nContent-Length:").append("" + content.length()).append("\r\n\r\n")
.append(content);
socketOs.write(toWrite.toString().getBytes());
} else if (f.isDirectory()) {
StringBuffer content = new StringBuffer();
content.append("<html><head><title>").append(target).append(
"</title></head><body><table border='1' width='100%'>");
content.append("<tr><th align='left' width='40px'>").append("Path").append("</th><td>").append(target.length()>0?target:"/")
.append("</td></tr>");
content.append("<tr><th align='left'>Type</th><th align='left'>Name</th></tr>");
if (!basePath.equals(filePath)) {
content.append("<tr><td>Directory</td><td>").append("<a href='").append(
target.substring(0, target.lastIndexOf("/") + 1)).append("'>..</a>").append("</td></tr>");
}
File[] files = f.listFiles();
for (File file : files) {
content.append("<tr><td>");
if (file.isFile()) {
content.append("File</td><td>");
} else if (file.isDirectory()) {
content.append("Directory</td><td>");
}
content.append("<a href=\"").append(target);
if (!(target.endsWith("/") || target.endsWith("\\"))) {
content.append("/");
}
content.append(file.getName()).append("\">").append(file.getName()).append("</a></td></tr>");
}
content.append("</table></body></html>");

StringBuffer sb = new StringBuffer();
sb.append("HTTP/1.1 200 OK\r\nCache-Control: max-age-0\r\n");
sb.append("Content-Length:").append("" + content.length()).append("\r\n\r\n");
sb.append(content);
socketOs.write(sb.toString().getBytes());
} else if (f.isFile()) {
socketOs.write(("HTTP/1.1 200 OK\r\nCache-Control: max-age-0\r\nContent-Length:" + f.length() + "\r\n\r\n")
.getBytes());
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(f);
int cnt = 0;
while ((cnt = fis.read(buffer)) >= 0) {
socketOs.write(buffer, 0, cnt);
}
fis.close();
}
socketOs.close();
} catch (Exception e) {
try {
s.getOutputStream().write("HTTP/1.1 500 Error\r\n".getBytes());
ByteArrayOutputStream byteos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteos);
printStream
.write("<html><head><title>Error 500</title></head><body><h1>Error 500</h1><pre style='font-size:15'>"
.getBytes());
e.printStackTrace(printStream);
printStream.write("</pre><body></html>".getBytes());
byteos.close();
byte[] byteArray = byteos.toByteArray();
s.getOutputStream().write(("Content-Length:" + byteArray.length + "\r\n\r\n").getBytes());
s.getOutputStream().write(byteArray);
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}

}
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}

閱讀全文

與java伺服器地址相關的資料

熱點內容
什麼app買吃的東西是批發價的 瀏覽:419
漏斗存錢罐解壓玩具 瀏覽:783
誇克可以在線解壓文件嗎 瀏覽:518
大樂透投注費用演算法 瀏覽:209
程序員前3到5年後的建議 瀏覽:130
共享電動車用什麼app找 瀏覽:723
cpu具有編譯功能嗎 瀏覽:749
我的世界伺服器怎麼獲得拒絕方塊 瀏覽:923
手機加密密碼去哪裡能找到 瀏覽:169
什麼特效相機app好玩 瀏覽:952
凱叔命令詞 瀏覽:85
製作雲伺服器怎麼轉發數據 瀏覽:721
文件預覽java實現 瀏覽:984
青島少兒編程 瀏覽:399
蜘蛛5音箱安卓軟體怎麼用 瀏覽:587
前公司源碼可以用嗎 瀏覽:127
單片機初始化程序編程 瀏覽:196
app流失率指什麼 瀏覽:807
抗震等級梁的加密 瀏覽:919
液壓機解壓教學視頻 瀏覽:992