小白提问:如何使用Java编写Http服务器,Nodejs编写Http客户端,进行通讯?
发布于 8 年前 作者 qq332769043 4655 次浏览 来自 问答

今天一直在尝试这个,但是没有办法接收到回应。 js客户端代码: var http = require(‘http’); var options = { hostname:‘10.10.4.143’, port:8888, path:’/’, method:‘POST’ }

var req = http.request(options); req.write(‘发给你了’); console.log(‘发给你了!’); req.end(); console.log(‘gg’);

java服务器端代码:

import java.io.; import java.net.ServerSocket; import java.net.Socket;

public class HTTPServer { public static void main(String[] args) { try { ServerSocket ss=new ServerSocket(8888); System.out.println(“在8888口监听”); Socket socket = null; while(true){ socket=ss.accept(); OutputStream out=new BufferedOutputStream(socket.getOutputStream()); InputStream in=new BufferedInputStream(socket.getInputStream());

StringBuffer request=new StringBuffer(); while (true) { int c=in.read(); if (c==-1) { break; } request.append((char)c); } System.out.println(request); //发送回执 PrintWriter pw=new PrintWriter(out);**

pw.println(“访问成功”); pw.flush(); socket.close(); } } catch (IOException e) { e.printStackTrace(); } } }

然后服务器端接收不到客户端数据,好像有一次接收到是在我已经强行停止客户端的进程时,而且接收到的编码格式是chunk中文显示乱码,而且客户端也接收不到服务器通讯内容- -!!尴尬。。。 感谢大家

2 回复

request是封装的http 服务端用原生socket就要按http的数据格式来接受,先找\r\r\r\n 然后再 按Content-Length 指定的长度读数据

已经解决附上java服务器端代码:

import java.io.*; import java.net.ServerSocket; import java.net.Socket;

public class HTTPServer { public static void main(String[] args) { try { ServerSocket ss=new ServerSocket(8888); System.out.println(“在8888口监听”); Socket socket = null; while(true){ socket=ss.accept(); BufferedReader bd=new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String requestHeader;
            int contentLength=0;
            while((requestHeader=bd.readLine())!=null&&!requestHeader.isEmpty()){
                System.out.println(requestHeader);
                /**
                 * 获得GET参数
                 */
                if(requestHeader.startsWith("GET")){
                    int begin = requestHeader.indexOf("/?")+2;
                    int end = requestHeader.indexOf("HTTP/");
                    String condition=requestHeader.substring(begin, end);
                    System.out.println("GET参数是:"+condition);
                }
                /**
                 * 获得POST参数
                 * 1.获取请求内容长度
                 */
                if(requestHeader.startsWith("Content-Length")){
                    int begin=requestHeader.indexOf("Content-Lengh:")+"Content-Length:".length()+1;
                    String postParamterLength=requestHeader.substring(begin).trim();
                    contentLength=Integer.parseInt(postParamterLength);
                    System.out.println("POST参数长度是:"+Integer.parseInt(postParamterLength));
                }
            }
            StringBuffer sb=new StringBuffer();
            if(contentLength>0){
                for (int i = 0; i < contentLength; i++) {
                    sb.append((char)bd.read());
                }
                System.out.println("POST参数是:"+sb.toString());
            }
            //发送回执
            PrintWriter pw=new PrintWriter(socket.getOutputStream());

            pw.println("HTTP/1.1 200 OK");
            pw.println("Content-type:text/html");
            pw.println();
            pw.println("<h1>访问成功!</h1>");

            pw.flush();
            socket.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

回到顶部