Visualizzazione dei risultati da 1 a 7 su 7

Visualizzazione discussione

  1. #1

    Che cos'è un datagramma in java?

    Non riesco a capire cosa sia un datagramma in java e l'esempio che viene esposto oltre che incomprensibile non mi aiuta per niente :

    codice:
    // Demonstrate Datagrams. 
    import java.net.*; 
     
    class Esempio { 
      public static int serverPort = 998; 
      public static int clientPort = 999; 
      public static int buffer_size = 1024; 
      public static DatagramSocket ds; 
      public static byte buffer[] = new byte[buffer_size]; 
     
      public static void TheServer() throws Exception { 
        int pos=0; 
        while (true) { 
          int c = System.in.read(); 
          switch (c) { 
            case -1:  
              System.out.println("Server Quits."); 
              ds.close();
              return; 
            case '\r':  
              break; 
            case '\n': 
              ds.send(new DatagramPacket(buffer,pos, 
                 InetAddress.getLocalHost(),clientPort)); 
              pos=0; 
              break; 
            default: 
              buffer[pos++] = (byte) c; 
          } 
        } 
      } 
     
      public static void TheClient() throws Exception { 
        while(true) { 
          DatagramPacket p = new DatagramPacket(buffer, buffer.length); 
          ds.receive(p); 
          System.out.println(new String(p.getData(), 0, p.getLength())); 
        } 
      } 
     
      public static void main(String args[]) throws Exception { 
        if(args.length == 1) { 
          ds = new DatagramSocket(serverPort); 
          TheServer(); 
        } else { 
          ds = new DatagramSocket(clientPort); 
          TheClient(); 
        } 
      } 
    }
    Ultima modifica di giannino1995; 21-11-2013 a 00:04

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.