Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cviko1A;
- import java.io.File;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class FileInfoSender {
- public static final String FILE_PATH = "C:\\Users\\pc12\\Downloads\\SYS_PRGM.ova";
- public static final int INFO_PORT = 5500;
- public static final int REQUEST_PORT = 5501;
- public static final int DATA_PORT = 5502;
- public static final String TARGET_HOST = "158.197.35.101"; // 158.197.35.255
- public static final long SLEEP_TO_NEXT_SEND = 1000; // 1sec
- public static void main(String[] args) {
- File file = new File(FILE_PATH);
- long fileLength = file.length();
- String fileName = file.getName();
- ExecutorService threadManager = Executors.newCachedThreadPool();
- threadManager.execute(new FileSender());
- try (DatagramSocket soket = new DatagramSocket()) {
- String message = fileName + "\n" + fileLength;
- byte[] messageBytes = message.getBytes();
- DatagramPacket paket = new DatagramPacket(messageBytes, messageBytes.length,
- InetAddress.getByName(TARGET_HOST), INFO_PORT);
- while (true) {
- soket.send(paket);
- Thread.currentThread().sleep(SLEEP_TO_NEXT_SEND);
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (UnknownHostException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- ============================================================================
- package cviko1A;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.SocketException;
- import java.net.SocketTimeoutException;
- import java.util.Scanner;
- public class FileReceiver {
- public static void main(String[] args) {
- try (DatagramSocket soket = new DatagramSocket(FileInfoSender.INFO_PORT)) {
- while (true) { // zo srandy
- byte[] buffer = new byte[soket.getReceiveBufferSize()];
- DatagramPacket paket = new DatagramPacket(buffer, buffer.length);
- soket.setSoTimeout((int) (FileInfoSender.SLEEP_TO_NEXT_SEND * 3));
- try {
- soket.receive(paket);
- } catch (SocketTimeoutException e) {
- System.err.println("Server nie je v sieti");
- return;
- }
- byte[] data = paket.getData();
- String message = new String(data).trim();
- System.out.println(message);
- Scanner scanner = new Scanner(message);
- String filename = scanner.nextLine();
- long fileLength = scanner.nextLong();
- scanner.close();
- System.out.println("Doslo: subor = " + filename);
- System.out.println("dlzka suboru = " + fileLength);
- System.out.println();
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- =================================================================
- package cviko1A;
- import java.net.DatagramSocket;
- import java.net.SocketException;
- public class FileSender implements Runnable {
- private DatagramSocket soket;
- public FileSender() throws SocketException {
- soket = new DatagramSocket(FileInfoSender.REQUEST_PORT);
- }
- @Override
- public void run() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement