Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java sending and receiving file (byte[]) over sockets

I am trying to develop a very simple client / server where the client converts a file to bytes, sends it to the server, and then converts the bytes back in to a file.

Currently the program just creates an empty file. I'm not a fantastic Java developer so any help much appreciated.

This is the server part that receives what the client sends.

ServerSocket serverSocket = null;      serverSocket = new ServerSocket(4444);       Socket socket = null;     socket = serverSocket.accept();      DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));     DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));     byte[] bytes = new byte[1024];      in.read(bytes);     System.out.println(bytes);      FileOutputStream fos = new FileOutputStream("C:\\test2.xml");     fos.write(bytes); 

And here is the client part

Socket socket = null;     DataOutputStream out = null;     DataInputStream in = null;     String host = "127.0.0.1";           socket = new Socket(host, 4444);     out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));     in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));      File file = new File("C:\\test.xml");     //InputStream is = new FileInputStream(file);     // Get the size of the file     long length = file.length();     if (length > Integer.MAX_VALUE) {         System.out.println("File is too large.");     }     byte[] bytes = new byte[(int) length];      //out.write(bytes);     System.out.println(bytes);      out.close();     in.close();     socket.close(); 
like image 238
Rookie Avatar asked Mar 01 '12 17:03

Rookie


People also ask

How will you achieve transfer file between client and server using Java socket?

For a file send from server to client, you start off with a file instance and an array of bytes. You then read the File into the byte array and write the byte array to the OutputStream which corresponds with the InputStream on the client's side.

Do Java sockets use TCP or UDP?

Yes, Socket and ServerSocket use TCP/IP. The package overview for the java.net package is explicit about this, but it's easy to overlook. UDP is handled by the DatagramSocket class.

What mode of communication do Java sockets use?

Two communication protocols that can be used for socket programming are User Datagram Protocol (UDP) and Transfer Control Protocol (TCP). The primary distinction is that UDP is connectionless, so there is no session between the client and the server.

What happens when the close () method of a ServerSocket is called?

The Close method closes the remote host connection and releases all managed and unmanaged resources associated with the Socket. Upon closing, the Connected property is set to false . For connection-oriented protocols, it is recommended that you call Shutdown before calling the Close method.


1 Answers

The correct way to copy a stream in Java is as follows:

int count; byte[] buffer = new byte[8192]; // or 4096, or more while ((count = in.read(buffer)) > 0) {   out.write(buffer, 0, count); } 

Wish I had a dollar for every time I've posted that in a forum.

like image 153
user207421 Avatar answered Sep 19 '22 20:09

user207421