代码哪里错了package com.birdboot.core; import java.io.IOException; import java.io.InputStream; import java.net.Socket; public class ClientHandler implements Runnable{ private Socket socket; public ClientHandler(Socket socket) { this.socket = socket; } public void run() { try { String line; InputStream in = socket.getInputStream(); StringBuilder builder = new StringBuilder(); int d; char cur='a',pre='a'; while ((d=in.read())!=-1){ cur=(char) d; if (pre==13&&cur==10){ break; } builder.append(cur); pre=cur; } line=builder.toString().trim(); System.out.println(line); String[] data = line.split("\s"); String method = data[0]; String uri = data[1]; String protocol = data[2]; System.out.println("method:"+method); System.out.println("uri:"+uri); System.out.println("protocol:"+protocol); while (true) { while ((d = in.read()) != -1) { cur = (char) d; if (pre == 13 && cur == 10) { break; } builder.append(cur); pre = cur; } line = builder.toString().trim(); if (line.isEmpty()){ break; } System.out.println(line); String[] buf = line.split(":"); System.out.print("消息头:" + buf[0]); System.out.println(":" + buf[1]); } } catch (IOException e) { e.printStackTrace(); } } }
时间: 2023-11-22 09:54:38 浏览: 75
在这段代码中,有几个可能会出错的地方。
1. 在第14行,如果读取的数据是13(回车)和10(换行)的组合,就会退出while循环,但是没有判断读取的字符是否为13和10,而是直接把读取的字符赋值给了cur,所以在后面的比较中可能会出错。应该改为:
```
if (d == 13) {
continue;
} else if (d == 10) {
break;
}
cur = (char) d;
```
2. 在第18行,应该将读取到的数据转换成字符串再去除空格,而不是直接去除空格。应该改为:
```
line = new String(builder.toString()).trim();
```
3. 在第21行,应该判断data数组的长度是否大于等于3,否则会出现数组越界异常。应该改为:
```
if (data.length >= 3) {
String method = data[0];
String uri = data[1];
String protocol = data[2];
System.out.println("method:" + method);
System.out.println("uri:" + uri);
System.out.println("protocol:" + protocol);
}
```
4. 在第27行,应该先判断builder是否为空,否则会出现空指针异常。应该改为:
```
if (!builder.toString().isEmpty()) {
String[] buf = builder.toString().split(":");
System.out.print("消息头:" + buf[0]);
System.out.println(":" + buf[1]);
}
```
希望这些修改能够解决你的问题。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)