Connecting Two Machines Without the Internet: Ethernet and Ad-Hoc Wi-Fi Methods

Connecting Two Machines Without the Internet: Ethernet and Ad-Hoc Wi-Fi Methods

Connecting Two Machines Without the Internet: Ethernet and Ad-Hoc Wi-Fi Methods

In this guide, we will explore two common methods to connect two machines for direct communication without using the internet. This can be useful for isolated testing and developing networked applications. We'll cover:

  • Direct Ethernet Connection
  • Ad-Hoc Wi-Fi Network

Method 1: Direct Ethernet Connection 🔌

A direct Ethernet connection involves using an Ethernet cable to connect the two machines directly.

Step-by-Step Guide

1. Get an Ethernet Cable:

  • Use a crossover Ethernet cable for direct connections between two computers. Modern network interfaces often support Auto-MDIX, so a regular Ethernet cable might work as well.

2. Connect the Machines:

  • Plug one end of the Ethernet cable into the Ethernet port of the first machine.
  • Plug the other end into the Ethernet port of the second machine.

3. Configure IP Addresses:

Assign static IP addresses to both machines.

On Windows:
  1. Open Control Panel > Network and Sharing Center > Change adapter settings.
  2. Right-click the Ethernet connection and select Properties.
  3. Select Internet Protocol Version 4 (TCP/IPv4) and click Properties.
  4. Set the following for each machine:
    • Machine 1: IP Address: 192.168.1.1, Subnet Mask: 255.255.255.0
    • Machine 2: IP Address: 192.168.1.2, Subnet Mask: 255.255.255.0
On macOS/Linux:
sudo ifconfig eth0 192.168.1.1 netmask 255.255.255.0 up   # Machine 1
sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up   # Machine 2
        

4. Verify the Connection:

Test the connection by pinging the other machine.

On Windows:
ping 192.168.1.2   # From Machine 1
ping 192.168.1.1   # From Machine 2
        
On macOS/Linux:
ping 192.168.1.2   # From Machine 1
ping 192.168.1.1   # From Machine 2
        

Method 2: Ad-Hoc Wi-Fi Network 📶

An ad-hoc Wi-Fi network allows two or more devices to connect directly via Wi-Fi without needing a router.

Step-by-Step Guide

On Windows:

1. Create Ad-Hoc Network:

netsh wlan set hostednetwork mode=allow ssid=MyAdHocNetwork key=password123
netsh wlan start hostednetwork
    

This creates and starts an ad-hoc network with SSID MyAdHocNetwork and password password123.

2. Connect to Ad-Hoc Network:

  • On the second machine, open the Wi-Fi settings.
  • Find MyAdHocNetwork and connect using the password password123.

On macOS:

1. Create Ad-Hoc Network:

  • Click on the Wi-Fi icon in the menu bar and select “Create Network”.
  • Set the Network Name, Channel, and Security options.

2. Connect to Ad-Hoc Network:

  • On the second machine, open the Wi-Fi settings.
  • Find the created network and connect.

On Linux:

1. Create Ad-Hoc Network:

nmcli dev wifi hotspot ifname wlan0 ssid MyAdHocNetwork password password123
    

This command creates an ad-hoc network with SSID MyAdHocNetwork and password password123.

2. Connect to Ad-Hoc Network:

  • On the second machine, open the Wi-Fi settings.
  • Find MyAdHocNetwork and connect.

Example: Java Socket Communication 💻

After setting up the direct connection (either via Ethernet or ad-hoc Wi-Fi), you can use Java sockets to communicate between the machines.

Server Code

import java.io.*;
import java.net.*;

public class MathServer {
    private static final int PORT = 8080;

    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(PORT)) {
            System.out.println("Server is listening on port " + PORT);

            while (true) {
                try (Socket socket = serverSocket.accept();
                     PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

                    String problem = in.readLine();
                    String result = solveProblem(problem);
                    out.println(result);
                } catch (IOException e) {
                    System.out.println("Client disconnected");
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private static String solveProblem(String problem) {
        // Example problem solver
        if (problem.startsWith("factorial:")) {
            int num = Integer.parseInt(problem.split(":")[1]);
            return String.valueOf(factorial(num));
        }
        return "Invalid problem";
    }

    private static int factorial(int n) {
        if (n <= 1) return 1;
        else return n * factorial(n - 1);
    }
}
    

Client Code

import java.io.*;
import java.net.*;

public class MathClient {
    private static final String HOST = "192.168.1.1"; // IP of the server machine
    private static final int PORT = 8080;

    public static void main(String[] args) {
        try (Socket socket = new Socket(HOST, PORT);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {

            System.out.println("Connected to the server");

            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println("factorial:" + userInput);
                String response = in.readLine();
                System.out.println("Result: " + response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
    

Conclusion 🔚

By using either a direct Ethernet connection or an ad-hoc Wi-Fi network, you can set up communication between two machines without the need for the internet. Once connected, you can use Java sockets to implement a server-client applications. In our next post, we'll implement a server-client application to solve mathematical problems. This setup is useful for testing and developing networked applications in an isolated environment.