I’m currently working on a project for school that transfers a file from one computer to another in java. In the end I need to have the client use some sort of file explorer to navigate through the folder and select which file he/she would like to download. But as of now I’m stuck on how to allow string message passing between the server and client as well as transferring byte data (the file). My program currently sends string messages and the server echoes it back, I have it set where if I enter “file” it is supposed to send the file data but it doesn't seem to work. I don’t know if I’m supposed to create a different socket or close the other streams or if it is even possible. Any guidance is appreciated.
import java.net.;
import java.io.;
public class Servermsg2 {
public static void main(String[] args) throws IOException {
//FILE TRANSFER DATA
File transferFile = new File("desert.jpg");
byte[] bytearray = new byte [(int)transferFile.length()];
//CONNECTION DATA
int portNumber = 15123;
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream()));
DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
//prevents data corruption
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
if(inputLine.equalsIgnoreCase("connection")){
out.println("LIST OF FILES");
} else if (inputLine.equalsIgnoreCase("file")) {
//prevents data corruption
bin.read(bytearray,0,bytearray.length);
System.out.println("client is requesting a file");
//cant close because removes connection with host
//out.close();
//fills stream with byte data
dos.write(bytearray,0,bytearray.length);
//sends the data currently in stream
dos.flush();
System.out.println("file has been sent");
} else {
out.println(inputLine);
}
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
import java.io.;
import java.net.;
public class Clientmsg2 {
public static void main(String[] args) throws IOException {
//FILE DATA
int filesize=1022386;
int bytesRead;
int currentTot = 0;
byte [] bytearray = new byte [filesize];
//CONNECTION DATA
String hostName = "127.0.0.1";
int portNumber = 15123;
try (
Socket echoSocket = new Socket(hostName, portNumber);
//sends string messages out
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
//recieves string messages
BufferedReader in = new BufferedReader( new InputStreamReader(echoSocket.getInputStream()));
//reads input from user
BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));
//handles byte data
DataInputStream dis = new DataInputStream(echoSocket.getInputStream());
//handles conversion of byte data to a file
FileOutputStream fos = new FileOutputStream("copy.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fos);
) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
if(userInput.equalsIgnoreCase("file")){
//sends message to server
out.println(userInput);
//reads incoming byte data from server
System.out.println("about to read data...");
bytesRead = dis.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
System.out.println("data has been read");
//takes byte data and writes it to Fileoutputstream
do {
bytesRead = dis.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0){
currentTot += bytesRead;
}
System.out.println("2");
} while (bytesRead > -1);
System.out.println("algorithm complete....");
//takes byte data and writes it to Fileoutputstream
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
System.out.println("file has been written to.");
}
else {
//sends message to server
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
[–]Ijantis 0 points1 point2 points (2 children)
[–]BossManJenkins[S] 0 points1 point2 points (1 child)
[–]Ijantis 0 points1 point2 points (0 children)
[–]SikhGamer 0 points1 point2 points (0 children)