Java服务器客户端连接问题
我已经为此工作了一段时间,但似乎无法让它工作。我有一个服务器,当客户端发送请求时,它会向客户端发送响应。但是,即使服务器接收并处理请求,它似乎也没有发送响应 服务器类Java服务器客户端连接问题,java,server,Java,Server,我已经为此工作了一段时间,但似乎无法让它工作。我有一个服务器,当客户端发送请求时,它会向客户端发送响应。但是,即使服务器接收并处理请求,它似乎也没有发送响应 服务器类 import java.io.*; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import com.sun.net.httpserver.*; public class Server impleme
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();
}
}
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();
}
}
客户端类
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();
}
}
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();
}
}
问题在于,在服务器类中,handleGetItemRequest创建了一个新的outputStream,而不是使用HttpExchange对象提供的outputStream。 请参阅,您可以使用exchange.getResponseBody(),它提供了一个OutputStream,您可以在创建ObjectOutputStream时将其用作参数
ObjectOutputStream o = new ObjectOutputStream(exchange.getResponseBody());