Über UDP mit Gerät kommunizieren.

soeni1987

Mitglied
Hallo, ich möchte an ein Gerät mit der IP Adresse 255.255.255.255 und dem Port 3000 die zwei Zeichen BT zusenden und die Antwort zurückgeben. leider bleibt mein Programm hängen, ich habe von UDP aucn hicht so viel Ahnung aber hier erstmal mein Code:

Code:
	/**
	 * This method searches for Printers and returns the ip adress of the first
	 * @return the ip adress
	 */
	public static String getIPAdress(){
		// TODO getIpAdress (has to be implemented)
		String ip=null;
		int port = 3000;
		
		   try {
			     DatagramSocket sock = new DatagramSocket();
			     InetAddress server = InetAddress.getByName("255.255.255.255"); 
			     String line="BT";
			     
			       byte[] req = line.getBytes();
			       sock.send(new DatagramPacket(req, req.length, server, port));
			 
			       byte[] ans = new byte[1024];
			       DatagramPacket a = new DatagramPacket(ans, ans.length);
			       sock.receive(a);
			       ip = new String(ans, 0, a.getLength());
			 
			       if (a.getPort() != port)
			         System.err.println((port = a.getPort())+": new port");
			     
			   } catch (UnknownHostException e) {
			     System.err.println("No printer found"); System.exit(1);
			   } catch (IOException e) {
			     e.printStackTrace(); System.exit(1);
			   }

		return ip;
	}

was mach ich falsch?
 

Neue Beiträge

Zurück