简体   繁体   中英

File Sharing Code works on localhost, fails on Network

My project consists of an application using which users can send files among themselves.

Hence as a basic unit, I developed a FileServer and a FileClient Program.

This code works perfectly fine on localhost but fails on the Network (wifi).

An estimated problem is that the client socket is getting closed or isn't responding after a certain point of time while recieving. But i cant really figure out what the problem is or how to approach the problem.

The error appears is a SocketException: Software caused connection abort. Socket write error.

FileServer CODE:

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

    public class FileServerPC {

    static String remoteIPaddress = "192.168.43.95";
    static String filename;
    String path;
    static File selectedfile = null;
    static ServerSocket servsock = null;
    static Socket sock = null;
    static FileInputStream fis;
    static BufferedInputStream bis;
    static DatagramSocket theSocket = null;
    static DatagramPacket theOutput;
    static OutputStream os;
    byte[] mybytearray;
    static double nosofpackets;
    static int packetsize = 1024;

    public FileServerPC() {
        try {
            servsock = new ServerSocket(1500);
            theSocket = new DatagramSocket(9999);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        FileServerPC fs = new FileServerPC();

        selectedfile = new File("C:\\flower.jpg");
        filename = selectedfile.getName();


        Runnable runnable = new Runnable() {
            @Override
            public void run() {

        try {
            System.out.println("Waiting...");

            sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);
            fis = new FileInputStream(selectedfile);
            System.out.println((int) selectedfile.length());
            long length = selectedfile.length();
            System.out.println("LENGTH: " + length);
            nosofpackets = Math
                    .ceil(((int) selectedfile.length()) / packetsize);
            // progressBar.setValue(50);
            bis = new BufferedInputStream(fis);
            String message = length + "`~`" + filename;
            byte[] data = message.getBytes();
            theOutput = new DatagramPacket(data, data.length,
                    InetAddress.getByName(remoteIPaddress),8888);
            theSocket.send(theOutput);
            byte[] newbytearray = new byte[packetsize];

            int dur = (int) (selectedfile.length());
            int dur1 = dur / 100;
            int counter = 0;

            System.out.println("duration: " + dur + "nosofpackets: "
                    + nosofpackets + "dur1: " + dur1);
            int val = 0;
            for (int i = 0; i <= nosofpackets ; i++) {
                os = sock.getOutputStream();
                bis.read(newbytearray, 0, newbytearray.length);
                os.write(newbytearray, 0, newbytearray.length);
                counter = counter + newbytearray.length;

                val = counter / dur1;
                System.out.println(val);
                os.flush();
            }
            System.out.println(val);
            os.flush();
            os.close();
            sock.close();
            theSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
            }
        };

        new Thread(runnable).start();
    } 

} 


FileClient CODE:


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

public class FileClientPC
{
    public static void main(String[] args) throws IOException
    {
        long start = System.currentTimeMillis();

        String remoteip="192.168.43.237";

        Socket sock = new Socket(remoteip, 1500);
        System.out.println("Connecting...");

        InputStream is = sock.getInputStream();
        DatagramSocket ds = new DatagramSocket(8888);

        byte[] buffer = new byte[65507]; 
        DatagramPacket dp = new DatagramPacket(buffer, buffer.length); 

        try {
            ds.receive(dp);
        } catch (IOException e) {
            System.err.println("chat error2 " + e);
        }

        String s = new String(dp.getData(), 0, dp.getLength());

        String temp[]=s.split("`~`");

        String size = temp[0];
        String name = temp[1];

        System.out.println(size +" "+ name);
        long sizeint = Integer.parseInt(size);
        System.out.println("Filename: " + name);

        FileOutputStream fos = new FileOutputStream("D:\\"+name);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        int packetsize = 1024;

        double nosofpackets = Math.ceil(sizeint / packetsize);

        System.out.println("No of packets: "+Double.toString(nosofpackets));

        byte newbytearray[];
        for (int i = 0; i <= nosofpackets; i++) {
            is = sock.getInputStream();
            newbytearray = new byte[packetsize];
            int bytesRead = is.read(newbytearray, 0, newbytearray.length);
            System.out.println("Packet:" + (i + 1));
            bos.write(newbytearray, 0, newbytearray.length);
        }

        long end = System.currentTimeMillis();
        bos.close();
        sock.close();
        ds.close();
    }
}

You need to specify which interface you want to listen to. The default is the local interface. (probably OS dependent). The API for ServerSocket explains how this works (the constructor and bind() functions for instance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM