Hi,

I want to write a java program that send a file through cable to another PC automatically in specefic time..

I don't know which cable to use(crossover or usb) and how to read and write from the serial port..

i tried to do it in VB(with help from some online sources) but since im new in VB it didn't work with me..
please any help will be appreciated..
Thanks,

Dani AI

Generated

Building on and pointing toward an IP-based approach, a practical, reliable solution is a tiny Java server on the receiving PC and a scheduled Java client on the sender. The server listens on a fixed port and writes incoming bytes to disk; the client opens a socket at the appointed time, sends a short header (filename + length), then streams the file in chunks. This avoids special USB bridge cables and the headaches of low-level serial drivers.

Example receiver (runs on the machine that will accept files):

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

public class FileReceiver {
  public static void main(String[] args) throws Exception {
    int port = 5000;
    try (ServerSocket ss = new ServerSocket(port)) {
      while (true) {
        try (Socket s = ss.accept();
             DataInputStream in = new DataInputStream(new BufferedInputStream(s.getInputStream()))) {
          String name = in.readUTF();
          long len = in.readLong();
          try (FileOutputStream fos = new FileOutputStream(name)) {
            byte[] buf = new byte[8192];
            long got = 0;
            while (got < len) {
              int r = in.read(buf, 0, (int)Math.min(buf.length, len - got));
              if (r < 0) break;
              fos.write(buf, 0, r);
              got += r;
            }
          }
        }
      }
    }
  }
}

Example sender (scheduled job; replace the scheduler or run via OS task scheduler):

import java.net.*;
import java.io.*;
import java.util.concurrent.*;

public class FileSender {
  public static void main(String[] args) throws Exception {
    Runnable job = () -> {
      File f = new File("send.bin");
      try (Socket s = new Socket("192.168.2.2", 5000);
           DataOutputStream out = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
           FileInputStream fis = new FileInputStream(f)) {
        out.writeUTF(f.getName());
        out.writeLong(f.length());
        byte[] buf = new byte[8192];
        int r;
        while ((r = fis.read(buf)) != -1) out.write(buf,0,r);
        out.flush();
      } catch (IOException e) { e.printStackTrace(); }
    };
    ScheduledExecutorService ex = Executors.newSingleThreadScheduledExecutor();
    ex.schedule(job, 10, TimeUnit.SECONDS); // example delay
  }
}

Notes and troubleshooting: when connecting two PCs directly with Ethernet, assign static IPs on the same subnet (e.g., 192.168.2.1/24 and 192.168.2.2/24); modern NICs auto MDI-X so a standard cable usually works. Open the chosen port in firewalls on both ends. For serial/USB-style transfers, use a library such as jSerialComm (https://fazecast.github.io/jSerialComm/) or a proper USB bridge cable—plain USB-A-to-USB-A will not work. For reliability add a checksum (MessageDigest) and a retry/resume strategy, or use OS-level scheduling (cron/Task Scheduler) if you prefer not to keep a Java scheduler running. For socket basics see the official Java tutorial: Java Sockets Tutorial.

Recommended Answers

All 3 Replies

There are a lot of options you can choose from:

Connections:
1. using an ethernet cable
2. using RS-232
3. USB ?

I suggest using an ethernet cable. its very easy to set-up a network and write a communications program in java (ie., using sockets, other network protocols etc). In using an RS-232, you need to know about COM ports and the specifics of the protocols, take a look on here:

Please do you know any links that can help to write a communication program using an ethernet cable..

Thanks

Just see the standard networking tutorial. Regardless of whether it is twisted pair, token ring, usb, whatever, whatever, you can and most definately should set up an IP network on it, then you can use the normal network classes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.