导航:首页 > 配服务器 > 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服务器地址相关的资料

热点内容
全球加密货币十大交易平台 浏览:963
汽车如何与服务器连接 浏览:85
如何创建服务器密码 浏览:918
creo扭曲命令正常使用 浏览:51
ase加密qt 浏览:666
看app登录和不登录有什么区别 浏览:464
da转换单片机 浏览:680
程序员老婆发型 浏览:816
py脚本反编译 浏览:463
为什么国内网络用国外的app卡 浏览:937
centos7更新命令 浏览:987
自动播放文件夹怎么关闭 浏览:770
slam算法实现 浏览:347
复制目标文件夹访问被拒需要权限 浏览:411
linuxbcast修改 浏览:185
手机如何用云服务器挂软件 浏览:280
时间窗app是什么东西 浏览:497
易语言端口映射源码 浏览:41
struts2权威指南pdf 浏览:905
用ct仿真单片机 浏览:798