「java网卡地址」java socket指定网卡
今天给各位分享java网卡地址的知识,其中也会对java socket指定网卡进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
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如何获取网卡地址
看你获取的是win系统还是linux系统了:
java执行操作系统的网卡地址语句:
获取机器名:
[java] view plain copy
public String getLocalHostName() {
String hostName;
try {
InetAddress addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
} catch (Exception ex) {
hostName = "";
}
return hostName;
}
获取IP(多个网卡时获取了多个IP):
[java] view plain copy
public ListString getNetworkAddress() {
ListString result = new ArrayListString();
EnumerationNetworkInterface netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
EnumerationInetAddress addresses=ni.getInetAddresses();
while(addresses.hasMoreElements()){
ip = addresses.nextElement();
if (!ip.isLoopbackAddress() ip.getHostAddress().indexOf(':') == -1) {
result.add(ip.getHostAddress());
}
}
}
return result;
} catch (Exception e) {
return null;
}
}
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地址
本方法主要用来限制系统在其他的机器上运行 其实原理简单的很 没有调用第三方插件 代码如下:
package users util;
import java io *;
import java util *;
import java util regex *;
public class NetID {
String IPCONFIG_MAND_WIN = ipconfig /all ;
boolean realMac = true;
String unique = ;
public static String getMacAddress() {
NetID hwid = new NetID();
return hwid getUnique() trim();
}
private String getUnique() {
String os = System getProperty( os name );
if (os startsWith( Windows )) {
return getUniqueWindows();
}else {
return ;
}
}
private String getUniqueWindows() {
String ipConfigResponse = null;
try {
ipConfigResponse = runConsoleCommand(IPCONFIG_MAND_WIN);
}
catch (IOException e) {
e printStackTrace();
}
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse \n );
while (tokenizer hasMoreTokens()) {
String line = tokenizer nextToken() trim();
int macAddressPosition = line indexOf( : );
if (macAddressPosition = ) {
continue;
}
String macAddressCandidate = line substring(macAddressPosition + )
trim();
if (isMacAddWin(macAddressCandidate)) {
if (realMac == true) {
generateUnique(macAddressCandidate);
}
else {
realMac = true;
}
}
}
return unique;
}
private String runConsoleCommand(String mand) throws IOException {
Process p = Runtime getRuntime() exec(mand);
InputStream stdoutStream = new BufferedInputStream(p getInputStream());
StringBuffer buffer = new StringBuffer();
while (true) {
int c = stdoutStream read();
if (c == ) {
break;
}
buffer append( (char) c);
}
String outputText = buffer toString();
stdoutStream close();
return outputText;
}
private boolean isMacAddWin(String macAddressCandidate) {
Pattern macPattern = pile( [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } [ a fA F]{ } );
Matcher m = macPattern matcher(macAddressCandidate);
return m matches();
}
private boolean isMacAddOSX(String macAddressCandidate) {
if (macAddressCandidate length() != ) {
return false;
}
else {
return true;
}
}
private void generateUnique(String macAddress) {
if (unique == ) {
unique += macAddress;
}
else {
unique += # ;
unique += macAddress;
}
}
public static void main(String [] args) {
System out println(NetID getMacAddress());
}
lishixinzhi/Article/program/Java/hx/201311/25985
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 socket指定网卡的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。