Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.io.*;
- public class ServerRecieve extends Thread
- {
- Server server1;
- String[] args;
- public int newSize = 0;
- public long fileSize = 0;
- int fileSize2 = 0;
- int filePortNumber = 0;
- Socket servSocket;
- Socket fileSocket = new Socket();
- boolean m_bRunThread = true;
- boolean ServerOn = true;
- int gotPortNumber = 0;
- int once = 0;
- String fileNameGlobal = "";
- int fileSizeGlobal = 0;
- boolean startThreadOnce = false;
- public ServerRecieve(Socket s) throws FileNotFoundException
- {
- super();
- this.servSocket = s;
- }
- public void run()
- {
- try
- {
- //ServerConnect is supposed to be started somewhere in this file
- DataInputStream messagesFromServer = new DataInputStream(servSocket.getInputStream());
- DataOutputStream output = new DataOutputStream(servSocket.getOutputStream());
- boolean stop = false;
- while (!stop)
- {
- String response = messagesFromServer.readUTF();
- if(response!=null && !response.isEmpty())
- {
- if(response.matches(".*\\d+.*"))
- {
- System.out.println("got a number, it is: " + response);
- }
- else if(response.contains("`"))
- {
- String[] parts = response.split("`");
- String part1 = parts[0];
- //part1 contains file name....now send the file or file size
- File file= new File(part1);
- System.out.println("File exists: " + file.exists());
- System.out.println("File can be read: " + file.canRead());
- System.out.println("File file.canWrite(): " + file.canWrite());
- if ( file.exists() && file.canRead() )
- {
- // return the number of bytes in the file as a long int
- long file_size= file.length();
- System.out.println("File size is: " + file_size);
- if ( file_size > 0 )
- {
- System.out.println("432211");
- output.writeLong( file_size );
- System.out.println("File size sent.");
- }
- else
- {
- output.writeLong( 0L );
- }
- }
- else
- {
- output.writeLong( 0L );
- }
- FileInputStream file_input= new FileInputStream(file);
- System.out.println( "Transmitting file: " + part1);
- byte[] file_buffer= new byte[1500];
- int number_read;
- while( (number_read= file_input.read( file_buffer )) != -1 )
- output.write( file_buffer, 0, number_read );
- System.out.println("File " + part1 + " sent with " + number_read + " bytes read.");
- }
- else if(response.equals("x") || response.equals("X"))
- {
- stop = true;
- System.exit(0);
- break;
- }
- else if(!response.contains("`") && !response.equals("x") && !response.equals("X"))
- {
- System.out.println(response);
- }
- }
- else if(response == null)
- {
- stop = true;
- System.exit(0);
- break;
- }
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- }
- }
- void OutputOptions()
- {
- System.out.println("Enter an option ('m', 'f', 'x'): ");
- System.out.println("(M)essage (send)");
- System.out.println("(F)ile (request) ");
- System.out.println("e(X)it ");
- }
- public void sendFileSize(Socket socket4, int fileSize)
- {
- try
- {
- OutputStream os = socket4.getOutputStream();
- OutputStreamWriter osw = new OutputStreamWriter(os);
- BufferedWriter bw = new BufferedWriter(osw);
- bw.write("File size is:" + fileSize);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- finally
- {
- }
- }
- public void findFile(String name,File file)
- {
- File[] list = file.listFiles();
- if(list!=null)
- for (File fil : list)
- {
- if (fil.isDirectory())
- {
- findFile(name,fil);
- }
- else if (name.equalsIgnoreCase(fil.getName()))
- {
- System.out.println(fil.getParentFile());
- }
- }
- }
- public int strToInt( String str )
- {
- int i = 0;
- int num = 0;
- boolean isNeg = false;
- if (str.charAt(0) == '-')
- {
- isNeg = true;
- i = 1;
- }
- while( i < str.length())
- {
- num *= 10;
- num += str.charAt(i++) - '0';
- }
- if (isNeg)
- {
- num = -num;
- }
- return num;
- }
- }
Add Comment
Please, Sign In to add comment