简体   繁体   English

在Boost ASIO服务器和Java客户端之间通过TCP发送双打

[英]sending doubles via TCP between Boost ASIO server and Java client

I am trying to set up a simple Boost ASIO server with a single Java client. 我正在尝试使用单个Java客户端设置一个简单的Boost ASIO服务器。

I am able to send and successfully receive strings between the two. 我能够发送并成功接收两者之间的字符串。 However, when I try to send double values, only garbage comes out on the other end. 但是,当我尝试发送双精度值时,另一端只会出现垃圾。

Below is stand alone code that shows my basic setup (with a C++ Boost ASIO server and a Java client). 下面是显示我的基本设置(带有C ++ Boost ASIO服务器和Java客户端)的独立代码。 When they are run, they do the following four sequential tasks: 运行它们时,它们会执行以下四个顺序的任务:

  1. The client sends a string to the server, which is successfully received and printed out. 客户端向服务器发送一个字符串,该字符串已成功接收并打印出来。
  2. The server sends a string to the client, which is successfully received and printed out. 服务器将字符串发送到客户端,该字符串将被成功接收并打印出来。
  3. The client sends a double value to the server, which is received but does NOT print out correctly. 客户端将双精度值发送到服务器,该值已被接收但不能正确打印。
  4. The server sends a double value to the client, which is received but does NOT print out correctly. 服务器将一个双精度值发送给客户端,该双精度值已被接收但不能正确打印。

Does anyone know what I am doing wrong? 有人知道我在做什么错吗? I am admittedly very new to networking (and Java). 我对网络(和Java)非常陌生。 I have been through the Boost ASIO documentation and examples but to know avail. 我已经看过Boost ASIO文档和示例,但知道还是有用。

C++ Boost ASIO server code: C ++ Boost ASIO服务器代码:

#include <iostream>
#include <string>

#include <boost/asio.hpp>
#include <boost/array.hpp>

using boost::asio::ip::tcp;

int main()
{
   unsigned short const PORT = 19876;

   try
   {
      boost::asio::io_service ioService;
      tcp::acceptor acceptor(ioService, tcp::endpoint(tcp::v4(), PORT));

      while (true)
      {
         // Listen for clients
         std::cout << "Listening for client..." << std::endl;
         tcp::socket socket(ioService);
         acceptor.accept(socket);
         std::cout << "Client heard..." << std::endl;

         size_t len;

         // Receive string from client and print it out
         boost::array<char, 128> cBuf;
         len = socket.read_some(boost::asio::buffer(cBuf, sizeof(cBuf)));
         std::cout.write(cBuf.data(), len);

         // Send string to client
         std::string message = "Server string to send to client\n";
         boost::asio::write(socket, boost::asio::buffer(message));

         // Receive double from client and print it out
         double dVal1 = -1.0;
         char * p_dVal1 = reinterpret_cast<char *>(&dVal1);
         len = socket.read_some(boost::asio::buffer(p_dVal1, sizeof(double)));
         std::cout << dVal1<< std::endl; // prints out garbage

         // Send double to client
         double dVal2 = 6.28;
         char const * p_dVal2 = reinterpret_cast<char const *>(&dVal2);
         boost::asio::write(socket, boost::asio::buffer(p_dVal2, sizeof(double)));
      }
   }
   catch (std::exception & e)
   {
      std::cerr << e.what() << std::endl;
   }

   return 0;
}

Java client code: Java客户端代码:

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

public class Client
{
    final static String HOST = "localhost";
    final static int    PORT = 19876;

    public static void main(String[] args) throws IOException
    {
        Socket socket = new Socket(HOST, PORT);

        // Send string to server
        PrintWriter pos = new PrintWriter(socket.getOutputStream(), true);
        pos.println("Client string to send to server");

        // Receive string from server
        BufferedReader bis =
            new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(bis.readLine());

        // Send double value to server
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeDouble(3.14);

        // Receive double from server
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        System.out.println(dis.readDouble()); // prints out garbage

        socket.close();
        pos.close();
        bis.close();
        dos.close();
        dis.close();
    }
}

Thank you very much in advance!!! 提前非常感谢您!!!

Aaron 亚伦

You need to use a lexical_cast , not a reinterpret_cast . 您需要使用lexical_cast ,而不是reinterpret_cast

With reinterpret_cast , you're telling the compiler to literally interpret the double* bit pattern as a const char* bit pattern. 使用reinterpret_cast ,您要告诉编译器将double*位模式真正地解释为const char*位模式。 Instead, your cast should create a new structure with ascii chars that represent the number in an ascii bit pattern. 相反,您的演员应该使用ascii字符创建一个新结构,以ascii位模式表示数字。

Also, you should ensure that you're managing endianess. 另外,您应确保自己在管理耐力。 You should properly convert to and from network endianess on both the client and server. 您应该在客户端和服务器上都可以正确地与网络端之间进行转换。 See this answer for additional information. 有关其他信息,请参见此答案

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

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