Home > Java > javaTutorial > body text

How does Java network programming implement data transmission?

WBOY
Release: 2024-04-15 16:48:01
Original
990 people have browsed it

Java network programming data transfer involves the following steps: Processing input and output data using IO streams, specifically InputStream and OutputStream. Establish network connections using sockets, including ServerSocket and Socket. The server uses ServerSocket to listen for connection requests, and the client uses Socket to initiate connection requests. After the connection is established, data is read and written through the socket. In practical applications, such as file transfer, the data read from the file can be sent to the other end through the socket, and then the data is read by the socket and saved to the file.

How does Java network programming implement data transmission?

Java Network Programming: Data Transfer Guide

In Java network programming, data transfer is crucial. Understanding how to use IO streams and sockets for efficient data transfer is critical to developing robust and efficient network applications.

IO stream:

IO stream provides a mechanism for processing input and output data. Java provides a variety of IO streams for reading and writing data:

  • InputStream: Abstract class for reading data from input sources.
  • OutputStream: Abstract class that writes data to the output source.

Example using IO streams:

// 从文件中读取数据
InputStream fileInputStream = new FileInputStream("data.txt");
// 从流中读取数据并存储到字节数组中
byte[] data = new byte[1024];
int bytesRead = fileInputStream.read(data);

// 向文件中写入数据
OutputStream fileOutputStream = new FileOutputStream("output.txt");
// 将字节数组写入流
fileOutputStream.write(data, 0, bytesRead);
Copy after login

Sockets:

Sockets are network programming An abstraction used in establishing connections between different computers. Java uses the java.net package to handle sockets.

  • ServerSocket: Created on the server side to monitor incoming connection requests.
  • Socket: Created on the client side, requesting to establish a connection with the server.

Example using sockets:

Server:

// 创建 ServerSocket 并绑定到端口
ServerSocket serverSocket = new ServerSocket(1234);
// 等待客户端连接
Socket clientSocket = serverSocket.accept();
// 从套接字读取数据
InputStream inputStream = clientSocket.getInputStream();
byte[] data = new byte[1024];
int bytesRead = inputStream.read(data);
Copy after login

Client:

// 创建 Socket 并连接到服务器
Socket clientSocket = new Socket("localhost", 1234);
// 向套接字写入数据
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write("Hello from client!".getBytes());
Copy after login

Practical case:

File transmission end:

// 从文件中读取数据并通过套接字发送
InputStream fileInputStream = new FileInputStream("data.txt");
Socket clientSocket = new Socket("localhost", 1234);
OutputStream outputStream = clientSocket.getOutputStream();
int bytesRead;
byte[] data = new byte[1024];
while ((bytesRead = fileInputStream.read(data)) > 0) {
    outputStream.write(data, 0, bytesRead);
}
Copy after login

File transmission receiving end:

// 从套接字中读取数据并保存到文件中
Socket serverSocket = new ServerSocket(1234).accept();
InputStream inputStream = serverSocket.getInputStream();
OutputStream fileOutputStream = new FileOutputStream("output.txt");
int bytesRead;
byte[] data = new byte[1024];
while ((bytesRead = inputStream.read(data)) > 0) {
    fileOutputStream.write(data, 0, bytesRead);
}
Copy after login

The above is the detailed content of How does Java network programming implement data transmission?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template