I'm not able to figure out this error, i have a code which sends time to a micro-controller through ethernet, It works fine for sometime later gives a error java.io.ioexcep tion:unable to open a device:255 devices are already opened can somebody tell me what's the reason to this error and how do we solve it.
i use netbeans 7.2 IDE with framework core java and jpcap for networking.
Below is my code
i use netbeans 7.2 IDE with framework core java and jpcap for networking.
Below is my code
Code:
public void sendTime(Packet pack) {
try {
InetAddress src = null;
InetAddress dest = null;
if (pack instanceof IPPacket) {
IPPacket ipp = (IPPacket) pack;
src = ipp.dst_ip;
dest = ipp.src_ip;
}
System.out.println("src ip: " + src);
System.out.println("dest ip: " + dest);
//obtain MAC address of the default gateway
EthernetPacket etherSrc = ((EthernetPacket) pack.datalink);
byte[] gwmac = null;
gwmac = etherSrc.src_mac;
System.out.println("dest mac : " + gwmac);
//Send packet
JpcapSender sender = JpcapSender.openDevice(devices[index]);
UDPPacket sendPacket = new UDPPacket(1, 9);//UDPPacket(src_port,dest_port);
/*setIPv4Parameter(int priority, boolean delay_flag, boolean through_flag, boolean realibility_flag, int rsv_typeofservice, boolean rsv_fragmentation reservation flag, boolean dont_fragment flag, boolean more_fragment flag, int offset, int ident, int time to live, int protocol, java.net.InetAddress src, java.net.InetAddress dst)
Sets the IPv4 parameters */
sendPacket.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 1010101, 100, IPPacket.IPPROTO_UDP,
src, dest);
// count++;
// System.out.println("count = "+count);
String outData = SendTime();
sendPacket.data = outData.getBytes();
System.out.println("string sent =" + sendPacket.data);
EthernetPacket ether = new EthernetPacket(); //get ethernet packet
ether.frametype = EthernetPacket.ETHERTYPE_IP; //set frame type as IP
//setting src nd dest MAC address
ether.src_mac = devices[index].mac_address;
System.out.println("source MAC address =" + ether.src_mac);
//ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9,(byte)10};
ether.dst_mac = gwmac;
System.out.println("destination MAC address =" + gwmac);
//set datalink frame of packet as ether
sendPacket.datalink = ether;
sender.sendPacket(sendPacket);
} catch (UnknownHostException ex) {
Logger.getLogger(func.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
Code:
public static String SendTime() {
int week;
Connection con = null;
StringBuilder outData = new StringBuilder();
try {
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter =
new SimpleDateFormat("00yy:MM:dd HH:mm:ss");
week = currentDate.get(currentDate.WEEK_OF_MONTH);
String dateNow = formatter.format(currentDate.getTime());
String date = dateNow.toString();
//String dateNow = formatter.format((currentDate.getTime()).toString());
outData.append("%");
outData.append(date).append(":").append(week).append("$");
outData.append("#");
System.out.println("time sent" + outData);
System.out.println("time sent");
} catch (Exception e) {
}
return outData.toString();
}