telnet
既测试了端口号也测试了IP是否通畅
//80是开放的端口号
telnet 111.206.208.135 80
//退出
ctrl+]
quit
//查看本机使用的端口号
telnet -ano
Socket
//客户端
public static void main(String[] args) {
// 创建一个Socket对象,指定服务器的IP地址和端口号
try {
Socket socket = new Socket("localhost", 8080);
OutputStream os = null;
InputStream is = null;
while (true) {
// 输出流发送信息
os = socket.getOutputStream();
Scanner sc = new Scanner(System.in);
System.out.println("输入你要发送的信息 (输入 'exit' 退出):");
String message = sc.nextLine();
if ("exit".equalsIgnoreCase(message)) {
break;
}
os.write(message.getBytes());
os.flush();
// 接受服务端数据
is = socket.getInputStream();
byte[] buffer = new byte[1024];
int len;
StringBuilder response = new StringBuilder();
while ((len = is.read(buffer)) != -1) {
response.append(new String(buffer, 0, len));
if (len < buffer.length) break; // 假设每次发送的数据不超过1024字节
}
System.out.println("从服务器接收的数据: " + response.toString());
}
is.close();
os.close();
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//服务端
public static void main(String[] args){
//创建一个服务端Socket
try {
ServerSocket ss = new ServerSocket(8080);
System.out.println("服务器启动成功!");
//监听客户端连接a
Socket socket = ss.accept();
InputStream is = null;
OutputStream os = null;
FileOutputStream fos = new FileOutputStream("D:\\student.txt");
while (true) {
// 接收客户端消息
is = socket.getInputStream();
byte[] buffer = new byte[1024];
int len;
StringBuilder message = new StringBuilder();
while ((len = is.read(buffer)) != -1) {
message.append(new String(buffer, 0, len));
fos.write(buffer, 0, len);
if (len < buffer.length) break; // 假设每次发送的数据不超过1024字节
}
System.out.println("接收到的消息: " + message.toString());
// 服务端给客户端输出数据
os = socket.getOutputStream();
Scanner sc = new Scanner(System.in);
System.out.println("输入回复信息 (输入 'exit' 退出):");
String msg = sc.nextLine();
if ("exit".equalsIgnoreCase(msg)) {
break;
}
fos.flush();
fos.close();
os.write(msg.getBytes());
os.flush();
}
//关闭资源
os.close();
is.close();
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
XML
第一步:创建解析器SAXReader saxReader = new SAXReader();
第二部:通过解析器获得dom文档Document doc = saxReader.read()
第三步:获得根节点Element root = doc.getRootElement();
第四步:获得所有子节点Iterator iter = root.elementIterator();
第五步:在获得子节点后再次迭代获得孙子节点,后面若还有更小节点以此类推,迭代获得
第六步:使用nextElement.getName()和nextElement.getText()。
//xml文件
<?xml version="1.0" encoding="utf-8" ?>
<students>
<student >
<id class = "shuxing">1234</id>
<name>绽放三</name>
<age>18</age>
</student>
<student>
<id>2234</id>
<name>绽放四</name>
<age>20</age>
</student>
</students>
//测试类获得各节点内容
public static void main(String[] args) {
//使用dom4j解析xml
//创建解析器
SAXReader saxReader = new SAXReader();
//通过解析器把物理文件转换成内存数据,通过解析器将物理文件按读到内存
try {
Document doc = saxReader.read("D:\\ideaProject\\J4061Advanced\\Java05XML\\src\\StudentList.xml");
//通过内存中的数据获得根节点
Element root = doc.getRootElement();
//先获取根节点的迭代器,然后进行迭代里面的节点
Iterator<Element> iter = root.elementIterator();
while (iter.hasNext()) {
//从根节点获取到子节点
Element element = iter.next();
//如果想获取对象元素里的标签内容则需要继续获取元素的标签迭代器
Iterator<Element> iter2 = element.elementIterator();
while (iter2.hasNext()) {
Element e2 = iter2.next();
//获取某个子节点标签里的属性和属性值
String class = e2.attributeValue("class");
System.out.println(Class);
String value = e2.getText();
//获取所有子节点和节点里的值
System.out.println("标签"+e2.getName()+"内容"+e2.getText());
}
}
} catch (DocumentException e) {
throw new RuntimeException(e);
}
}
标签:05telnet,Java,Socket,System,len,new,os,节点,String
From: https://blog.csdn.net/m0_72407563/article/details/142419128