Link to home
Start Free TrialLog in
Avatar of codeQuantum
codeQuantumFlag for Canada

asked on

How to transfer a file from client to server

I need a simple way to transfer a tiny text file from a client java class to a server java class.

If possible, I want to do this bit per bit or octet per octet.

For the little background, this is for a course about java and networks... I am supposed to make java code to transfer the text file, then implement data verification later. (make sure no bits were lost in the way, and use a checksum to verify the data.)

But for now I only need help with file transfer.  Here is my "server" code so far :
// tcpServer.java by fpont 3/2000
 
// usage : java tcpServer <port number>.
// default port is 1500.
// connection to be closed by client.
// this server handles only 1 connection.
 
import java.net.*;
import java.io.*;
 
public class Serveur {
    
    public static void main(String args[]) {
	
	int port;
	ServerSocket server_socket;
	BufferedReader input;
	
	try { 
	    port = Integer.parseInt(args[0]);
	}
	catch (Exception e) {
	    System.out.println("port = 1500 (default)");
	    port = 1500;
	}
 
	try {
	    
	    server_socket = new ServerSocket(port);
	    System.out.println("Server waiting for client on port " + 
			       server_socket.getLocalPort());
	    
	    // server infinite loop
	    while(true) {
		Socket socket = server_socket.accept();
		System.out.println("New connection accepted " +
				   socket.getInetAddress() +
				   ":" + socket.getPort());
		input = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
		// print received data 
		try {
		    while(true) {
			String message = input.readLine();
			if (message==null) break;
			System.out.println(message);
		    }
		}
		catch (IOException e) {
		    System.out.println(e);
		}
		
		// connection closed by client
		try {
		    socket.close();
		    System.out.println("Connection closed by client");
		}
		catch (IOException e) {
		    System.out.println(e);
		}
		
	    }
	    
	    
	}
	
	catch (IOException e) {
	    System.out.println(e);
	}
    }
}
 
 
 
/*import java.io.*;
import java.net.*;
 
public class Serveur {
   static final int port = 1500;
 
   public static void main(String[] args) throws Exception {
        ServerSocket s = new ServerSocket(port);
        Socket soc = s.accept();
 
        // Un BufferedReader permet de lire par ligne.
        BufferedReader plec = new BufferedReader(
                               new InputStreamReader(soc.getInputStream())
                              );
 
        // Un PrintWriter possède toutes les opérations print classiques.
        // En mode auto-flush, le tampon est vidé (flush) à l'appel de println.
        PrintWriter pred = new PrintWriter(
                             new BufferedWriter(
                                new OutputStreamWriter(soc.getOutputStream())), 
                             true);
 
        while (true) {
           String str = plec.readLine();          // lecture du message
           if (str.equals("END")) break;
           System.out.println("ECHO = " + str);   // trace locale
           pred.println(str);                     // renvoi d'un écho
        }
        plec.close();
        pred.close();
        soc.close();
   }
}*/

Open in new window

SOLUTION
Avatar of MicheleMarcon
MicheleMarcon
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of codeQuantum

ASKER

Michele : where should that code go? what lines should it replace?

CEHJ : that code is for the client part right? since you did not mention the server part, does it means that it should work as it is?

I don't see how the pieces fit together, like where do i read the file name (to simplify, suppose it is a string i declare at the start of the code), or where we are saving the file. In my understanding :

Client code should : get a file path, open the file, transfer the file to the server, show success message on completion

Server code should : keep listening until it receives the file stream, saves the file (use same name as original file), show success message on completion

Keep in mind that I am a beginner on this subject, and that the code you see above is pretty much the result of copy/paste. So I would appreciate some details and explanations.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How do I create an InputStream from a file?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the late reply. Thanks everyone!!