Java基于socket编程实现电路交换、报文交换、分组交换
时间: 2023-10-24 13:09:34 浏览: 45
Java基于Socket编程可以实现电路交换、报文交换和分组交换,但需要对网络协议有一定的了解。
在电路交换中,通信双方建立一条独占的物理连接,并在通信过程中一直保持连接状态,直到其中一方主动断开连接。Java基于Socket编程可以实现这种方式的通信,通过Socket和ServerSocket类可以建立TCP连接,从而实现可靠的数据传输。
在报文交换中,通信双方之间通过交换报文进行通信。报文是指一定格式的数据包,包括报文头和报文体。通信双方在交换报文时需要遵循特定的协议。Java基于Socket编程可以实现这种方式的通信,通过Socket类可以建立TCP连接,通过输入输出流可以实现报文的读写。
在分组交换中,通信双方之间的数据被分成一系列的数据块,每个数据块称为分组。每个分组包含了一部分数据和必要的控制信息,例如源地址、目标地址、序号等。通信双方在交换分组时需要依赖网络层的协议,例如IP、UDP等。Java基于Socket编程可以实现这种方式的通信,通过DatagramSocket和DatagramPacket类可以实现UDP协议的数据传输。
相关问题
Java基于socket编程实现电路交换
电路交换是一种数据传输方式,它建立一条稳定的连接通路,并使用这条通路传输数据。在Java中,可以使用socket编程来实现电路交换。
首先,需要创建一个ServerSocket对象来监听客户端的连接请求。代码如下:
```
ServerSocket serverSocket = new ServerSocket(port);
```
其中,port为监听的端口号。
接着,需要在一个无限循环中等待客户端的连接请求,并在连接成功后创建一个Socket对象来与客户端进行通信。代码如下:
```
while (true) {
Socket socket = serverSocket.accept();
// 处理与客户端的通信
}
```
在与客户端进行通信之前,需要创建一个输入流和输出流来进行数据的读写。代码如下:
```
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
```
接下来可以使用inputStream读取客户端发送的数据,或使用outputStream向客户端发送数据。在通信完成后,需要关闭Socket对象。完整代码如下:
```
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
// 处理与客户端的通信
inputStream.close();
outputStream.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
```
需要注意的是,这只是一个简单的示例,实际应用中还需要考虑很多问题,如数据的编码、协议的设计等。
Java基于socket编程实现电路交换、报文交换、分组交换,写出代码
以下是Java基于socket编程实现电路交换、报文交换、分组交换的示例代码:
1. 电路交换
//服务器端代码
import java.io.*;
import java.net.*;
public class CircuitSwitchingServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
//创建服务器端Socket,监听端口号为5000
serverSocket = new ServerSocket(5000);
} catch (IOException e) {
System.err.println("Could not listen on port: 5000.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println("Waiting for connection.....");
try {
//等待客户端连接
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println("Connection successful");
System.out.println("Waiting for input.....");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
//读取客户端发送的消息,并将其原样返回
System.out.println("Client says: " + inputLine);
out.println(inputLine);
if (inputLine.equals("Bye")) {
break;
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
//客户端代码
import java.io.*;
import java.net.*;
public class CircuitSwitchingClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
//创建客户端Socket,指定服务器地址和端口号
echoSocket = new Socket("localhost", 5000);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
//将用户输入的消息发送给服务器端
out.println(userInput);
//从服务器端读取返回的消息并输出
System.out.println("Server says: " + in.readLine());
if (userInput.equals("Bye")) {
break;
}
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
2. 报文交换
//服务器端代码
import java.io.*;
import java.net.*;
public class MessageSwitchingServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
//创建服务器端Socket,监听端口号为5000
serverSocket = new ServerSocket(5000);
} catch (IOException e) {
System.err.println("Could not listen on port: 5000.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println("Waiting for connection.....");
try {
//等待客户端连接
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println("Connection successful");
System.out.println("Waiting for input.....");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
//读取客户端发送的消息,并将其每个字符转换为大写字母后返回
System.out.println("Client says: " + inputLine);
out.println(inputLine.toUpperCase());
if (inputLine.equals("Bye")) {
break;
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
//客户端代码
import java.io.*;
import java.net.*;
public class MessageSwitchingClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
//创建客户端Socket,指定服务器地址和端口号
echoSocket = new Socket("localhost", 5000);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
//将用户输入的消息发送给服务器端
out.println(userInput);
//从服务器端读取返回的消息并输出
System.out.println("Server says: " + in.readLine());
if (userInput.equals("Bye")) {
break;
}
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
3. 分组交换
//服务器端代码
import java.io.*;
import java.net.*;
public class PacketSwitchingServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
//创建服务器端Socket,监听端口号为5000
serverSocket = new ServerSocket(5000);
} catch (IOException e) {
System.err.println("Could not listen on port: 5000.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println("Waiting for connection.....");
try {
//等待客户端连接
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println("Connection successful");
System.out.println("Waiting for input.....");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
//读取客户端发送的消息,并将其每个字符转换为小写字母后返回
System.out.println("Client says: " + inputLine);
out.println(inputLine.toLowerCase());
if (inputLine.equals("Bye")) {
break;
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
//客户端代码
import java.io.*;
import java.net.*;
public class PacketSwitchingClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
//创建客户端Socket,指定服务器地址和端口号
echoSocket = new Socket("localhost", 5000);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
//将用户输入的消息发送给服务器端
out.println(userInput);
//从服务器端读取返回的消息并输出
System.out.println("Server says: " + in.readLine());
if (userInput.equals("Bye")) {
break;
}
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)