Using pure and raw socket, this issue deals with 4 points:
- file reading
- socket writing
- open server socket
- reading from a server socket
NOTE: for simplicity and brevity I am not using buffered streams; do not do this in production code without knowing very well that you wish not to use the buffered streams, because they usually help a lot in the performance of reading and writing operations.
File reading
To begin with, we need a path. I’ll call it path
, assume that it is a variable that will be correctly filled. To open the file for reading, we use the FileInputStream
. To read the first 255 bytes of a file, we can do so:
InputStream in = new FileInputStream(path);
byte[] buffer = new byte[255];
int readBytes = in.read(buffer);
in.close();
Not to forget to close the appeal, we can use the Autocloseable and for a try-with-recources
:
try (InputStream in = new FileInputStream(path)) {
byte[] buffer = new byte[255];
int readBytes = in.read(buffer);
}
Written in socket
To write in the socket, we need to open a socket and pick up your OutpuStream
. To open the socket, take the stream and write "any string", we can do so (documentation:
try (
Socket bareSocket = new Socket(hostName, portNumber);
OutputStream out = bareSocket.getOutputStream();
) {
out.write("uma string qualquer".getBytes());
}
Note that it is always necessary to close everything, so I put everything in a block try-with-resources
.
So with the OutputStream
open, we need to write everything that is read from the InputStream
from the archive to the OutputStream
socket. Apache people have already solved this problem for us with the IOUtils.copy
. So the writing in the socket with the contents of the file would look like this:
try (
Socket bareSocket = new Socket(hostName, portNumber);
OutputStream out = bareSocket.getOutputStream();
InputStream in = new FileInputStream(path)
) {
IOUtils.copy(in, out);
}
Opening of server socket
Oracle itself provides an example code using ServerSocket
: https://docs.oracle.com/javase/tutorial/networking/sockets/examples/EchoServer.java
There are no secrets here, just create an object ServerSocket
using a port and, when it receives a connection call, pick up the socket generated with accept()
. Something like that, for a portNumber
informed:
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
InputStream in = clientSocket.getInputStream();
) {
// abri a conexão, agora faça coisas...
}
Reading from a server socket
As we already have the InputStream
, we can use it normally as another InputStream
any. For example, if we want to write the image to the file in the path pathDestino
, I’ll tell you what:
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
InputStream in = clientSocket.getInputStream();
OutputStream out = new FileOutputStream(pathDestino, false);
) {
IOUtils.copy(in, out);
}
Need to be with socket? or use a web server to receive the file?
– Antonio
Socket, unless q is something very difficult/complicated, so an alternative medium can be used.
– Lucas Moraes de Souza
I will use this documentation to answer: https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html; of course everything is easier to handle if you use run on Servlets, make HTTP requests for sending data, but I will focus here on pure socket
– Jefferson Quesado