文件共享代码在localhost上运行,在网络上运行失败

时间:2012-03-26 12:30:20

标签: java sockets networking socketexception fileshare

我的项目包含一个应用程序,用户可以使用它们自己发送文件。

因此,作为一个基本单元,我开发了一个FileServer和一个FileClient程序。

此代码在localhost上完全正常,但在网络(wifi)上失败。

估计问题是客户端套接字在接收后的某个时间点后关闭或没有响应。但我真的无法弄清问题是什么或如何解决问题。

出现的错误是SocketException:软件导致连接中止。套接字写错误。

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();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要指定要收听的界面。默认为本地接口。 (可能依赖OS)。 ServerSocket bind()解释了其工作原理(例如构造函数和{{1}}函数)。