Hey, as usual I’ve started looking into another part of Java that I’m not familiar at all with and I have a few questions to ask. My latest ‘experiment’ as I’ll call it has been to try and figure out how to transfer a file from one computer to another; It was quite easy to find a small client/server program to do this so I could see how it works. After a few minutes of fooling around and trying to figure out some of the program me and a friend successfully transferred a text file from my computer to his. For something so simple we freaked out and had a ton of fun with it. ;D
My first question stems from what I had to do to get the file transfer to work. As far as I can tell, from my extremely limited knowledge about what the below code does it seems that I needed to enter my external IP and then port-forward the specified port (13267) to my local ip address on the router so that the client program could connect to my computer and download the file. Is there a way to skip the port forwarding so that I could give anyone a program that would send a text file to my computer with no set up required?
The second question that I have is more of a ‘Did I understand this?’ type question. Below are the two program examples which I found on google and then modified two lines to get it working on my computer. I’ll just type what I think each program is doing and just correct whatever I get wrong if you can.
import java.net.*;
import java.io.*;
public class FileServer {
public static void main (String [] args ) throws IOException {
// create socket
ServerSocket servsock = new ServerSocket(13267);
while (true) {
System.out.println("Waiting...");
Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);
// sendfile
File myFile = new File ("test.txt");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}
}
}
IMO, the above code creates a new Socket on the port 13267, shows “Waiting…”, if the connection is accepted then it shows “Accepted connection:”, a file named test.txt is loaded and sent byte by byte to the client, the socket is closed.
import java.net.*;
import java.io.*;
public class FileClient{
public static void main (String [] args ) throws IOException {
int filesize=6022386; // filesize temporary hardcoded
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
// localhost for testing
Socket sock = new Socket("555.555.555.5",13267); //Ip of the FileServer and the Port to use.
System.out.println("Connecting...");
// receive file
byte [] mybytearray = new byte [filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("test-copy.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
// thanks to A. Cádiz for the bug fix
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
}
}
IMO, the above code connects to the ip 555.555.555.5 on port 13267, shows “Connecting…”, receives the file byte by byte and writes it to 'test-copy.txt, not sure about most of this, the socket is closed.