i want to implement client server communication through wirless network in java,by using Socket,ServerSocket classes can i connect two computers with wireless connection???

Dani AI

Generated

Brief clarification tied to the earlier posts: sockets operate over whatever IP network is present, so the Java Socket/ServerSocket approach works on Wi‑Fi the same way it works on Ethernet. was correct that an actual network interface and routing (local LAN or routed/forwarded path) must exist before the two JVMs can speak.

Practical checklist and cautions before implementing file transfer:

  • Pick a stable port (above 1024 to avoid privileges), bind the server to the correct local address (or 0.0.0.0) and discover that address with ipconfig/ifconfig.
  • Allow the chosen port through the host firewall and confirm Java is permitted to accept connections.
  • If machines are on different networks, arrange NAT port forwarding, use a VPN, or relay through a public server; direct client->server requires a reachable IP and open port.
  • Consider integrity and privacy: send file length and name, verify bytes received, and use SSL/TLS (SSLSocket) for sensitive data.

Minimal, reliable pattern (send filename + length, then raw bytes) — server receives and writes to disk, client sends metadata then bytes:

// Server: accepts one file, saves as recv_<name>
ServerSocket ss = new ServerSocket(9000);
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("recv_" + name)) {
    byte[] buf = new byte[8192];
    int r;
    long rem = len;
    while (rem > 0 && (r = in.read(buf,0,(int)Math.min(buf.length, rem))) != -1) {
      fos.write(buf,0,r);
      rem -= r;
    }
  }
}
// Client: sends filename and length, then file bytes
try (Socket s = new Socket("SERVER_LOCAL_IP", 9000);
     DataOutputStream out = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
     FileInputStream fis = new FileInputStream("sendfile.bin")) {
  File f = new File("sendfile.bin");
  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();
}

Quick troubleshooting: confirm server IP/port with netstat, test reachability with ping or telnet/nc, check firewall logs, and validate file length after transfer. This pattern keeps logic simple and works reliably on the same Wi‑Fi subnet or across networks when routing/forwarding is configured.

Recommended Answers

All 4 Replies

Not with pure Java, no. The ad-hoc network will already have to exist and then you can use it to connect from one to the other, yes.

i have listened that if u are using sockets,then the systems must be connected through lan physically......is that not true,can u help me in transfering files from one system to another system which are connected in a wireless network??

They have to be connected through a network. Whether that network is a "cable" network, or a "wireless" network is irrelevant. "cable" is not what is meant by "physically". "Physically", in that statement, means that an actual network connection exists, but it must be a "real", standard network connection.

ohk then it will work for both wired and wireless nwtworks......thanks......can u help me in implementing file transfer in wireless network??

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.