Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  File Transfer Question  (Read 489 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-01-11 00:18:53 »

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.  Grin

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. Wink

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 {
    // 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.


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; // 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.
Offline Best Username Ever

Junior Duke





« Reply #1 - Posted 2013-01-11 01:04:32 »

Don't bother to learn network stuff until after you learn how to use streams. Write a program that uses input and output streams to copy one file to another location. Use a fixed size buffer, no matter how large the file is. Then try writing a network program that sends one byte and closes the stream and another program that prints the first byte received and closes the stream. To test networking on a local network, use your local IP (probably 192.168.1.*) or a loopback IP (127.0.0.1). Another thing you can do to test streams is to use a pipe. Working with InputStreams and OutputStreams is essentially the same thing with any stream type. Pipes are designed to allow communication on the same computer. (Not a network.)

After that, post your code here. You should know how the code works well enough to write your own. There are some problems with that code, so do not use it as a model.
Offline tyeeeee1
« Reply #2 - Posted 2013-01-11 01:11:26 »

Don't bother to learn network stuff until after you learn how to use streams. Write a program that uses input and output streams to copy one file to another location. Use a fixed size buffer, no matter how large the file is. Then try writing a network program that sends one byte and closes the stream and another program that prints the first byte received and closes the stream. To test networking on a local network, use your local IP (probably 192.168.1.*) or a loopback IP (127.0.0.1). Another thing you can do to test streams is to use a pipe. Working with InputStreams and OutputStreams is essentially the same thing with any stream type. Pipes are designed to allow communication on the same computer. (Not a network.)

After that, post your code here. You should know how the code works well enough to write your own. There are some problems with that code, so do not use it as a model.

Thanks, I'll try to put some time aside over the next day or two and see if I can experiment with that.  Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!