yeputons

Untitled

Apr 17th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. /**
  2.  * Created with IntelliJ IDEA.
  3.  * User: e.suvorov
  4.  * Date: 15.04.13
  5.  * Time: 15:29
  6.  * To change this template use File | Settings | File Templates.
  7.  */
  8. import java.io.ByteArrayInputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.net.*;
  12. import java.awt.*;
  13. import javax.imageio.ImageIO;
  14. import javax.swing.*;
  15.  
  16. public class Ch24Test {
  17.     public void run() throws Exception {
  18.         DatagramSocket s = new DatagramSocket(24130);
  19.         byte[] tosend = { '\n' };
  20.         s.send(new DatagramPacket(tosend, 0, tosend.length, InetAddress.getByName("ch24.org"), 24130));
  21.  
  22.         JFrame fr = new JFrame("received");
  23.         JLabel lbl = new JLabel();
  24.         fr.add(lbl);
  25.         fr.setVisible(true);
  26.         fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.  
  28.         for (;;) {
  29.             final int BUFSZ = 100 * 1024;
  30.             byte[] torecv = new byte[BUFSZ];
  31.             DatagramPacket recv = new DatagramPacket(torecv, torecv.length);
  32.             s.receive(recv);
  33.             System.out.println("RECEIVED: " + recv.getAddress());
  34.  
  35.             InputStream str = new ByteArrayInputStream(recv.getData());
  36.             Image img = ImageIO.read(str);
  37.             lbl.setIcon(new ImageIcon(img));
  38.             fr.pack();
  39.         }
  40.     }
  41.  
  42.     public static void main(String[] args) {
  43.         try {
  44.             new Ch24Test().run();
  45.         } catch (Exception e) {
  46.             e.printStackTrace();
  47.             System.exit(1);
  48.         }
  49.     }
  50. }
Add Comment
Please, Sign In to add comment