gpt4 book ai didi

java - 套接字写入错误

转载 作者:行者123 更新时间:2023-12-01 14:04:44 26 4
gpt4 key购买 nike

package WebServer;
import java.io.*;
import java.util.*;
import java.net.*;

final public class WebServer {
ServerSocket server;
public void start()throws Exception{
server=new ServerSocket(5000);
Socket client;
while(true){
client=server.accept();
Thread t1=new Thread(new Handlehttp(client));
t1.start();
}
}
public static void main(String[] args) {
try{
new WebServer().start();
}catch(Exception e){
e.printStackTrace();
}
}
}
final class Handlehttp implements Runnable{
Socket s;
DataOutputStream ds;
BufferedReader br;
public Handlehttp(Socket s) throws Exception {
this.s=s;
ds=new DataOutputStream(s.getOutputStream());
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
private synchronized void httprequest()throws Exception{
String rqln,var;
rqln=br.readLine();
System.out.println("Port number : "+s.getPort());
if(rqln!=null)
System.out.println(rqln);
while((var=br.readLine())!=null){
System.out.println(var);
}
}
private synchronized void httpreponse()throws Exception{
String var,fileName;
fileName="D:/file.htm";
FileInputStream fis=new FileInputStream(fileName);
int var1;
var="HTTP/1.0 200 OK\r\n";
var=var+"Connection: keep-alive\r\n";
var=var+"Server : First\r\n";
var=var+"Content-Type: text/html\r\n";
var=var+"\r\n";
ds.writeBytes(var);
while((var1=fis.read())!=-1){
ds.write(var1);
}
}

@Override
public void run() {
try{
httprequest();
httpreponse();
//s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

我在 ds.writeBytes 处收到套接字软件错误,没有发生其他异常,我猜套接字已关闭。我能够看到来自浏览器的 HTTP 请求,但仍然无法将任何排序信息打印到浏览器

顺便说一句,我确实提出了有关多线程服务器的先前问题,但由于先前的问题已解决(可能已解决),因此已开始针对套接字错误提出新问题

我得到的异常是

Port number : 10225
GET /file.htm HTTP/1.1
Host: localhost:5000
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
DNT: 1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

Port number : 10226

java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:132)
at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
at WebServer.Handlehttp.httpreponse(WebServer.java:57)
at WebServer.Handlehttp.run(WebServer.java:82)
at java.lang.Thread.run(Thread.java:722)

最佳答案

当对等方关闭连接时,readLine() 返回 null。因此,之后您无法对其进行写入(除非在关闭的情况下,这在 HTTP 中不会发生)。如果您正在实现 HTTP,则需要阅读 HTTP RFC 以了解如何正确实现。猜测还不够好。请注意,您收到的请求是 HTTP 1.1,并且它包含 Connection: keep-alive header ,并且没有 Content-Length header ,这意味着 header 后面没有可读取的请求正文。

关于java - 套接字写入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18999472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com