Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- public class ClientRecieve extends Thread
- {
- int filePortNumber = 0;
- Socket servSocket;
- Socket fileSocket;
- boolean m_bRunThread = true;
- boolean ServerOn = true;
- int once = 0;
- String fileNameGlobal = "";
- public ClientRecieve(Socket s) throws FileNotFoundException
- {
- super();
- this.servSocket = s;
- }
- public void run()
- {
- try
- {
- DataInputStream messagesFromServer = new DataInputStream(servSocket.getInputStream());
- boolean stop = false;
- DataOutputStream output = new DataOutputStream(servSocket.getOutputStream());
- 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);
- if ( file.exists() && file.canRead() )
- {
- long file_size= file.length();
- System.out.println("File size is: " + file_size);
- if ( file_size > 0 )
- {
- output.flush();
- output.writeLong( file_size );
- output.flush();
- }
- }
- else
- {
- output.flush();
- output.writeLong( 0L );
- output.flush();
- }
- 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 );
- }
- output.flush();
- 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 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