DatagramSender : Networks « J2ME « Java






DatagramSender

DatagramSender

/*
J2ME in a Nutshell
By Kim Topley
ISBN: 0-596-00253-X

*/


import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;

public class DatagramSender {
    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Usage: DatagramSender port length");
            System.exit(1);
        }
        
        try {
            DatagramConnection sender = 
                    (DatagramConnection)Connector.open("datagram://localhost:" + args[0]);
            int length = Integer.parseInt(args[1]);
            byte[] buffer = new byte[length];
            for (int i = 0; i < length; i++) {
                buffer[i] = (byte)('0' + (i % 10));
            }
            Datagram dgram = sender.newDatagram(buffer, buffer.length);
            sender.send(dgram);
            
            // Wait for the packet to be returned
            for (int i = 0; i < length; i++) {
                buffer[i] = (byte)0;
            }
            sender.receive(dgram);
            length = dgram.getLength();
            System.out.println("Received return packet, length is " + length);
            
            // Show the content of the datagram.
            for (int i = 0; i < length; i++) {
                System.out.print(buffer[i] + " ");
            }
            System.out.println();
        } catch (IOException ex) {
            System.out.println("IOException: " + ex);
        }
    }
}

           
       








Related examples in the same category

1.MIDlet to invoke a CGI script.
2.MIDlet to invoke a CGI script (POST method is used)
3.Https MIDlet
4.Pass a cookie (stored in rms) between the MIDlet and a Java servlet.
5.Use GET or POST to communicate with a Java servlet.
6.Use Java servlets sessions to tally golf scores.
7.Http Test
8.An example MIDlet to invoke a CGI script.An example MIDlet to invoke a CGI script.
9.Timer ServerTimer Server
10.Http ExampleHttp Example
11.Socket connectionSocket connection
12.Http ConnectionHttp Connection
13.Cookie MIDletCookie MIDlet
14.JargoneerJargoneer
15.Post MIDlet
16.Patchy MIDletPatchy MIDlet
17.MIDlet to invoke a CGI script (GET method).MIDlet to invoke a CGI script (GET method).
18.Fetch Page MidletFetch Page Midlet
19.Invoke Servlet Midlet 2
20.Invoke Servlet Midlet 1
21.MIDlet to invoke a CGI script (POST method is used) (2)MIDlet to invoke a CGI script (POST method is used) (2)
22.Demonstrates the functionality of DatagramConnection framework.Demonstrates the functionality of DatagramConnection framework.
23.Sample to demonstrate Http GET and POST from MIDlet
24.Get file from networkGet file from network
25.Midlet Servlet 2Midlet Servlet 2
26.MIDlet to fetch a page using an HttpConnectionMIDlet to fetch a page using an HttpConnection
27.A simple network clientA simple network client
28.Send client request and Get server responseSend client request and Get server response
29.Socket MIDletSocket MIDlet
30.www.amazon.com Book Ranking MIDletwww.amazon.com Book Ranking MIDlet
31.Time Server
32.Http MIDletHttp MIDlet
33.Datagram Receiver