Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import javax.microedition.midlet.*;
  2. import javax.microedition.lcdui.*;
  3. import javax.microedition.io.*;
  4.  
  5. public class DatagramClient extends MIDlet implements CommandListener
  6. {
  7. //Declare the Objects
  8. public Form form1;
  9. public Command sendCommand;
  10. public Display display;
  11. public TextField textfield;
  12.  
  13.  
  14. public DatagramClient()
  15. {
  16. //Create Display Command
  17. display=Display.getDisplay(this);
  18. form1=new Form("DatagramClient");
  19. sendCommand=new Command("send",Command.OK,1);
  20. textfield=new TextField("Enter Text",null,30,TextField.ANY);
  21. form1.append(textfield);
  22. form1.addCommand(sendCommand);
  23. form1.setCommandListener(this);
  24. }
  25. public void startApp()
  26. {
  27. display.setCurrent(form1);
  28. }
  29. public void pauseApp()
  30. {
  31. }
  32. public void destroyApp(boolean unconditional)
  33. {
  34. }
  35. public void commandAction(Command cmd,Displayable d)
  36. {
  37. if(cmd==sendCommand)
  38. {
  39. try
  40. {
  41. DatagramConnection dgc = (DatagramConnection) Connector.open("datagram://localhost:9001");
  42. try {
  43. while(true)
  44. {
  45. byte[] payload = textfield.getString().getBytes();
  46. Datagram datagram = dgc.newDatagram(payload, payload.length);
  47. dgc.send(datagram);
  48. }
  49.  
  50. }
  51. finally {
  52. dgc.close();
  53. }
  54. }
  55. catch (Exception x)
  56. {
  57. x.printStackTrace();
  58. }
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement