By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

java.io.ioexception:unable to open a device:255 devices are already opened

100+
P: 126
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.ioexception: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

Expand|Select|Wrap|Line Numbers
  1.  public void sendTime(Packet pack) {
  2.  
  3.         try {
  4.             InetAddress src = null;
  5.             InetAddress dest = null;
  6.             if (pack instanceof IPPacket) {
  7.                 IPPacket ipp = (IPPacket) pack;
  8.                 src = ipp.dst_ip;
  9.                 dest = ipp.src_ip;
  10.             }
  11.             System.out.println("src ip: " + src);
  12.             System.out.println("dest ip: " + dest);
  13.             //obtain MAC address of the default gateway  
  14.  
  15.             EthernetPacket etherSrc = ((EthernetPacket) pack.datalink);
  16.             byte[] gwmac = null;
  17.             gwmac = etherSrc.src_mac;
  18.             System.out.println("dest mac : " + gwmac);
  19.             //Send packet
  20.             JpcapSender sender = JpcapSender.openDevice(devices[index]);
  21.  
  22.             UDPPacket sendPacket = new UDPPacket(1, 9);//UDPPacket(src_port,dest_port);              
  23.  
  24.             /*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) 
  25.              Sets the IPv4 parameters */
  26.  
  27.             sendPacket.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 1010101, 100, IPPacket.IPPROTO_UDP,
  28.                     src, dest);
  29.             // count++;
  30.             // System.out.println("count = "+count);
  31.             String outData = SendTime();
  32.             sendPacket.data = outData.getBytes();
  33.             System.out.println("string sent =" + sendPacket.data);
  34.  
  35.             EthernetPacket ether = new EthernetPacket(); //get ethernet packet
  36.             ether.frametype = EthernetPacket.ETHERTYPE_IP; //set frame type as IP
  37.             //setting src nd dest MAC address
  38.             ether.src_mac = devices[index].mac_address;
  39.             System.out.println("source MAC address =" + ether.src_mac);
  40.             //ether.dst_mac=new byte[]{(byte)0,(byte)6,(byte)7,(byte)8,(byte)9,(byte)10};
  41.             ether.dst_mac = gwmac;
  42.             System.out.println("destination MAC address =" + gwmac);
  43.             //set datalink frame of packet as ether
  44.             sendPacket.datalink = ether;
  45.             sender.sendPacket(sendPacket);
  46.         } catch (UnknownHostException ex) {
  47.             Logger.getLogger(func.class.getName()).log(Level.SEVERE, null, ex);
  48.         } catch (IOException ex) {
  49.             JOptionPane.showMessageDialog(null, ex);
  50.         }
  51.  
  52.  
  53.     }
Expand|Select|Wrap|Line Numbers
  1. public static String SendTime() {
  2.  
  3.         int week;
  4.         Connection con = null;
  5.         StringBuilder outData = new StringBuilder();
  6.         try {
  7.             Calendar currentDate = Calendar.getInstance();
  8.             SimpleDateFormat formatter =
  9.                     new SimpleDateFormat("00yy:MM:dd HH:mm:ss");
  10.             week = currentDate.get(currentDate.WEEK_OF_MONTH);
  11.             String dateNow = formatter.format(currentDate.getTime());
  12.             String date = dateNow.toString();
  13.             //String dateNow = formatter.format((currentDate.getTime()).toString());
  14.             outData.append("%");
  15.             outData.append(date).append(":").append(week).append("$");
  16.             outData.append("#");
  17.             System.out.println("time sent" + outData);
  18.             System.out.println("time sent");
  19.         } catch (Exception e) {
  20.         }
  21.         return outData.toString();
  22.  
  23.     }
Jan 25 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.