Java将数据从服务器发送回客户端

时间:2015-11-01 18:34:19

标签: java http server client

晚上好,我在这里得到了这台服务器和客户端。

Web服务器

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class WebServer {



public static void main(String[] args) throws Exception {
    HttpServer http = HttpServer.create(new InetSocketAddress(8000), 0);
    http.createContext("/test", new MyHandler());
    http.setExecutor(null); // creates a default executor
    http.start();

    //NimServer nimserver = new NimServer(32778);
    //nimserver.serve();

}




static class MyHandler implements HttpHandler {
    //AtomicInteger atomicInteger = new AtomicInteger(0); 
    //int theValue = atomicInteger.get(); 
    @Override
    public void handle(final HttpExchange t) throws IOException {
        final String response;

        final String requestMethod = t.getRequestMethod();
        if ("GET".equals(requestMethod)) {
           // response = String.format("Besuche: %d%n", atomicInteger.addAndGet(1));
        }
        else if ("POST".equals(requestMethod)) {
           // atomicInteger.set(0);
            int clientno = new DataInputStream(t.getRequestBody()).readInt();
            System.out.println("Send from Client: " + clientno);
            int newclientno = clientno + 1;
            System.out.println("Increased by Server: " + newclientno);
            new DataOutputStream(t.getResponseBody()).writeInt(newclientno);

        }
        else {
            throw new IOException("Unsupported method");
        }

        //t.sendResponseHeaders(200, response.length());
        //final OutputStream os = t.getResponseBody();
        //os.write(newclientno);
        //os.close();

    }
}



}

HttpClient的

import java.net.*;
import java.io.*;

public class HttpClient {

public static int clientno = 0;

public static void main(String[] args) throws Exception {
//NimMessage clientnumber = new NimMessage();
//clientnumber.nachricht = "Client No: " + clientno;

URL test = new URL("http://localhost:8000/test");
HttpURLConnection connect = (HttpURLConnection) test.openConnection();
connect.setDoOutput(true);
connect.setDoInput(true);
connect.setRequestMethod("POST");
new DataOutputStream(connect.getOutputStream ()).writeInt(clientno);//send int out
int newclientno = new DataInputStream(connect.getInputStream()).readInt();
System.out.println("Send from Server: " + newclientno);
//BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
//String inputLine;





//System.out.println(clientnumber.createJsonNachricht().toString());




//while ((inputLine = in.readLine()) != null) 
  //  System.out.println(inputLine);
//in.close();

}


}

我能够将整数clinetno 从客户端发送到服务器并在服务器上增加它。但我无法弄清楚如何将新的整数newclientno 发送回客户端并在控制台上显示它。有什么建议我做错了吗?

1 个答案:

答案 0 :(得分:0)

好的,我发现了我的错误,我不得不在服务器中添加一个标头来完成连接。我已经拥有,但没有注意到它。

t.sendResponseHeaders(200, 0);