Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- import javax.microedition.io.*;
- public class DatagramClient extends MIDlet implements CommandListener
- {
- //Declare the Objects
- public Form form1;
- public Command sendCommand;
- public Display display;
- public TextField textfield;
- public DatagramClient()
- {
- //Create Display Command
- display=Display.getDisplay(this);
- form1=new Form("DatagramClient");
- sendCommand=new Command("send",Command.OK,1);
- textfield=new TextField("Enter Text",null,30,TextField.ANY);
- form1.append(textfield);
- form1.addCommand(sendCommand);
- form1.setCommandListener(this);
- }
- public void startApp()
- {
- display.setCurrent(form1);
- }
- public void pauseApp()
- {
- }
- public void destroyApp(boolean unconditional)
- {
- }
- public void commandAction(Command cmd,Displayable d)
- {
- if(cmd==sendCommand)
- {
- try
- {
- DatagramConnection dgc = (DatagramConnection) Connector.open("datagram://localhost:9001");
- try {
- while(true)
- {
- byte[] payload = textfield.getString().getBytes();
- Datagram datagram = dgc.newDatagram(payload, payload.length);
- dgc.send(datagram);
- }
- }
- finally {
- dgc.close();
- }
- }
- catch (Exception x)
- {
- x.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement