简体   繁体   English

套接字编程-从Java Servlet到服务器C的write()固定字节数

[英]Socket programming - write() fixed number of bytes from Java Servlet to server C

need some help reading the exact bytes from my java client side to C server. 需要一些帮助,将确切的字节从我的Java客户端读取到C服务器。 STREAM. 流。 I would like to read, lets say, the first two bytes then know which (string/Total No of bytes) are being sent thus use my recv_exactly() function which will take in the actual number of bytes as an argument. 我想阅读,可以说前两个字节,然后知道正在发送哪个(字符串/总字节数),因此使用我的recv_exactly()函数,该函数将实际的字节数作为参数。 This is so that I can limit the wait time instead of reading all 1024 expected buffer size. 这样一来,我可以限制等待时间,而不是读取所有1024个预期的缓冲区大小。 AND also, any ideas how i can send a struct from the java side to make this neat. 而且,任何想法,我如何可以从Java端发送一个结构,使这一整洁。 Thanks a bunch! 谢谢一群!

          //***************  Java Client *****************

        protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException{
        sslsocket.startHandshake();
    kmipoutstream = sslsocket.getOutputStream();
    OutputStreamWriter outputstreamwriter = new OutputStreamWriter(kmipoutstream);

    // figure out what we want to ask for
    final String path = request.getPathInfo();

           System.out.println("request pathInfo: " + path);


    if (path == null || path.endsWith("/users")) {

           //***********SHOULD I SEND 13 FIRST = 2 BYTES?? ***************

                    outputstreamwriter.write("13")
        outputstreamwriter.write("GET ALL USERS");
    } else if (path.endsWith("/keys")) {
        outputstreamwriter.write("GET ALL KEYS");
    } else if (path.endsWith("/templates")) {
        outputstreamwriter.write("GET ALL TEMPLATES");}
    outputstreamwriter.flush();
    BufferedReader wireBufReader = new BufferedReader(
        new InputStreamReader(sslsocket.getInputStream()));

    String tmp = wireBufReader.readLine();

    System.out.println(tmp);
    int numrows = Integer.parseInt(tmp);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Cache-control", "max-age=0");
    response.setHeader("Content-Range", "0-" + numrows + "/" + numrows);

    while (!(tmp = wireBufReader.readLine()).isEmpty()) {
    response.getOutputStream().println(tmp);}
    kmipoutstream.close();
         }

           //**********  Server.C  *******************

           inBuf = calloc(1, 1024);
          if (inBuf == NULL){
      debug_print("ERROR: Memory allocation for inbuf.\n", NULL);
      endProcessing = 1;}
          printf("This is inBuf= %s \n", *inBuf);
             while (!endProcessing){
             sts = RS_SUCCESS;
             do{
            //do accept, followed by negotiate
            sts = rs_ssl_negotiate_viaAccept(rs_ssl_env, IOMODE_NONBLOCKING,
         listenerSocket, &rs_ssl_conn, &ssllog);
          printf("After negotiate and accept sts = %d\n", sts);

          if (RS_SUCCESS != sts){
    debug_print("ERROR: Error during accept and negotiate: %d\n", sts);
    rslog_print(ssllog);
    break;   
  /*
   * receive the get request, parse it out, and call the db method.
   */
  //memset(inBuf, 0, 1024);

   //*******I NEED HELP HERE. NOT SURE HOW TO KNOW THE EXACT NUMBER OF 
                     BYTES BEING SENT BEFORE I CALL
                                     RS_SSL_RECV_EXACTLY ********************

     if( = rs_ssl_recv_exactly(rs_ssl_conn, inBuf, 2, &ssllog)){
      debug_print("ERROR: During HTTP receive: %d\n", sts);
          rslog_print(ssllog);
          break; }
   else
   { if (sts = db_get_userlist_json(jInfo->db_ctx, &jsonBuf, &numrows))
              {debug_print("error getting json user: %d\n", sts);
                break;}}
  __atoe_l(inBuf, actualBytes);
 debug_print("successfully received %d bytes of request:\n<%s>\n", bytesRecvd, inBuf);

   protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException{
         sslsocket.startHandshake();
   kmipoutstream = sslsocket.getOutputStream();
   OutputStreamWriter outputstreamwriter = new OutputStreamWriter(kmipoutstream);

   // figure out what we want to ask for
   final String path = request.getPathInfo();

   System.out.println("request pathInfo: " + path);

   if (path == null || path.endsWith("/users")) {

        // ******** SHOULD I SEND 13 FIRST = 2 BYTES??  ***********

      outputstreamwriter.write("13");
      outputstreamwriter.write("GET ALL USERS");
   } else if (path.endsWith("/keys")) {
      outputstreamwriter.write("GET ALL KEYS");
   } else if (path.endsWith("/templates")) {
      outputstreamwriter.write("GET ALL TEMPLATES");
   }
    outputstreamwriter.flush();
    BufferedReader wireBufReader = new BufferedReader(
         new InputStreamReader(sslsocket.getInputStream()));

    String tmp = wireBufReader.readLine();

    System.out.println(tmp);
    int numrows = Integer.parseInt(tmp);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Cache-control", "max-age=0");
    response.setHeader("Content-Range", "0-" + numrows + "/" + numrows);

    while (!(tmp = wireBufReader.readLine()).isEmpty()) {
        response.getOutputStream().println(tmp);
    }
    kmipoutstream.close();
}

This is so that I can limit the wait time instead of reading all 1024 expected buffer size. 这样一来,我可以限制等待时间,而不是读取所有1024个预期的缓冲区大小。

But recv() doesn't behave like that. 但是recv()的行为并非如此。 It blocks until at least one byte is available (in blocking mode), then transfers whatever is available, up to the specified length. 它会阻塞,直到至少有一个字节可用(在阻塞模式下),然后再传输所有可用字节,直到指定的长度。 It makes no attempt to fill the buffer, unless that much data happens to be available. 除非有大量数据可用,否则它不会尝试填充缓冲区。

So your problem doesn't exist in the first place. 因此,您的问题根本就不存在。

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

相关问题 如何将固定数量的字节从字节缓冲区写入Java中的文件 - how to write a fixed number of bytes from a bytebuffer into the file in java 通过套接字发送字节(Java Servlet到C) - Sending bytes over socket (Java Servlet to C) Java套接字编程阻止对服务器和客户端的读写 - java socket programming blocking on read and write to and from server&client 当帧数和每帧长度不固定时,如何在套接字编程中从服务器获取所有电报? - How to get all the telegram from server in socket programming when the number of frames and the length of each frame is not fixed? C服务器和Java客户端之间的套接字编程 - Socket Programming between C server and Java client Java套接字从C#读取字节 - Java socket read bytes from C# Java套接字编程推servlet-将Servlet部署到服务器后未初始化 - Java Socket programming push servlet - Servlet is not getting initialised after deploying it to the server 如何将字节写入服务器套接字 - how to write bytes to server socket 如何使用来自服务器的随机数在客户端 JAVA SOCKET PROGRAMMING 上随机显示图像数组中的图像 - How to use random number from server to randomly display image from an array of images on client JAVA SOCKET PROGRAMMING 如何在JAVA循环中从文件中读取固定数量的字节? - How to Read fixed number of bytes from a file in a loop in JAVA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM