|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Aug 2008
Messaggi: 210
|
[Java] Identificazione computer
Esiste un qualche numero nel computer che lo identifichi univocamente? Se sì, esiste qualche metodo in Java che mi consenta di leggere questo numero? Mi servirebbe per identificare univocamente un pc
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
A me viene in mente il MAC Address della scheda di rete.
Prova a dare un occhio alla classe NetworkInterface nel package java.net In particolare al metodo getHardwareAddress() qui Occhio che la possibilità di leggere direttamente via Java un MAC Address è recente (JDK 1.6).
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Aug 2008
Messaggi: 210
|
Ho provato con questo ma viene lanciata una NullPointerException... cosa c'è che non va?
Codice:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class MacAddress {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
/*
* Get NetworkInterface for the current host and then read the
* hardware address.
*/
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
/*
* Extract each array of mac address and convert it to hexa with the
* following format 08-00-27-DC-4A-9E.
*/
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Il codice mi sembra pulito... scopri che cosa è "null" e probabilmente capirai anche perchè.
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Aug 2008
Messaggi: 210
|
Io credo che sia l'array di byte mac, è lì che viene lanciata l'eccezione. Sto cercando su google altre soluzioni ma niente sembra essere adatto a quello che voglio
|
|
|
|
|
|
#6 | |
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
Quote:
|
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Sì, in effetti.
Magari non è neanche la cosa più conveniente da fare nella circostanza in cui si trova UnknownSoldier: non gli ho neanche chiesto per quale scopo deve identificare univocamente un pc...
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
#8 |
|
Member
Iscritto dal: Aug 2008
Messaggi: 210
|
Siccome sto creando una chat e sto implementando un sistema per il ban, mi servirebbe un metodo efficace per identificare un pc e non permettergli l'accesso.
|
|
|
|
|
|
#9 | |
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
Quote:
|
|
|
|
|
|
|
#10 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Comunque, riguardo al fatto che il tuo byte[] sia null, questa è l'implementazione del metodo getHardwareAddress() della classe NetworkInterface in Java 1.6.
Leggi il commento del metodo. Quote:
Credo sia un problema di permessi. P.S.: getMacAddr0(...) è una chiamata a funzione nativa
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 02-09-2008 alle 09:06. |
|
|
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Jan 2005
Città: London, United Kingdom
Messaggi: 959
|
Forse ti può servire questo link: ricavare il mac address
__________________
zattix |
|
|
|
|
|
#12 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
ma il codice al link a ochio è esattamente quello postato da UnknownSoldier. Il problema è che la funzione getHardwareAddress() non torna l'array di byte con il MAC Address come atteso, ma una null reference. [vedi post sopra]. Ho provato anch'io con questo codice: Codice:
package macaddress;
import java.net.NetworkInterface;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
/**
*
* @author francesco
*/
public class MacAddress
{
private static InetAddress localhost;
private static byte[] hwdAddress;
public static void main(String[] args)
{
try
{
localhost = InetAddress.getLocalHost();
System.out.println("Local host found: " + localhost.getHostAddress());
NetworkInterface localInterface =
NetworkInterface.getByInetAddress(localhost);
hwdAddress = localInterface.getHardwareAddress();
}
catch(UnknownHostException unknownEx)
{
System.err.println("No IP address for the host could be found");
unknownEx.printStackTrace();
}
catch(SocketException socketEx)
{
System.err.println("I/O error occurs during recovery of InetAddress and/or " +
"MacAddress");
socketEx.printStackTrace();
}
catch(NullPointerException nullEx)
{
System.err.println("The specified address for local host is null.");
nullEx.printStackTrace();
}
if(hwdAddress != null)
{
System.out.println("Printing out MAC ADDRESS found on localhost:");
System.out.println("");
for (int i = 0; i < hwdAddress.length-2; i++)//QUI: hwdAddress è NULL
{
System.out.print(Byte.toString(hwdAddress[i]) + "-");
}
// printing out the last two
System.out.print(Byte.toString(hwdAddress[hwdAddress.length-1] + ":") ;
System.out.print(Byte.toString(hwdAddress[hwdAddress.length]));
}
else
{
System.err.println("Cannot print out MAC ADDRESS of localhost because:");
System.err.println("the address doesn't exist or is not accessible (security " +
"permission needed)");
}
}
}
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) Ultima modifica di banryu79 : 02-09-2008 alle 10:57. |
|
|
|
|
|
|
#13 |
|
Junior Member
Iscritto dal: Oct 2008
Messaggi: 1
|
suggerirei di considerare anche l'id della scheda madre o del processore
anche io vorrei fare qualcosa di analogo ma la mia idea era di identificare un computer attraverso mac e id della scheda madre ma, cercando con google , per ricavare l'id della scheda madre sembra che ci siano difficoltà ancora maggiori |
|
|
|
|
|
#14 |
|
Senior Member
Iscritto dal: Sep 2005
Città: Torino
Messaggi: 606
|
Ho provato anch'io i codici, e ho provato anche un programmino che avevo fatto che mi stampava i mac address di tutte le NetworkInterface, e vanno tutti, poi l'illuminazione, se mi disconnetto mi da null, se gli passo l'indirizzo di loopback idem.
Però se invece gli date l'indirizzo ip di rete che avete (quindi con l'interfaccia abilitata) dovrebbe funzionare. Provate e nel caso smentitemi.
__________________
"Se proprio dovete piratare un prodotto, preferiamo che sia il nostro piuttosto che quello di qualcun altro." [Jeff Raikes] "Pirating software? Choose Microsoft!" |
|
|
|
|
|
#15 | |
|
Senior Member
Iscritto dal: Oct 2007
Città: Padova
Messaggi: 4131
|
Quote:
Non capisco come mai al tempo mi dava quell'errore (ho fatto girare esattamente lo stesso codice che ho postato qui all'epoca) By the way, perchè per accedere in lettura al MAC address è necessaria la connessione di rete attiva? (Sospetto di aver fatto una domanda cretina, ma tant'è...)
__________________
As long as you are basically literate in programming, you should be able to express any logical relationship you understand. If you don’t understand a logical relationship, you can use the attempt to program it as a means to learn about it. (Chris Crawford) |
|
|
|
|
|
|
#16 | |
|
Senior Member
Iscritto dal: Sep 2005
Città: Torino
Messaggi: 606
|
Quote:
Codice:
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
{
NetworkInterface in = en.nextElement();
if (!in.isLoopback()) {
// Qui dentro ci va la stampa del MAC address
}
}
Il mistero si infittisce???oppure qualche nostro eroe giunge ad una conclusione? [EDIT]: penso che il problema sia dandogli l'indirizzo di loopback non sa più che pesci pigliare, sì una cosa del genere insomma....a voi la palla
__________________
"Se proprio dovete piratare un prodotto, preferiamo che sia il nostro piuttosto che quello di qualcun altro." [Jeff Raikes] "Pirating software? Choose Microsoft!" Ultima modifica di Oceans11 : 02-10-2008 alle 10:47. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 19:23.




















