Advertisement
LukacikPavel

siete_udp

Feb 27th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package cviko1A;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.DatagramPacket;
  6. import java.net.DatagramSocket;
  7. import java.net.InetAddress;
  8. import java.net.SocketException;
  9. import java.net.UnknownHostException;
  10. import java.util.concurrent.ExecutorService;
  11. import java.util.concurrent.Executors;
  12.  
  13. public class FileInfoSender {
  14.  
  15.     public static final String FILE_PATH = "C:\\Users\\pc12\\Downloads\\SYS_PRGM.ova";
  16.     public static final int INFO_PORT = 5500;
  17.     public static final int REQUEST_PORT = 5501;
  18.     public static final int DATA_PORT = 5502;
  19.     public static final String TARGET_HOST = "158.197.35.101"; // 158.197.35.255
  20.     public static final long SLEEP_TO_NEXT_SEND = 1000; // 1sec
  21.  
  22.     public static void main(String[] args) {
  23.         File file = new File(FILE_PATH);
  24.         long fileLength = file.length();
  25.         String fileName = file.getName();
  26.  
  27.         ExecutorService threadManager = Executors.newCachedThreadPool();
  28.         threadManager.execute(new FileSender());
  29.  
  30.         try (DatagramSocket soket = new DatagramSocket()) {
  31.             String message = fileName + "\n" + fileLength;
  32.             byte[] messageBytes = message.getBytes();
  33.             DatagramPacket paket = new DatagramPacket(messageBytes, messageBytes.length,
  34.                     InetAddress.getByName(TARGET_HOST), INFO_PORT);
  35.             while (true) {
  36.                 soket.send(paket);
  37.                 Thread.currentThread().sleep(SLEEP_TO_NEXT_SEND);
  38.             }
  39.         } catch (SocketException e) {
  40.             // TODO Auto-generated catch block
  41.             e.printStackTrace();
  42.         } catch (UnknownHostException e) {
  43.             // TODO Auto-generated catch block
  44.             e.printStackTrace();
  45.         } catch (IOException e) {
  46.             // TODO Auto-generated catch block
  47.             e.printStackTrace();
  48.         } catch (InterruptedException e) {
  49.             // TODO Auto-generated catch block
  50.             e.printStackTrace();
  51.         }
  52.     }
  53.  
  54. }
  55. ============================================================================
  56. package cviko1A;
  57.  
  58. import java.io.IOException;
  59. import java.net.DatagramPacket;
  60. import java.net.DatagramSocket;
  61. import java.net.SocketException;
  62. import java.net.SocketTimeoutException;
  63. import java.util.Scanner;
  64.  
  65. public class FileReceiver {
  66.  
  67.     public static void main(String[] args) {
  68.         try (DatagramSocket soket = new DatagramSocket(FileInfoSender.INFO_PORT)) {
  69.             while (true) { // zo srandy
  70.                 byte[] buffer = new byte[soket.getReceiveBufferSize()];
  71.                 DatagramPacket paket = new DatagramPacket(buffer, buffer.length);
  72.                 soket.setSoTimeout((int) (FileInfoSender.SLEEP_TO_NEXT_SEND * 3));
  73.                 try {
  74.                     soket.receive(paket);
  75.                 } catch (SocketTimeoutException e) {
  76.                     System.err.println("Server nie je v sieti");
  77.                     return;
  78.                 }
  79.                 byte[] data = paket.getData();
  80.                 String message = new String(data).trim();
  81.                 System.out.println(message);
  82.                 Scanner scanner = new Scanner(message);
  83.                 String filename = scanner.nextLine();
  84.                 long fileLength = scanner.nextLong();
  85.                 scanner.close();
  86.                 System.out.println("Doslo: subor = " + filename);
  87.                 System.out.println("dlzka suboru = " + fileLength);
  88.                 System.out.println();
  89.             }
  90.         } catch (SocketException e) {
  91.             // TODO Auto-generated catch block
  92.             e.printStackTrace();
  93.         } catch (IOException e) {
  94.             // TODO Auto-generated catch block
  95.             e.printStackTrace();
  96.         }
  97.     }
  98.  
  99. }
  100. =================================================================
  101. package cviko1A;
  102.  
  103. import java.net.DatagramSocket;
  104. import java.net.SocketException;
  105.  
  106. public class FileSender implements Runnable {
  107.  
  108.     private DatagramSocket soket;
  109.  
  110.     public FileSender() throws SocketException {
  111.         soket = new DatagramSocket(FileInfoSender.REQUEST_PORT);
  112.     }
  113.  
  114.     @Override
  115.     public void run() {
  116.  
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement