1

メッセージを15文字の複数のメッセージに分割し、UDP接続を介して送信するにはどうすればよいか疑問に思いました。最初に送信するメッセージを作成するには、現在、パケット数(送信するメッセージ)を1つしか送信できません。

public class MainTalker extends Activity 
{
    private static DatagramSocket socket;
    private static DatagramPacket packet;
    private static Thread send;
    private Button cl1;
    private static EditText ed1;
    private static String msg = "Message";
    private static byte[] data = new byte[15];
    private static String TAG = "Sender UDP";
    static int count=0;
    static String[] output = new String[100] ;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_talker);

        ed1 = (EditText) findViewById(R.id.msg);
        cl1 = (Button) findViewById(R.id.send);

        try {
            socket = new DatagramSocket(11001);
            Toast toast = Toast.makeText(getApplicationContext(),"Port Opened",Toast.LENGTH_SHORT);
            toast.show();
            System.out.println("Port Opened ");
            socket.setBroadcast(true);
        } catch (Exception e) {
            System.out.println("Exception: Port not opened");
            e.printStackTrace();
            Log.e(TAG, e.getMessage());
        }

        cl1.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                System.out.println("Button Clicked ");

                send = new sendthr();
                send.start();
            }
        });
    }

    private static class sendthr extends Thread {
        public void run()
        {
                System.out.println("Thread Started");

                String messg = ed1.getText().toString();

                for(int i=0; i<messg.length(); i+=10)
                {
                    output[i] = messg.substring(i,i+10);
                    System.out.println(output[i]);
                    count++;

                if (messg == null)
                    output[i] = msg;
                data = output[i].getBytes();
                try
                { //Create packet with target host and target port
                    packet = new DatagramPacket(data,data.length,InetAddress.getByName("192.168.8.17"), 11001);
                    System.out.println("Packet Created");

                } catch (UnknownHostException e) {
                    System.out.println("Exception: Packet Created");

                    e.printStackTrace();
                    Log.e(TAG, e.getMessage());
                }

                try
                {
                    System.out.println("Sending the packet  "+ msg);
                    socket.send(packet);
                    System.out.println("Packet sent");
                } catch (IOException e) {
                    System.out.println("Exception: Packet Send");
                    e.printStackTrace();
                    Log.e(TAG, e.getMessage());
                }
                }
            }     
    }
}
4

0 に答える 0