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.

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.net.*; import java.io.*;
public class FileServer { public static void main (String [] args ) throws IOException { ServerSocket servsock = new ServerSocket(13267); while (true) { System.out.println("Waiting...");
Socket sock = servsock.accept(); System.out.println("Accepted connection : " + sock);
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.net.*; import java.io.*;
public class FileClient{ public static void main (String [] args ) throws IOException { int filesize=6022386; long start = System.currentTimeMillis(); int bytesRead; int current = 0; Socket sock = new Socket("555.555.555.5",13267); System.out.println("Connecting...");
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;
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.