Java Data Report Generation Code

Source: Internet
Author: User

During execution, the server starts with java test 1 and the client starts with java test. When the server type A character, the client prints the corresponding character, I hope the following code will be useful to you.

  1. Import java.net .*;
  2. Public class test {
  3. Public static int serverPort = 666;
  4. Public static int clientPort = 999;
  5. Public static int buffer_size = 1024;
  6. Public static DatagramSocket ds;
  7. Public static byte buffer [] = new byte [buffer_size];
  8. Public static void TheServer () throws Exception {
  9. Int pos = 0;
  10. While (true ){
  11. Int c = System. in. read ();
  12. Switch (c ){
  13. Case:
  14. Ds. send (new DatagramPacket (buffer, pos, InetAddress. getLocalHost (), clientPort ));
  15. Pos = 0;
  16. Break;
  17. Default:
  18. Buffer [pos] = (byte) c;
  19. }
  20. }
  21. }
  22. Public static void TheClient () throws Exception {
  23. While (true ){
  24. DatagramPacket p = new DatagramPacket (buffer, buffer. length );
  25. Ds. receive (p );
  26. System. out. println (new String (p. getData (), 0, p. getLength ()));
  27. }
  28. }
  29. Public static void main (String [] args) throws Exception {
  30. // TODO Auto-generated method stub
  31. If (args. length = 1 ){
  32. Ds = new DatagramSocket (serverPort );
  33. TheServer ();
  34. } Else {
  35. Ds = new DatagramSocket (clientPort );
  36. TheClient ();
  37. }
  38. }
  39. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.