import java.net.*; import java.io.*; public class ksamp_udp { public static final String HOST = "192.168.0.200"; //通信先IPアドレス public static final int PORT = 20000; //通信先ポート番号 public static void main(String args[]) { DatagramSocket socket = null; InetSocketAddress remoteAddress = new InetSocketAddress( HOST, PORT); try { socket = new DatagramSocket(); //通信路確保 byte[] recv_buff = new byte[1024]; DatagramPacket recv_packet = new DatagramPacket(recv_buff, recv_buff.length); while(true){ //プログラム永久ループ //1)コマンド送信 String send_text = "1 mix"; byte[] send_buff = send_text.getBytes(); DatagramPacket send_packet = new DatagramPacket(send_buff, send_buff.length, remoteAddress); socket.send(send_packet); //2)データ受信 try { socket.setSoTimeout(500); socket.receive(recv_packet); String rect_text = new String(recv_buff, 0, recv_packet.getLength()); System.out.println( "受信データ=" + rect_text); } catch( SocketTimeoutException ex ) { System.out.println( "TIMEOUT"); //受信タイムアウト continue; } } } catch (IOException e) { e.printStackTrace(); } finally { socket.close(); } }//main() }