简体   繁体   English

服务器-客户端连接问题Java

[英]Server-client connection issues Java

I have been working on this for a while but can't seem to get it to work. 我已经为此工作了一段时间,但似乎无法使其正常工作。 I have a server that sends a response to the client when they send a request. 我有一台服务器,当客户端发送请求时,该服务器会向客户端发送响应。 However, the server doesn't seem to be sending response even though it receives and processes the request. 但是,即使服务器接收并处理了请求,服务器似乎也没有发送响应。

Server Class 服务器等级

import java.io.*;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import com.sun.net.httpserver.*;

public class Server implements HttpHandler
{

    Server()
    {
    }

    public static void main(String[] args) throws Exception 
    {
        //starts server
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);

        server.createContext("/", new Server());
        server.setExecutor(null); // creates a default executor
        server.start();
    }

    public void handle(HttpExchange exchange) throws IOException 
    {
        //request info
        Headers requestHeaders = exchange.getRequestHeaders();

        boolean getItems = requestHeaders.get("RequestType").get(0).equals("getItems");

        //calls the handler method for getItems
        if(getItems)
        {
            try {
                handleGetItemsRequest(exchange);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //handle a request to get all points
    public void handleGetItemsRequest(HttpExchange exchange) throws IOException, ClassNotFoundException
    {
        System.out.println("Get Items");

        String response = "Here's all of the items";

        ByteArrayOutputStream b = new ByteArrayOutputStream();
        ObjectOutputStream o = new ObjectOutputStream(b);
        o.writeObject(response);
        o.close();
    }

    /*public static byte[] serialize(Object obj) throws IOException {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        ObjectOutputStream o = new ObjectOutputStream(b);
        o.writeObject(obj);
        return b.toByteArray();
    }*/

    public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream b = new ByteArrayInputStream(bytes);
        ObjectInputStream o = new ObjectInputStream(b);
        return o.readObject();
    }

}

Client Class 客户类别

import java.awt.List;
import java.io.*;
import java.net.*;
import java.util.ArrayList;

public class GetItems {

    public static void main(String [] args) throws ClassNotFoundException{
        try {
            URL url = new URL("http://localhost:8000/");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("RequestType", "getItems");
            connection.connect();


            BufferedReader in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String inputLine;

            inputLine = in.readLine();
            System.out.println(inputLine);
            in.close();

        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }

    }

    public static Object deserialize(byte[] string) throws IOException, ClassNotFoundException {
        ByteArrayInputStream b = new ByteArrayInputStream(string);
        ObjectInputStream o = new ObjectInputStream(b);
        return o.readObject();
 }
}

The problem is that in the Server class the handleGetItemRequest creates a new outputStream instead of using which is provided by the HttpExchange object. 问题在于,在Server类中,handleGetItemRequest创建了一个新的outputStream,而不是使用HttpExchange对象提供的outputStream。 See the reference here , you can use exchange.getResponseBody() which provides you an OutputStream and you can use this as parameter when you create the ObjectOutputStream. 请参阅此处的参考 ,可以使用exchange.getResponseBody()为您提供OutputStream,并且在创建ObjectOutputStream时可以将其用作参数。

ObjectOutputStream o = new ObjectOutputStream(exchange.getResponseBody());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM