0

我正在制作一个简单的 Web 服务器,每次客户端连接时都会在其中生成一个新线程。然后我需要使用 BufferedOutputStream 发送响应(因为 HTTP 协议需要 /r/n 在行尾)。这是我的代码:

class HttpServer{
    public static void main(String args[]){
        ServerSocket ss = null;
        int port = 40000;

        try{
            if(args.length > 0){
                port = Integer.parseInt(args[0]);
            }

            ss = new ServerSocket(port);

            while(true){
                Socket client = ss.accept();
                User user = new User(client);
                user.start();
            }
        }
        catch(Exception e){
                System.err.println(e.getMessage());
        }
    }
}

我的用户类:

class User extends Thread{

        Socket client;

        public User(Socket s){
            client = s;
        }

        public void run(){
            try{
                BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());

                println(bos, "Hello World");
                bos.close();

                client.close();
            }
            catch(Exception e){
                    System.err.println(e.getMessage());
            }
        }

        private void println(BufferedOutputStream bos, String s) throws IOException { 
            String news = s + "\r\n"; 
            byte[] array = news.getBytes(); 
            for(int i=0; i<array.length; i++){
                bos.write(array[i]);
            }
            return;
        }
    }

当我尝试创建 BufferedOutputStream 时出现问题;它给了我一个“套接字已关闭”错误,我不知道为什么。

任何帮助将非常感激。

谢谢!

这是不起作用的代码:

class User extends Thread{

    Socket client;

    public User(Socket s){
        client = s;
    }

    public void run(){
        try{
            System.out.println("Address: " + client.getInetAddress().toString());

            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

            String line;
            Boolean first = true;
            while(!((line = reader.readLine()).equals(""))){
                if(first)
                    System.out.println(line.split(" ")[1]);
                first = false;
            }
            reader.close();

            BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
            println(bos, "Hello World");
            bos.close();



            // Close socket
            client.close();
        }
        catch(Exception e){
                System.err.println(e.getMessage());
        }
    }

    private void println(BufferedOutputStream bos, String s) throws IOException { 
        String news = s + "\r\n"; 
        byte[] array = news.getBytes(); 
        for(int i=0; i<array.length; i++){
            bos.write(array[i]);
        }
        return;
    }
}
4

3 回答 3

3

此异常意味着关闭了 Socket。

关闭套接字的输入流或输出流会关闭另外两个,关闭任何 FilterInput/OutputStream/Reader/Writer 会关闭它正在过滤的流/读取器/写入器。

于 2013-08-04T01:09:52.763 回答
-1

据我所知,您的代码没有问题。操作系统或浏览器似乎更有可能以某种方式限制对您的服务器的访问。

于 2013-08-03T11:54:41.290 回答
-1
Socket client = ss.accept();
User user = new User(client);
user.start();

accept呼叫阻塞等待连接。永远不会创建您的用户线程。您应该创建一个新的 User 类,它会为您创建一个新线程并将您的消息写入服务器。

    public class User extends Thread{

            Socket client;
            int port;
            public User(int p) throws UnknownHostException, IOException{
                port = p;
                client = new Socket(InetAddress.getLocalHost(), 40000);
            }

            public void run(){
                try{
                    BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());

                    println(bos, "Hello World");
                    bos.close();

                    client.close();
                }
                catch(Exception e){
                        System.err.println(e.getMessage());
                }
            }

            private void println(BufferedOutputStream bos, String s) throws IOException { 
                String news = s + "\r\n"; 
                byte[] array = news.getBytes(); 
                for(int i=0; i<array.length; i++){
                    bos.write(array[i]);
                }

                return;
            }

            public static void main(String[] args) throws UnknownHostException, IOException {
                User user = new User(4000);
                user.start();
            }
        }

服务器应该只监听传入的连接。(也许打印它)

import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


class HttpServer{
    public static void main(String args[]){
        ServerSocket ss = null;
        int port = 40000;

        try{
            if(args.length > 0){
                port = Integer.parseInt(args[0]);
            }

            ss = new ServerSocket(port);

            while(true){
                Socket client = ss.accept();
                Scanner scanner = new Scanner(client.getInputStream());
                while (scanner.hasNext()) {
                    System.out.println(scanner.nextLine());
                }
            }
        }
        catch(Exception e){
                System.err.println(e.getMessage());
        }
    }
}

将它们作为不同的 java 进程运行,您应该在服务器端看到 hello world。如果您更改端口,您可能也必须在客户端进行更改。

于 2013-08-03T11:57:40.703 回答