4

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();
    }
}
7
  • As you have a TCP already, I would use that. FTP has to run on privileged ports which makes running your own more complicated. Commented Aug 27, 2015 at 14:19
  • refer to stackoverflow.com/questions/32226231/… Commented Aug 27, 2015 at 14:22
  • Thank you for the quick reply. I updated the main post with my code for the file transfer on tcp, however the file doesn't reach the server in whole Commented Aug 27, 2015 at 14:23
  • Your client should read in a loop until read returns -1. Otherwise you read at most 1024 bytes as that’s the size of your array, but for network streams, it might even read less for a single invocation of read as that method does not necessarily wait until your buffer array is full. By the way, your use of BufferedInputStream in 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. Commented Aug 27, 2015 at 14:28
  • You did not get InputStream from Socket in Server. Commented Aug 27, 2015 at 14:29

1 Answer 1

4

You did not get InputStream from the socket after serverSocket.accept(). Open InputStream on the socket.

    clientSocket = serverSocket.accept();

    InputStream in = clientSocket.getInputStream();

    // Writing the file to disk
    // Instantiating a new output stream object
    OutputStream output = new FileOutputStream("YourFile.zip");

    byte[] buffer = new byte[1024];
    while ((bytesRead = in.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
    // Closing the FileOutputStream handle
    output.close();

Refer to working example at : Write and Read File over Socket

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.