Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

All this code does is transfer the files. In order to make changes to the file, one has to re-transfer the file. During this time, I stand by to automate the update operation, where the server listens to the files and updates them automatically whenever any changes are made.

fileserver.java
import java.io.*;
import java.net.*;
import java.util.*;
public class FileServer {

private static ServerSocket serverSocket;
private static Socket clientSocket = null;

public static void main(String[] args) throws IOException {

try {
        serverSocket = new ServerSocket(13850);// creating a new serversocket
        System.out.println("Server started.");
} catch (Exception e)// catches errors and display them to the user for eg in case the port is busy we may specify a different port
{
        System.err.println("Port already in use.");
        System.exit(1);
}

    while (true)
{
try {
clientSocket = serverSocket.accept();// establishing the connection
System.out.println("Accepted connection : " + clientSocket);

Thread t = new Thread(new CLIENTConnection(clientSocket));
/*creating thread for clientconnection.java file and sending socket as an object thus everytime a connection is established a new thread/process is generated through which the file is sent and recieved*/
t.start();// executing the thread. here the new process or thread is created and ready to be implemented

} catch (Exception e) // catches error if any and displays it in the terminal/command prompt
{
System.err.println("Error in connection attempt.");
}
}
}
}

clientconnection.java
import java.util.*;
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.String;



public class CLIENTConnection implements Runnable {

private Socket clientSocket;
private BufferedReader in = null;

public CLIENTConnection(Socket client) {
this.clientSocket = client;
}

@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));

receiveFile();

in.close();

}


catch (IOException ex) {
Logger.getLogger(CLIENTConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void receiveFile() {
try {
int bytesRead;
DataInputStream clientData = new DataInputStream(clientSocket.getInputStream());

String fileName = clientData.readUTF();
OutputStream output = new FileOutputStream(("received_from_client_" + fileName));
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}

output.close();
clientData.close();

System.out.println("File "+fileName+" received from client.");
} catch (IOException ex) {
System.err.println("Client error. Connection closed.");
}
}


}

filecliet.java
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class FileClient {

private static Socket sock;// defining a socket
private static String fileName;// defining a file
private static BufferedReader stdin;// creating a buffered reader object to take and read the input from the users
private static PrintStream os;// creating an object of printstream to display text or output in the terminal/command prompt

public static void main(String[] args) throws IOException {
try {
sock = new Socket("localhost", 13850);// creating a socket specifying the port and the ip address of the server to the client
stdin = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
System.err.println("Cannot connect to the server, try again later."+e);
        System.exit(1);
}

os = new PrintStream(sock.getOutputStream());

try {

sendFile();

} catch (Exception e) {
System.err.println("not valid input");
}


sock.close();
}



public static void sendFile() {
try {
System.err.print("Enter file name: ");
fileName = stdin.readLine();//reads the file entered by the user 


File myFile = new File(fileName);
byte[] mybytearray = new byte[(int) myFile.length()];//determining the lenght of the file

FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);//creating an input stream to read the contents of the file
//bis.read(mybytearray, 0, mybytearray.length);

DataInputStream dis = new DataInputStream(bis);
dis.readFully(mybytearray, 0, mybytearray.length);

OutputStream os = sock.getOutputStream();

//Sending file name and file size to the server
DataOutputStream dos = new DataOutputStream(os);//creating an output stream to send the file to the server
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
dos.write(mybytearray, 0, mybytearray.length);//writing the contents of the file to the server
dos.flush();
System.out.println("File "+fileName+" sent to Server.");
} catch (Exception e) {                           //reports error if any
System.err.println("File does not exist!"); 
}
}


}
share|improve this question
Post it on Programmers quickly, then (if they'll even accept urgent requests). This isn't the place to ask for code. – Jamal Aug 7 at 4:12
3  
Don't post it on Programmers. – codesparkle Aug 7 at 4:43
@codesparkle: I apologize for that. Just a habit of mine, it seems. – Jamal Aug 7 at 4:47
i have sent my code along with the post! – manu Aug 7 at 5:17
now kindly review it – manu Aug 7 at 5:17

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.