简体   繁体   English

无法从Java服务器使用Boost C ++客户端读取

[英]Cant read with a boost C++ client from a java server

I am trying to create a simple messaging application, using a C++ client and a Java server. 我正在尝试使用C ++客户端和Java服务器创建一个简单的消息传递应用程序。

It seems I can't get around because the C++ client fails to get information from the Java server, yet I can't track down the problem. 似乎我无法解决问题,因为C ++客户端无法从Java服务器获取信息,但是我无法找到问题所在。

I have tried connecting with a Java client to the Java server and it works well. 我尝试将Java客户端连接到Java服务器,并且效果很好。

I have tried connecting with the C++ Client to a simple C++ echo server I made for this purpose, and everything is going well (it reads the information). 我尝试将C ++客户端连接到为此目的而制作的简单C ++回显服务器,并且一切进展顺利(它读取信息)。

I have tried connecting with the java client to the c++ echo server and it works well. 我尝试将Java客户端连接到C ++回显服务器,并且效果很好。

Keep in mind that the Java server get all the information from the client, and is responding (eg: when I try to Log in, the server gets it, logs me in, and "sends" a http response setting the cookie and displaying a welcome message, but the client never gets it). 请记住,Java服务器从客户端获取所有信息,并且正在响应(例如:当我尝试登录时,服务器获取它,登录我,然后“发送” http响应,以设​​置cookie并显示一个欢迎讯息,但客户永远无法收到。

Here is the java code which send the reply: 这是发送回复的Java代码:

        while ((msg = tokenizer.nextMessage()) != null)
    {
        System.out.println("Received \"" + msg + "\" from client");
        String response = (String)protocol.processMessage(msg);
        System.out.println(response); // used for testing
        if (response != null)
        {

            clientSocket.getOutputStream().write(response.getBytes("UTF-8"));
            //the Out below this line is being initialized on connection, just put it here for you to read
            //Also the out.println(response) doesn't work as well, those are 2 attempts i have made
            //out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"), true);
            //out.println(response);

        }

        if (protocol.isEnd(msg))
        {
            break;
        }

    }

Here is the client side code (C++): 这是客户端代码(C ++):

        //while (_socket.available() == 0){
    //  boost::this_thread::sleep(boost::posix_time::milliseconds(10));
    //  std::cout << "test";
    //}
    char reply[max_length];
    size_t reply_length = boost::asio::read(_socket,boost::asio::buffer(reply,10));
    //size_t reply_length = boost::asio::read(_socket, boost::asio::buffer(reply, _socket.available()));
    std::cout << "Reply is:\n";
    std::cout.write(reply, reply_length);
    std::cout << "\n";

Note that the while above in the start of the code is used in order to wait for the response after each sent message, I have tried replacing it with a longer sleep time so I wont have to check the size of the incoming buffer, as you can see just after it I am trying to read a buffer the size of 10, just from testing, and i put the "real" read line in a comment just after it. 请注意,上面代码中的while用来等待每条发送的消息后的响应,我尝试将其替换为更长的睡眠时间,这样就不必检查传入缓冲区的大小,因为可以在测试之后立即看到,我正尝试从测试中读取大小为10的缓冲区,然后将“真实”读取行放在它后面的注释中。

Any help would be appreciated. 任何帮助,将不胜感激。

Thank you. 谢谢。

EDIT - Forgot to mention that if I close the socket after sending the information passes, but doing so fails the purpose, as I am trying to keep the socket open until the client performs a log out. 编辑-忘了提一下,如果我在发送信息传递后关闭了套接字,但是这样做没有达到目的,因为我试图保持套接字打开,直到客户端执行注销。

EDIT #2- I have tried a diffrent method of reading, by using a delimiter char and reaing the buffer 1 char at the time, it just get stuck blocking with the empty buffer. 编辑#2-我尝试了一种不同的读取方法,通过使用定界符char并在此时获得1个char缓冲区,它只是被空缓冲区卡住了。

Here is the code for the second type of reading which i have tried: 这是我尝试的第二种阅读类型的代码:

std::string respone = "";
    char ch='0';
    boost::system::error_code error;
    try {
        while(!error && ch!='$'){
            size_t tmp=0;

            try {
                std::cout << "going to read:";
                tmp = _socket.read_some(boost::asio::buffer(&ch + tmp, 1 - tmp), error);
                std::cout << "finish reading 1 char";

                if (error)
                    throw boost::system::system_error(error);
            }
            catch (std::exception& e) {
                std::cerr << "recv failed (Error: " << e.what() << ')' << std::endl;

            }
            respone.append(1, ch);
        } 
    }

有点愚蠢,但显然我的防病毒软件阻止了来自Java服务器的所有数据包(出于某种原因而不是c ++),将其关闭可以解决所有问题。

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

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