Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中如何通过套接字发送EOF_Java_Html_Sockets - Fatal编程技术网

java中如何通过套接字发送EOF

java中如何通过套接字发送EOF,java,html,sockets,Java,Html,Sockets,基本上是一个从服务器请求图像的html文件,我已经创建了一个服务器来做响应工作,但是当html请求图像时,服务器会完全发送图像,但是客户端浏览器会继续加载图像,并说“从服务器传输数据”,我的研究遍布互联网,我发现我可以通过关闭连接来完成,但这对我不起作用 以下是我用java构建的服务器的代码: package lesson1; import java.io.*; import java.net.*; import java.lang.Exception; public class SecCl

基本上是一个从服务器请求图像的html文件,我已经创建了一个服务器来做响应工作,但是当html请求图像时,服务器会完全发送图像,但是客户端浏览器会继续加载图像,并说“从服务器传输数据”,我的研究遍布互联网,我发现我可以通过关闭连接来完成,但这对我不起作用

以下是我用java构建的服务器的代码:

package lesson1;

import java.io.*;
import java.net.*;
import java.lang.Exception;

public class SecClass{

BufferedReader fd;
String  link = null, HTMLData = null, WEBROOT = "D:/PDT1";
String ptr = null, directory = null,
        GET_HEAD_POST = null, Protocol = null;
ServerSocket SRVSOCK;
Socket SOCK;
DataOutputStream dOut;
DataInputStream dInput;
InputStreamReader IR;


public static void main(String[] args) throws Exception
{
    SecClass SERVER = new SecClass();
    SERVER.run();
}

public void run() throws Exception
{
    SRVSOCK = new ServerSocket(80);
    while(true){
        SOCK = SRVSOCK.accept();
        new Thread(new SocketThread(SOCK)).start();
    }
}

public class SocketThread implements Runnable {

    @SuppressWarnings("unused")
    private Socket socket;

    public SocketThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try{
            dOut = new DataOutputStream(SOCK.getOutputStream());
        }catch(Exception ie){
            System.out.println("Cound'nt create dOut");
        }

        try{
            IR = new InputStreamReader(SOCK.getInputStream());
        }catch(Exception ie){
            System.out.println("Cound'nt create IR");
        }


        BufferedReader BR = new BufferedReader(IR);
        String MESSAGE = null;
        System.out.println("Received Request!");
        try{
            MESSAGE = BR.readLine();

        }catch(Exception ie){
            System.out.println("Cound'nt Receive Message");
        }
        System.out.println(MESSAGE);

        if(MESSAGE != null && MESSAGE.contains("HTTP/")){
            Protocol = MESSAGE.substring(MESSAGE.length()-8);
            System.out.println("Request protocol is " + Protocol);
        } else {
            System.out.println("It's a Not HTTP request");
        }

        if(MESSAGE != null && MESSAGE.contains("GET ")){
            GET_HEAD_POST = "GET ";
            System.out.println("Reqest is GET");
            ptr = MESSAGE.substring(4);
            directory = ptr.substring(0,ptr.length()-9);
            link = ptr.substring(ptr.length() - 10);
            System.out.println(link);

        } else if(MESSAGE != null && MESSAGE.contains("HEAD ")){
            System.out.println("Request is HEAD");
            GET_HEAD_POST = "HEAD ";
            ptr = MESSAGE.substring(5);
            directory = ptr.substring(0,ptr.length()-9);
            link = ptr.substring(ptr.length() - 10);
        } else if(MESSAGE != null && MESSAGE.contains("POST ")){
            System.out.println("Request is POST");

        } else {
            System.out.println("Cound'nt verify request");
            try{
                SRVSOCK.close();
            }catch(Exception ie){
                System.out.println("Could not close SRVSOCK");
            }
            try{
                SOCK.close();
            }catch(Exception ie){
                System.out.println("Could not close SOCK");
            }

        }

        if(MESSAGE == null)
        {
            System.out.println("Unknown request");
            try{
                SOCK.close();
            }catch(Exception ie){
                System.out.println("Could not close SOCK");
            }

        } else {
            if((link.charAt(0)) == '/' && link != null){
                link = WEBROOT + directory + "index.html";
                try{

                    OpenFile();
                    dOut.close();
                    SOCK.close();
                    System.out.println("Came out of Openfile Func");

                }catch(Exception ie){
                    System.out.println("Could not Call for OpenFile Function");
                }
                try{
                    MESSAGE = BR.readLine();
                }catch(Exception ie){
                    System.out.println("Could not Receive message");
                }

                System.out.println(MESSAGE);

            } else {
                System.out.println("User Requested for a file");
                System.out.println(directory);

                link = WEBROOT + directory;
                try{
                    OpenFile();
                    dOut.close();
                    SOCK.close();
                }catch(Exception ie){
                    System.out.println("Could not Call for OpenFile Function");
                }
            }

        }
    }
}
public void OpenFile() throws Exception{
    try (BufferedReader br = new BufferedReader(new FileReader(link))) {
        String line;
        System.out.println("Request Link is: " + link);
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            dOut.writeBytes(line);
        }
        dOut.flush();
        dOut.close();
        SOCK.close();
    } catch(Exception e){
        System.out.println(link);
        dOut.writeBytes("<html><h1>404 File Not Found</h1></html>");
        System.out.println("File Does not Exist");

    }
}
}
packagelesson1;
导入java.io.*;
导入java.net。*;
导入java.lang.Exception;
公务舱{
缓冲读取器;
字符串link=null,HTMLData=null,WEBROOT=“D:/PDT1”;
字符串ptr=null,目录=null,
GET\u HEAD\u POST=null,Protocol=null;
服务器套接字SRVSOCK;
插座;
数据输出流dOut;
数据输入流数据输出;
输入流读卡器;
公共静态void main(字符串[]args)引发异常
{
SecClass服务器=新的SecClass();
SERVER.run();
}
public void run()引发异常
{
SRVSOCK=新服务器套接字(80);
while(true){
SOCK=SRVSOCK.accept();
新线程(新SocketThread(SOCK)).start();
}
}
公共类SocketThread实现可运行{
@抑制警告(“未使用”)
专用插座;
公共套接字线程(套接字){
this.socket=socket;
}
@凌驾
公开募捐{
试一试{
dOut=newdataoutputstream(SOCK.getOutputStream());
}捕获(例外情况即){
System.out.println(“cond'nt create dOut”);
}
试一试{
IR=新的InputStreamReader(SOCK.getInputStream());
}捕获(例外情况即){
System.out.println(“cond'nt create IR”);
}
BufferedReader BR=新的BufferedReader(IR);
字符串消息=null;
System.out.println(“已收到请求!”);
试一试{
MESSAGE=BR.readLine();
}捕获(例外情况即){
System.out.println(“通知接收消息”);
}
System.out.println(消息);
if(MESSAGE!=null&&MESSAGE.contains(“HTTP/”){
协议=MESSAGE.substring(MESSAGE.length()-8);
System.out.println(“请求协议为”+协议);
}否则{
System.out.println(“这不是HTTP请求”);
}
if(MESSAGE!=null&&MESSAGE.contains(“GET”)){
GET\u HEAD\u POST=“GET”;
System.out.println(“请求即获取”);
ptr=消息子串(4);
directory=ptr.substring(0,ptr.length()-9);
link=ptr.substring(ptr.length()-10);
System.out.println(链接);
}else if(MESSAGE!=null&&MESSAGE.contains(“HEAD”)){
System.out.println(“请求为头部”);
GET_HEAD\u POST=“HEAD”;
ptr=消息子串(5);
directory=ptr.substring(0,ptr.length()-9);
link=ptr.substring(ptr.length()-10);
}else if(MESSAGE!=null&&MESSAGE.contains(“POST”)){
System.out.println(“请求为POST”);
}否则{
System.out.println(“通知验证请求”);
试一试{
SRVSOCK.close();
}捕获(例外情况即){
System.out.println(“无法关闭SRVSOCK”);
}
试一试{
SOCK.close();
}捕获(例外情况即){
System.out.println(“无法关闭SOCK”);
}
}
如果(消息==null)
{
System.out.println(“未知请求”);
试一试{
SOCK.close();
}捕获(例外情况即){
System.out.println(“无法关闭SOCK”);
}
}否则{
if((link.charAt(0))='/'&&link!=null){
link=WEBROOT+目录+“index.html”;
试一试{
OpenFile();
dOut.close();
SOCK.close();
System.out.println(“出自Openfile Func”);
}捕获(例外情况即){
System.out.println(“无法调用OpenFile函数”);
}
试一试{
MESSAGE=BR.readLine();
}捕获(例外情况即){
System.out.println(“无法接收消息”);
}
System.out.println(消息);
}否则{
System.out.println(“用户请求文件”);
System.out.println(目录);
link=WEBROOT+目录;
试一试{
OpenFile();
dOut.close();
SOCK.close();
}捕获(例外情况即){
System.out.println(“无法调用OpenFile函数”);
}
}
}
}
}
public void OpenFile()引发异常{
try(BufferedReader br=new BufferedReader(new FileReader(link))){
弦线;
System.out.println(“请求链接为:“+Link”);
而((line=br.readLine())!=null){
系统输出打印项次(行);
双写字节(行);
}
dOut.flush();
dOut.close();
SOCK.close();
}捕获(例外e){
System.out.println(链接);
dOut.writeBytes(“未找到404文件”);
System.out.println(“文件不存在”);
}
}
}
这是我的HTML代码:

<html>
<head>
    <meta charset="utf-8"/>
    <title>hello</title>
</head>
<body>
    <header> 
        <div class="HeaderBox">
            <img src="logo.png" alt="image" class="logoBox"/>
        </div>
    </header>
</body>
</html>

你好

正如您所见,当图像完全传输时,我曾两次尝试关闭服务器上的连接,但在我的浏览器中,它卡在加载中,在底部,它说从127.0.0.1传输数据,首先移动除ServerSocket SRVSOCK之外的所有字段;从SecClass类到SocketThread类。制作插座插座;run()方法中的本地引用。原因是您的代码正在使用多线程。那里
public void run() throws Exception
{
SRVSOCK = new ServerSocket(8088);
while(true){
    Socket SOCK;
    SOCK = SRVSOCK.accept();
    new Thread(new SocketThread(SOCK)).start();
}
} 
public void OpenFile() throws Exception{
    try (FileInputStream br = new FileInputStream (link)) {
        System.out.println("Request Link is: " + link);
        int i;
        while ((i = br.read()) > -1)
            dOut.write(i);

           dOut.flush();
            dOut.close();
            SOCK.close();
        } catch(Exception e){
            System.out.println(link);
            dOut.writeBytes("<html><h1>404 File Not Found</h1></html>");
            System.out.println("File Does not Exist");

        }
}