I am new to Java networking, and having looked for a resolution for my problem for a while now, I figured why not ask some advice from some more qualified people on this matter?
I currently have made a small tool which manages a server of mine, and another small client tool. My goal is for the tool to be able to send commands from the client to the server computer. This way I can perform certain actions on the server computer from another machine, including sending a zip archive with updated files.
I have the basics setup: a TCP connection that sends a command from client to server (server replies with a confirmation) and then I would like the supposed action to take place. My question now is this:
When sending a file (.zip) from the client to server, should I send it over TCP or use something like FTP? I would not only like to send the file to the server, but also when it arrived to extract and replace the existing files.
Kind regards, Alex
EDIT: This is what I used for transferring a file from the client to server, however the file doesn't reach the destination in full size.. D:
Server
package server.control.net.impl;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Update implements Runnable {
private final static int serverPort = 5555;
private final static String fileInput = "C:\\Users\\Alexander\\Documents\\update.zip";
public static void main(String args[]) throws IOException{
ServerSocket servsock = new ServerSocket(serverPort);
File myFile = new File(fileInput);
while (true) {
Socket sock = servsock.accept();
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sock.close();
}
}
public static void start(){
Update upd = new Update();
Thread tupd = new Thread(upd);
tupd.start();
}
@Override
public void run() {
}
}
Client
package server.control.net;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClient {
private final static String serverIP = "127.0.0.1";
private final static int serverPort = 5555;
private final static String fileOutput = "C:\\Users\\Alexander\\Documents\\updateoutput.zip";
public static void main(String args[]) throws UnknownHostException, IOException {
Socket sock = new Socket(serverIP, serverPort);
byte[] mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(fileOutput);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
}
readreturns-1. Otherwise you read at most1024bytes as that’s the size of your array, but for network streams, it might even read less for a single invocation ofreadas that method does not necessarily wait until your buffer array is full. By the way, your use ofBufferedInputStreamin the server is obsolete. By using an array of the same size as the file size you already have a sufficient buffer on your own.