I have wrote a socket program where the host will receive file and client will send ( as far as I am concerned ). However, today when I checked it,I saw if I run the Host, nothing happens. But When I run the Client side, server side start prompting and asks me for file to send. and then server starts sending the file to my client pc. here is the code Server side:

ServerSocket servsock = new ServerSocket(50000);
        Socket sock = servsock.accept();


        File to_b_encf = new File("input");

        OutputStream out = sock.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(to_b_encf);

        byte[] buffer = new byte[64 * 1024];
        int bytesRead = 0;

        while ((bytesRead = fileInputStream.read(buffer)) != -1) {
            if (bytesRead > 0) {
                out.write(buffer, 0, bytesRead);
                totalSent += bytesRead;
                System.out.println("sent " + totalSent);
            }
        }

        sock.close();
        servsock.close();
        fileInputStream.close();

Client Side Code:

Socket sock = new Socket("172.16.27.106", 50000);
        InputStream in = sock.getInputStream();
        Scanner sc = new Scanner(System.in);
        System.out.print("File path: ");
        String output = sc.nextLine();

        sc.close();

        File fileName = new File(output);


        fileOutputStream = new FileOutputStream(output);
        // sending the actual file

        byte[] buffer = new byte[64 * 1024];
        bytesRead = 0;

        while ((bytesRead = in.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, bytesRead);
            totalRecieved += bytesRead;
        }
        sock.close();
        fileOutputStream.close();

Now, I have another question apart from whats wrong with my file sending and file receiving. I want to send file from one laptop to another without the help of any router or LAN connection. So, I guess I will need an Application Specific Network. What will be the best or secure application specific network? Does Java provide any library for application specific network? By the way, I have no idea regarding this field. It is not my current concern of the project. Just for a heads up actually.

Thank you.

有帮助吗?

解决方案

I have wrote a socket program where the host will receive file and client will send ( as far as I am concerned )

It looks like the server is the one sending the file and the client is receiving.

However, today when I checked it,I saw if I run the Host, nothing happens. But When I run the Client side, server side start prompting and asks me for file to send. and then server starts sending the file to my client pc.

That's exactly how client-server applications work. The server sits there waiting until the client connects. In your application, the server doesn't actually prompt the user for anything. All the prompts are in your client code. Once the client connects, the server goes and pushes its file down the socket to the client.

I don't see anything wrong with this application besides the confusion between what the server and client are actually doing.

As for your second question, I can't help you there. Trying asking it in another question.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top