Advertisement
nRikee

pract6exer5

Nov 29th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. import java.net.*;
  2. import java.util.*;
  3.  
  4. public class pract6ejer5
  5. {
  6.     public static void main(String[] args) throws Exception {
  7.         System.out.println("Ejercicio 5: ");
  8.        
  9.             DatagramSocket d =  new DatagramSocket();
  10.             String nomAlumne = "nRikee\n";
  11.             InetAddress ip = InetAddress.getByName("localhost");
  12.             DatagramPacket dp = new DatagramPacket(nomAlumne.getBytes(),nomAlumne.getBytes().length,ip,7777);
  13.            
  14.             byte[] buffer = new byte[1000];
  15.             DatagramPacket p = new DatagramPacket(buffer, 1000);
  16.             DatagramSocket ds = new DatagramSocket();
  17.            
  18.             HiloRecibe fil = new HiloRecibe();
  19.            
  20.             fil.start();
  21.             ds.send(dp);
  22.            
  23.             String nom= new String(p.getData(),0,p.getLength());
  24.             System.out.println("\t" + nom);
  25.             d.close();
  26.     }
  27. }
  28.  
  29. class HiloRecibe extends Thread {
  30.     public HiloRecibe() { }
  31.     public void run() {
  32.         try{
  33.             while(true){
  34.                 DatagramSocket d =  new DatagramSocket(7777);
  35.                 byte[] buffer = new byte[1000];
  36.                 DatagramPacket p = new DatagramPacket(buffer, 1000);
  37.                
  38.                 d.receive(p);
  39.                
  40.                 String nom= new String(p.getData(),0,p.getLength());
  41.                 System.out.println("\t" + nom);
  42.                 d.close();
  43.             }
  44.         } catch(Exception e){}
  45.     }
  46.    
  47.     public static void main(String args[])
  48.     {
  49.         HiloRecibe h = new HiloRecibe();
  50.         h.start();
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement