Java定义了操作服务器主机的ip和HostName的类 java.net.InetAddress,如下就是一个实例:
package com.dylan.java.net;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//此类中没有定义构造器,二是通过静态方法返回此类的对象实例
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia.getHostAddress());
System.out.println(ia.getHostName());
System.out.println(ia.getAddress());
//getCanonicalHostName()这个方法与getHostName()有什么不同呢?安全问题?
System.out.println(ia.getCanonicalHostName());
//InetAddress ia2 = new InetAddress(); 这个类没有构造方法吗?
System.out.println(InetAddress.getByName("www.baidu.com"));
/*
* 注意加上的(byte)是因为byte的方位是-128—128,没有加这个也是不会报错的
*/
byte[] bs = new byte[]{(byte)127,123,0,1};
InetAddress ia2 = InetAddress.getByAddress("dylan",bs);
System.out.println("第二种方法:"+ ia2.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(), InetAddress.getByName("Localhost"),6000); ds.send(dp); ds.close(); } catch(Exception e){} } public static void main(String[] ...
java.net.InetAddress 类包装器。 用法 获取主机的 IP 地址,给定主机名: => (by-name "github.com") # 获取本地主机地址: => (localhost) # 检查地址地址是否可达: => (reachable? (by-name "server-in-...
import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.sql.Time; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing....
import java.net.InetAddress; import java.net.UnknownHostException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java....
33. import java.net.InetAddress; 34. import java.net.SocketException; 35. import java.net.UnknownHostException; 36. import java.util.Date; 37. import java.util.Scanner; 38. import java.util.Stack...
Android中socket通信简单实现 socket通信需要有一个服务器和客户端,可以把同一个APP作为服务器跟客户端,也可以分开成两个APP。...import java.net.InetAddress; import java.net.ServerSocket; import java.net.So
import java.net.InetAddress;import java.net.Socket;public class SimpleHttpClientUsingSocket {public static void main(String[] args) {String host = null;if (args.length > 0) {host = args[0];} else {...
获取计算机的mac地址和ip地址,mac地址返回的是一个list,ip地址是string
new TCPClient(bufOut.toByteArray(), InetAddress.getLocalHost(), 1211); break; } else { bufOut.write(buf, 0, len); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch ...
是一个可以用java的基本类java.net.InetAddress做的程序实例
import java.net.InetAddress; import java.util.Arrays; import jpcap.*; import jpcap.packet.*; public class ARP { public static byte[] arp(InetAddress ip) throws java.io.IOException{ //发现本机器的...
import java.net.InetAddress; import java.net.UnknownHostException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import net.sf.json.JSONArray; import org...
网络编程(TCP) TCP需要连接(三次握手)。发送端也称客户端,接收端...import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; /* * Socket(InetAddress address, int port)
InetAddress inetAddress = InetAddress.getLocalHost(); String ip = inetAddress.getHostAddress(); System.out.println("~~~服务端已就绪,等待客户端接入~,服务端 ip 地址: " + ip); // 2. 调用 accept ...
主要介绍了Java中的IP地址和InetAddress类使用详解,是Java入门学习中的基础知识,需要的朋友可以参考下
Java网络编程基础-InetAddress类与URL类
import java.net.*; import java.util.*; public class Server { public static void main(String args[]){ try { ServerSocket server = new ServerSocket(8888); System.out.println("服务器在监听8888端口...");...
在java中使用套接口相当简单,Java API为处理套接口的通信提供了一个类java.net.Socket.,使得编写网络应用程序相对容易.服务器采用多线程以满足多用户的请求,通过JDBC与后台数据库连接,并通过创建一个...
import java.net.InetAddress; /* 流程: 1. 创建UDP服务器对应的发送端Socket 2. 准备对应数据包,需要带有指定数据 3. 发送数据 send 4. 关闭UDP发送端 */ public class SenderDemo1 { public static void ...