Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- public class FileServer {
- static ServerSocket servSock;
- static Socket initSocket;
- static String fileSize = "";
- static File myFile = new File("attachment.pdf");
- static byte [] byteArr;
- static FileInputStream fis;
- static BufferedInputStream bis;
- static OutputStream os;
- static final int MAX_THREAD = 3;
- static String[] startAndEnd;
- public static void main(String[] args) throws IOException {
- System.out.println("Waiting...");
- if (!initSocket())
- return;
- if (!sendFileSize())
- return;
- if(!initFile())
- return;
- while(true){
- listenRecieveConnection();
- receiveOffsetFile();
- sendFile();
- }
- }
- public static boolean initSocket() {
- try {
- servSock = new ServerSocket(13267);
- } catch (IOException e) {
- System.out.println(e.getLocalizedMessage());
- disconnected();
- return false;
- }
- try {
- initSocket = servSock.accept();
- } catch (IOException e) {
- System.out.println(e.getLocalizedMessage());
- disconnected();
- return false;
- }
- System.out.println("Accepted connection : " + initSocket);
- return true;
- }
- public static boolean initFile() {
- try {
- fis = new FileInputStream(myFile);
- bis = new BufferedInputStream(fis);
- byteArr = new byte [(int)myFile.length()];
- bis.read(byteArr,0,byteArr.length);
- } catch (IOException e) {
- System.out.println(e.getLocalizedMessage());
- return false;
- }
- return true;
- }
- public static boolean sendFileSize() {
- fileSize += myFile.length() + "\n";
- try {
- os = initSocket.getOutputStream();
- os.write(fileSize.getBytes(),0,fileSize.getBytes().length);
- os.flush();
- } catch (IOException e) {
- System.out.println(e.getLocalizedMessage());
- disconnected();
- return false;
- }
- return true;
- }
- public static void disconnected() {
- try {
- servSock.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static boolean listenRecieveConnection() {
- try {
- initSocket = servSock.accept();
- } catch (IOException e) {
- System.out.println(e.getLocalizedMessage());
- disconnected();
- return false;
- }
- System.out.println("Accepted connection : " + initSocket);
- return true;
- }
- public static boolean receiveOffsetFile() {
- try {
- BufferedReader in = new BufferedReader(new InputStreamReader(initSocket.getInputStream()));
- String line =in.readLine();
- startAndEnd = line.split(" ");
- } catch (IOException e) {
- System.out.println(e.getLocalizedMessage());
- disconnected();
- return false;
- }
- return true;
- }
- public static boolean sendFile() {
- int start = Integer.parseInt(startAndEnd[0]) - 1;
- int end = Integer.parseInt(startAndEnd[1]) - 1;
- int size = (end - start) + 1;
- try {
- os = initSocket.getOutputStream();
- os.write(byteArr,start,size);
- os.flush();
- System.out.println("Send file to : " + initSocket);
- } catch (IOException e) {
- System.out.println(e.getLocalizedMessage());
- disconnected();
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement