java网卡地址(java获取网关ip)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
本篇文章给大家谈谈java网卡地址,以及java获取网关ip对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
java怎样取得网卡物理地址
给你一个局域网的聊天程序或许对你有用!!!
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.GregorianCalendar;
import javax.swing.JDialog;
public class QQ extends Frame implements ActionListener {
Label label1 = new Label("请输入您要发纤橘送的信息(限英文):");
Label label2 = new Label("以下是你收到的消息记录:"歼竖誉);
Label label3 = new Label("氏段把以上消息发给如下IP地址:");
TextArea input = new TextArea("", 7, 14, TextArea.SCROLLBARS_BOTH);
TextArea output = new TextArea("", 8, 14, TextArea.SCROLLBARS_BOTH);
TextField IPAdd = new TextField("192.168.1.88");
Button send = new Button("发送消息");
Button about = new Button("关于");
Button clear = new Button("清空消息纪录");
GregorianCalendar time = new GregorianCalendar();
QQ() {
super("仿QQ聊天工具");
setLayout(null);
setLocation(250, 250);
this.setSize(518, 218);
this.setResizable(false); // 大小不可变
this.setBackground(new Color(220, 220, 220));
Toolkit kit = Toolkit.getDefaultToolkit();
Image myImage = kit.getImage("icons\\QQ.bmp");
this.setIconImage(myImage);
label1.setFont(new Font("宋体", Font.PLAIN, 12));
label1.setForeground(new Color(0, 0, 192));
label1.setBounds(8, 28, 216, 16);
input.setBackground(new Color(255, 255, 128));
input.setFont(new Font("Times New Roman", Font.BOLD, 15));
input.setForeground(Color.magenta);
input.setBounds(8, 44, 248, 120);
output.setBackground(new Color(128, 255, 255));
output.setFont(new Font("Times New Roman", Font.PLAIN, 12));
output.setForeground(Color.magenta);
output.setBounds(264, 44, 248, 136);
output.setEditable(false);
send.setFont(new Font("新宋体", Font.PLAIN, 12));
send.setLocation(136, 188);
send.setSize(120, 22);
clear.setFont(new Font("新宋体", Font.PLAIN, 12));
clear.setLocation(392, 188);
clear.setSize(120, 22);
label2.setFont(new Font("宋体", Font.PLAIN, 12));
label2.setForeground(new Color(0, 0, 192));
label2.setBounds(264, 28, 216, 16);
about.setFont(new Font("新宋体", Font.PLAIN, 12));
about.setLocation(264, 188);
about.setSize(120, 22);
label3.setFont(new Font("宋体", Font.PLAIN, 12));
label3.setForeground(new Color(0, 0, 192));
label3.setBounds(8, 172, 160, 16);
IPAdd.setFont(new Font("新宋体", Font.PLAIN, 12));
IPAdd.setLocation(8, 190);
IPAdd.setSize(120, 19);
add(label1);
add(input);
add(label3);
add(label2);
add(output);
add(IPAdd);
add(send);
add(about);
add(clear);
addWindowListener(new closeWin());
send.addActionListener(this);
about.addActionListener(this);
clear.addActionListener(this);
setVisible(true);
waitForData();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == send)
sendData();
else if (e.getSource() == clear)
output.setText("");
else if (e.getSource() == about) {
AboutQQ test = new AboutQQ(this);
}
}
public static void main(String args[]) {
new QQ();
}
void sendData() {
try {
String msg = input.getText();
if (msg.equals(""))
return ;
input.setText("");
String ad = IPAdd.getText();
InetAddress tea = InetAddress.getLocalHost();
String asd = tea.getHostAddress();//发送方的IP地址
output.append("[" + asd + "]:(" + time.get(GregorianCalendar.YEAR)
+ "-" + time.get(GregorianCalendar.MONTH) + "-"
+ time.get(GregorianCalendar.DATE) + " "
+ time.get(GregorianCalendar.HOUR) + ":"
+ time.get(GregorianCalendar.MINUTE) + ":"
+ time.get(GregorianCalendar.SECOND) + ") " + "\n" + msg
+ "\n");
msg = "From [" + asd + "]:(" + time.get(GregorianCalendar.YEAR)
+ "-" + time.get(GregorianCalendar.MONTH) + "-"
+ time.get(GregorianCalendar.DATE) + " "
+ time.get(GregorianCalendar.HOUR) + ":"
+ time.get(GregorianCalendar.MINUTE) + ":"
+ time.get(GregorianCalendar.SECOND) + ") \n" + msg;
InetAddress address = InetAddress.getByName(ad);
int len = msg.length();
byte[] message = new byte[len];
msg.getBytes(0, len, message, 0);
DatagramPacket packet = new DatagramPacket(message, len, address,
9999);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
} catch (Exception e) {
}
}
void waitForData() {
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
DatagramSocket socket = new DatagramSocket(9999);
while (true) {
socket.receive(packet);
String s = new String(buffer, 0, 0, packet.getLength());
output.append(s + "\n");
packet = new DatagramPacket(buffer, buffer.length);
}
} catch (Exception e) {
}
}
}
class closeWin extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Frame fr = (Frame) (e.getSource());
fr.dispose();
System.exit(0);
}
}
class AboutQQ {
private Label label;
private JDialog dialog;
public AboutQQ(Frame f){
label = new Label("Version 1.0");
dialog = new JDialog(f, "About", true);
dialog.setLocation(f.getLocation());
Container dialogPane = dialog.getContentPane();
dialogPane.setLayout(new BorderLayout());
dialogPane.add(label);
dialogPane.setBounds(50,50,50,50);
dialog.pack();
dialog.setVisible(true);
}
}
java如何获取mac地址?
以windows举例。
运行命令" cmd ipconfig /all"就会出现以下结果
Physical Address. . . . . . . . . : 20-CF-30-9A-60-EE
。
java就能掘兆过这样的命令来获取。以下是示例。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestMac
{
public static void main(String[] args) {
System.out.println("Operation System=" + getOsName());
System.out.println("Mac Address=" + getMACAddress());
System.out.println("通过ip获取mac"+getMACAddress("橘散旅192.168.1.101"));
}
public static String getOsName() {
String os = "";
os = System.getProperty("os.name");
return os;
}
public static String getMACAddress() {
String address = "";
String os = getOsName();
if (os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") 圆凳 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
}
} else if (os.startsWith("Linux")) {
String command = "/bin/sh -c ifconfig -a";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("HWaddr") 0) {
int index = line.indexOf("HWaddr") + "HWaddr".length();
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
}
address = address.trim();
return address;
}
public static String getMACAddress(String ipAddress) {
String str = "", strMAC = "", macAddress = "";
try {
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (int i = 1; i 100; i++) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") 1) {
strMAC = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
}
} catch (IOException ex) {
return "Can't Get MAC Address!";
}
//
if (strMAC.length() 17) {
return "Error!";
}
macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
+ ":" + strMAC.substring(12, 14) + ":"
+ strMAC.substring(15, 17);
//
return macAddress;
}
}
剑天梦的回答原理和我这个一样,都是通过Process 执行命令。 我直接补充到答案里了。不过
我这边运行那个命令出来的结果很多,那么花的时间就长了。优点是能够获取别人的mac地址 。
java 获取无线 mac地址吗
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class GetMac {
/**
* java获取客户端网卡的MAC地址
*
* @param args
*/
public static void main(String[] args) {
GetMac get = new GetMac();
System.out.println("1="+get.getMAC());
System.out.println("2="+get.getMAC("127.0.0.1"));
}
// 1.获取客户端ip地址( 这个必须从客户端传到后台):
// jsp页面下,很简单,request.getRemoteAddr() ;
// 因为系统的VIew层是用JSF来实现的,因此页面上没法直接获得类似request,在bean里做了个强制转换
// public String getMyIP() {
// try {
// FacesContext fc = FacesContext.getCurrentInstance();
// HttpServletRequest request = (HttpServletRequest) fc
// .getExternalContext().getRequest();
// return request.getRemoteAddr();
// } catch (Exception e) {
// e.printStackTrace();
// }
// return "";
// }
// 2.获取客户端mac地址
// 调用window的命令,在后台Bean里实现 通过ip来获取mac地址。方法如下:
// 运行速度【快】
public String getMAC() {
String mac = null;
try {
Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all");
InputStream is = pro.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String message = br.readLine();
int index = -1;
while (message != null) {
if ((index = message.indexOf("Physical Address")) 0) {
mac = message.substring(index + 36).trim();
break;
}
message = br.readLine();
}
System.out.println(mac);
br.close();
pro.destroy();
} catch (IOException e) {
System.out.println("Can't get mac address!");
return null;
}
return mac;
}
// 运行速度【慢渗烂】
public String getMAC(String ip) {
String str = null;
String macAddress = null;
try {
Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; true;) {
str = input.readLine();
if (str != null) {
if (str.indexOf("MAC Address") 州喊蚂 1) {
macAddress = str
.substring(str.indexOf("册埋MAC Address") + 14);
break;
}
}
}
} catch (IOException e) {
e.printStackTrace(System.out);
return null;
}
return macAddress;
}
}
java程序对于双网卡怎么获取两块网卡IP
用双网卡同时访问内外网暂时没有很完美的解决办法,陆枯因为存在路由冲突,毕竟有两个网关地址,现在可以试试下面的办法:
先来解决双网卡冲突的问题。可以通过改变路由地址表搞定。以你的单位用机为例,机器有两块网卡,接到两台路由器上:
内部网地址设置闷稿为192.168.1.110,子网掩码:255.255.255.0,网关:192.168.1.1
办公网地址:10.94.12.123,子网掩码:255.255.255.0,网关:10.94.12.254
如果按正常的设置方法设置每块网卡的IP地址和网关,再cmd下使用route print查看时会看到以0.0.0.0 0.0.0.0 开头的两个东西,即指向0.0.0.0的有两个网关,这样就会出现路由冲突,两个网络的访问存在困难。要实现同时访问两个网络就要用到route命令
第一步:route delete 0.0.0.0(删除所有0.0.0.0的路由)
第二步:route add 0.0.0.0 mask 0.0.0.0 192.168.1.1(添加0.0.0.0网络路由)
第三步:route add 10.0.0.0 mask 255.0.0.0 10.94.12.254(添加10.0.0.0网络路由)
这时就可以同时访问两个网络了,但碰到一个问题,使用上述命令添加的路由在系统重新启动后会蚂悉孝自动丢失,保存现有的路由表
作一个BAT文件吧,把上面3步的内容全加进去,并设置系统在开始的启动菜单里运行他。这样只要一开机,路由表就会按我们所需要的进行变更,双网的访问再也不会存在问题了。

关于java网卡地址和java获取网关ip的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
