tcp发送xm和接收xm,用java编写
时间: 2024-03-14 13:47:36 浏览: 26
好的,下面是一个基于Java的TCP连接发送和接收XML数据的示例代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
public class TCPClient {
public static void main(String[] args) {
try {
// 创建socket连接
Socket socket = new Socket("localhost", 8888);
// 获取输出流
Writer out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
// 发送XML数据
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><body>Hello World!</body></message>\r\n";
out.write(xml);
out.flush();
// 获取输入流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 接收XML数据
StringBuilder sb = new StringBuilder();
String line = in.readLine();
while (line != null) {
sb.append(line);
if (line.endsWith("</message>")) {
break;
}
line = in.readLine();
}
String response = sb.toString();
// 处理响应数据
System.out.println("Server response: " + response);
// 关闭连接
out.close();
in.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们创建了一个TCP连接到localhost的8888端口。然后我们获取了输出流,发送了一个XML数据,注意我们在XML数据的末尾加上了\r\n。接着我们获取了输入流,循环读取服务器的响应数据,直到读取到以</message>结尾的数据为止。然后我们处理响应数据并关闭连接。
接下来是服务器端的代码示例:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) {
try {
// 创建ServerSocket对象并绑定8888端口
ServerSocket serverSocket = new ServerSocket(8888);
while (true) {
// 监听客户端的连接请求并接受连接
Socket socket = serverSocket.accept();
// 获取输入流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 接收XML数据
StringBuilder sb = new StringBuilder();
String line = in.readLine();
while (line != null) {
sb.append(line);
if (line.endsWith("</message>")) {
break;
}
line = in.readLine();
}
String request = sb.toString();
// 处理XML数据
System.out.println("Client request: " + request);
// 发送响应数据
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><body>Hello World!</body></message>\r\n";
Writer out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
out.write(xml);
out.flush();
// 关闭连接
out.close();
in.close();
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
在上面的示例中,我们创建了一个ServerSocket对象并绑定8888端口,然后在while循环中不断监听客户端的连接请求并接受连接。接着我们获取了输入流,循环读取客户端发送的XML数据,直到读取到以</message>结尾的数据为止。然后我们处理XML数据并发送响应数据"<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><body>Hello World!</body></message>\r\n",最后关闭连接。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)