简体   繁体   中英

Transferring binary data using sockets in Java

I am trying to create a Client-Server model of file transferring in Java in which the client will read a file and send the data to the server and the server will receive the data and write it to a file. I had already seen this post, but it deals with C.

I wrote a simple algorithm which would just send the file as soon it connects to the server.
This is the code for the client:

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

public class Client {
    public static void main(String args[]) {
        try {
            Socket s = new Socket(args[0], Integer.parseInt(args[1]));
            System.out.println("Connected to " + s.getRemoteSocketAddress());
            DataInputStream din = new DataInputStream(s.getInputStream());
            DataOutputStream dout = new DataOutputStream(s.getOutputStream());
            File file = new File(args[2]);
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int no_of_bytes = 0;
            while ((no_of_bytes = fis.read(buffer)) != -1) {
                dout.write(buffer, 0, no_of_bytes);
            }
        } catch (Exception e) {
            System.err.println(e.getLocalizedMessage());
        }
    }
}

This is the code for the server:

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

public class Server {
    public static void main(String args[]) {
        try {
            ServerSocket ss = new ServerSocket(Integer.parseInt(args[0]));
            Socket s = ss.accept();
            System.out.println("Connected to " + s.getRemoteSocketAddress());
            DataInputStream din = new DataInputStream(s.getInputStream());
            DataOutputStream dout = new DataOutputStream(s.getOutputStream());
            File new_file = new File("/home/Puspam/Videos/received.png");
            new_file.createNewFile();
            BufferedWriter bw = new BufferedWriter(new FileWriter(new_file));
            int a;
            while ((a = din.read()) != -1) {
                bw.write((char) a);
            }
            din.close();
            bw.flush();
        } catch (Exception e) {
            System.err.println(e.getLocalizedMessage());
        }
    }
}

After running the two programs with an image file for the experiment, I could find that the file has not been transferred properly. When I try to open the received file, the image viewer software in my PC shows an error that it is not a valid image file. Also, I could see that the received file is a bit larger than the original one.
What mistake am I doing here?

I've had this kinda thing before. Try to use the same type of Input/Output Stream on both ends. Like, BufferedReader to BufferedWriter, etc. You're using a FileInputStream in conjunction with a BufferedWriter. Also, you may need to flush the BufferedWriter in every iteration of your for loop.

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