Create your own Simple File Transfer App over LAN (with Source code in JAVA)

Many Core Java packages can be used to create useful applications. This application uses java.net package to establish connection between two systems residing on the same network.  This point is important, because it will not work for arbitrary networks (having no connection between them). For those who don’t have basic networking knowledge, in the simplest language same network means computer systems connected through the same router, or directly connected to each other via LAN Wire.

Networking Basics : When we connect to a network (a router), we get an IP from the router, which is usually of the form xxx.xxx.x.x, where x is a no. between 0 to 255 like 192.168.1.5 or 192.168.0.5 . Usually the first address in the series is of the router itself, i.e. 192.168.1.1.
In networking, there is a simple concept of a socket which is the combination of IP Address and Port number. Port no. is a is a no. between 0 to 65535. Out of this, 0 to 1023 are reserved for specific services and 1024 to 65536 can be used for our codes. In java, there are two classes which help in communication via these ports. These are namely, java.net.Socket and java.net.ServerSocket.  Socket acts as a client and ServerSocket acts as a server.

This simple software uses these two classes to send and receive files. ServerSocket opens a port on a system and waits for a client to connect to that port. On the other end, a system on the same network uses Socket class to connect to this device to establish connection and recieve data. The serveris currently able to send one file at a time to client.

The image shown below shows both the Client and Server side applications :

server

The Server Side Application has options to select the file to be sent and then press the button “Send File”. For the client side, it has a text field where we enter the address specified by the server (which is 192.168.1.6 here). The default location where the client saves the file is Desktop of the user but the user can change the path where these files are saved after downloading. But, protected paths like C: and C:Program Files should be avoided as the destination paths, else an error is shown and Client shutdowns. The default location is then reset again to Desktop.

A simple setup using Batch script, VB Script,Registry Script and Winrar was created to deploy the JAR file onto Windows Systems both 32 and 64 bit. JRE/ JDK 1.4 or above should be installed to use this application. You can try this application on your same system (local server) by using 127.0.0.1 as server address on client side.After installation on windows system, you will get these options on Right Click of each file/folder (called as context menu), which allows instant sharing as shown below.

Screenshot of Installed Program

Screenshot of Installed Program

Download the application from here

SOURCE

Client side source code (Client.java) :

Server Side code (Server.java):

Just let us know if you are not able to understand any method or class used here

3 Comments

  1. what does lastIndexOf(“/”) returns? I mean we do not have a backslash in ip addresses?

  2. @simran : please specify line no. and the code you are talking about (client/server)

    If you are having problem in getLocalHost() method then here is the reason :InetAddress.getLocalHost() returns IP with the PC name like : Waheguru_HP/192.168.1.5

    to display only IP and remove PC name, I have used lastIndexOf(“/”);

  3. lastIndexOf(“/”) return the position of the character that occurs rightmost from beginning of the string.

    Example:
    String s = “The Geeky way”;
    System.out.println(s.lastIndexOf(“e”));
    This will return: 6.

    Notice that char “e” is trice in the given string. It searches and finds the last occurring position of char “e”.
    Spaces are also included as character and index always start from 0.

    Program given in post : “/” is being used to separate computer name (Host name) and IP.

Leave a Reply

Your email address will not be published. Required fields are marked *