Last active
March 20, 2019 23:38
-
-
Save michalwiacek/4242dff54bbcb697cc1d3fe80b37df8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.company; | |
| import java.io.*; | |
| import java.net.*; | |
| public class FileClient{ | |
| public static void main(String[] args){ | |
| try( | |
| Socket socket = new Socket("localhost", 8000);//Nawiızywanie po³ıczenia z serwerem | |
| InputStream is = socket.getInputStream();//pobieranie strumienia bajtowego WE | |
| FileOutputStream fos = new FileOutputStream(new File("clientFile.dat"));//strumieñ plikowy WY pracujıcy na bajtach | |
| ObjectInputStream ois = new ObjectInputStream(is) | |
| ){ | |
| System.out.println("Connected to server..."); | |
| System.out.println("File is downloading..."); | |
| Message msg = (Message) ois.readObject(); | |
| System.out.println(msg.toString()); | |
| fos.write(msg.getContent(),0, msg.getLenght()); | |
| fos.close(); | |
| System.out.println("File downloaded"); | |
| }catch(Exception e){ | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.company; | |
| import java.io.*; | |
| import java.net.*; | |
| import java.util.Date; | |
| public class FileServer{ | |
| public static void main(String[] args){ | |
| try{ | |
| File file = new File("serverFile.dat"); | |
| if(file.exists()){ | |
| ServerSocket server = new ServerSocket(8000); // uruchamianie gniazda serwera | |
| System.out.println("Server run..."); | |
| while(true){ | |
| try( | |
| Socket socket = server.accept();// akceptacja po³ıczenia z klientem | |
| OutputStream os = socket.getOutputStream();//pobieranie bajtowego strumienia WY | |
| FileInputStream fis = new FileInputStream(file);//Tworzenie strumienia bajtowego WE do pliku | |
| BufferedInputStream bis = new BufferedInputStream(fis);//buforowanie strumienia bajtów | |
| ObjectOutputStream oos = new ObjectOutputStream(os) | |
| ){ | |
| System.out.println("Client accepted..."); | |
| int k = 0; | |
| byte[] data = new byte[512];//tablica (paczka) na dane | |
| System.out.println("File is sending "); | |
| while((k=bis.read(data))!=-1)//buforowany odczyt danych ze strumienia WE(pliku) | |
| { | |
| Message msg = new Message(data, new Date(), k); | |
| oos.writeObject(msg); | |
| oos.close(); | |
| } | |
| oos.writeObject(null); | |
| System.out.println("File has been sent"); | |
| } | |
| System.out.println("Client disconnected "); | |
| } | |
| }else{ | |
| System.err.println(String.format("File '%s' doesn't exist in the main server directory...", file.getAbsolutePath())); | |
| } | |
| }catch(Exception e){ | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.company; | |
| import java.io.Serializable; | |
| import java.util.Arrays; | |
| import java.util.Date; | |
| public class Message implements Serializable { | |
| private static final long serialVersionUID = 6529685098267757690L; | |
| byte[] content; | |
| Date date; | |
| int lenght; | |
| public byte[] getContent() { | |
| return content; | |
| } | |
| public void setContent(byte[] content) { | |
| this.content = content; | |
| } | |
| public Date getDate() { | |
| return date; | |
| } | |
| public void setDate(Date date) { | |
| this.date = date; | |
| } | |
| public int getLenght() { | |
| return lenght; | |
| } | |
| public void setLenght(int lenght) { | |
| this.lenght = lenght; | |
| } | |
| public Message(byte[] content, Date date, int lenght) { | |
| this.content = content; | |
| this.date = date; | |
| this.lenght = lenght; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Message{" + | |
| "content=" + Arrays.toString(content) + | |
| ", date=" + date + | |
| ", lenght=" + lenght + | |
| '}'; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| czesc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment