Java 网络编程练习题及答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java 网络编程练习题及答案Java 网络编程是指利用Java语言进行网络通信和传输数据的技术。
在日常的软件开发和网络应用中,网络编程是一个非常重要的方向。
本文将提供一些Java网络编程的练习题,并给出相应的答案,以帮助读者更好地掌握这一技术。
一、基础练习题
1. 编写一个简单的Java程序,实现客户端向服务器端发送消息,并在服务器端打印接收到的消息。
```java
// 客户端代码
import java.io.*;
import .*;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
OutputStream outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream, true);
out.println("Hello, Server!");
InputStream inputStream = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String response = in.readLine();
System.out.println("Server: " + response);
out.close();
in.close();
socket.close();
}
}
// 服务器端代码
import java.io.*;
import .*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server is running...");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String request = in.readLine();
System.out.println("Client: " + request);
OutputStream outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream, true);
out.println("Hello, Client!");
out.close();
in.close();
socket.close();
serverSocket.close();
}
}
```
2. 编写一个Java程序,实现多个客户端同时向服务器端发送请求,并在服务器端打印接收到的请求。
```java
// 客户端代码
import java.io.*;
import .*;
public class Client {
public static void main(String[] args) throws IOException { Socket socket = new Socket("localhost", 8888);
OutputStream outputStream = socket.getOutputStream(); PrintWriter out = new PrintWriter(outputStream, true);
out.println("Hello, Server!");
out.close();
socket.close();
}
}
// 服务器端代码
import java.io.*;
import .*;
public class Server {
public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server is running...");
while (true) {
Socket socket = serverSocket.accept();
new Thread(() -> {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String request = in.readLine();
System.out.println("Received request: " + request);
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
}
```
二、进阶练习题
1. 编写一个Java程序,实现基于TCP协议的文件传输。
客户端将本地的一个文件发送给服务器端,服务器端接收后保存到本地磁盘,并通知客户端传输完成。
```java
// 客户端代码
import java.io.*;
import .*;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 8888);
String filePath = "path/to/local/file.txt";
File file = new File(filePath);
byte[] fileBytes = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(fileBytes, 0, fileBytes.length);
OutputStream outputStream = socket.getOutputStream();
outputStream.write(fileBytes, 0, fileBytes.length);
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
socket.close();
}
}
// 服务器端代码
import java.io.*;
import .*;
public class Server {
public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server is running...");
while (true) {
Socket socket = serverSocket.accept();
new Thread(() -> {
try {
String filePath = "path/to/save/file.txt";
File file = new File(filePath);
byte[] fileBytes = new byte[1024];
InputStream inputStream = socket.getInputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
int bytesRead;
while ((bytesRead = inputStream.read(fileBytes)) != -1) {
bufferedOutputStream.write(fileBytes, 0, bytesRead);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
System.out.println("File received and saved: " + filePath);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
}
```
2. 编写一个Java程序,实现基于UDP协议的消息广播。
客户端向服务器端发送消息,服务器端将消息广播给所有连接的客户端。
```java
// 客户端代码
import java.io.*;
import .*;
public class Client {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
String message = "Hello, Server!";
byte[] messageBytes = message.getBytes();
InetAddress serverAddress = InetAddress.getByName("localhost");
int serverPort = 8888;
DatagramPacket packet = new DatagramPacket(messageBytes, messageBytes.length, serverAddress, serverPort);
socket.send(packet);
socket.close();
}
}
// 服务器端代码
import java.io.*;
import .*;
public class Server {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
System.out.println("Server is running...");
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,
buffer.length);
socket.receive(packet);
String request = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received request: " + request);
// 广播消息给所有客户端
InetAddress clientAddress = packet.getAddress();
int clientPort = packet.getPort();
String response = "Message received: " + request;
byte[] responseBytes = response.getBytes();
DatagramPacket responsePacket = new
DatagramPacket(responseBytes, responseBytes.length, clientAddress, clientPort);
socket.send(responsePacket);
}
}
}
```
以上是几个Java网络编程的练习题及相应的答案,通过实践这些练习题,读者可以对Java网络编程有更深入的理解和掌握。
希望本文对读者有所帮助!。