0

ネットワークプログラミング初心者です。UDP ブロードキャスト パケットの送信方法を学習するデモ プログラムを作成したいと考えています。ここに私が書いた小さなデモがあります:

public class DatagramClient
{
   private final static int PACKETSIZE = 100 ;

   public static void main( String args[] )
   {

      DatagramSocket socket = null ;

      try
      {
         // Convert the arguments first, to ensure that they are valid
         InetAddress host = InetAddress.getByName( "67.194.218.255" ) ;
         int port         = 34567;//Integer.parseInt( args[1] ) ;

         // Construct the socket
         socket = new DatagramSocket() ;

         // Construct the datagram packet
         byte [] data = "Hello Server".getBytes() ;
         DatagramPacket packet = new DatagramPacket( data, data.length, host, port ) ;

         // Send it
         socket.send( packet ) ;

         // Set a receive timeout, 2000 milliseconds
         socket.setSoTimeout( 2000 ) ;

         // Prepare the packet for receive
         packet.setData( new byte[PACKETSIZE] ) ;

         // Wait for a response from the server
         socket.receive( packet ) ;

         // Print the response
         System.out.println( new String(packet.getData()) ) ;

      }
      catch( Exception e )
      {
         System.out.println( e ) ;
      }
      finally
      {
         if( socket != null )
            socket.close() ;
      }
   }
}



public class DatagramServer
{

   private final static int PACKETSIZE = 100 ;

   public static void main( String args[] )
   {
      try
      {

         // Convert the argument to ensure that is it valid

         int port = 34567;

         // Construct the socket
         DatagramSocket socket = new DatagramSocket( port ) ;
         socket.setBroadcast(true);

         System.out.println( "The server is ready..." ) ;


         for( ;; )
         {
            // Create a packet
            DatagramPacket packet = new DatagramPacket( new byte[PACKETSIZE], PACKETSIZE ) ;

            // Receive a packet (blocking)
            socket.receive( packet ) ;

            // Print the packet
            System.out.println( packet.getAddress() + " " + packet.getPort() + ": " + new String(packet.getData()) ) ;

            // Return the packet to the sender
            socket.send( packet ) ;
        }  
     }
     catch( Exception e )
     {
        System.out.println( e ) ;
     }
  }
}

Wireshark によって検出されたパケットはありませんが、どこが間違っているのかわかりませんでした。前もって感謝します!

4

1 に答える 1