Boost :: Asio写锁

时间:2015-04-13 11:49:24

标签: c# c++ sockets boost boost-asio

我有一个基于TCP / IP连接的客户端/服务器的简单实现。

客户端通过套接字连接到服务器发送一些数据,然后接收一些数据。 以下是我在c++/Linux boost.asio

上编写的服务器的实现
#include <ctime>
#include <iostream>

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

using boost::asio::ip::tcp;
using namespace std;
std::string make_daytime_string()
{
    using namespace std; // For time_t, time and ctime;
    time_t now = time(0);
    return ctime(&now);
}
std::string make_response()
{
    std::string response;
    response.clear();
    std::cout<< "*****************************************"<< endl;
    response = "HTTP/1.1 200 OK\n" ;
    response += "Transfer-Encoding: chunked\n";
    response += "Date: " + make_daytime_string();
    response += "Content-Type: text/plain;";
    std::cout << response << endl;
    std::cout<<"*****************************************"<<endl;
    return response;

}
void on_read( const boost::system::error_code& error, std::size_t bytes_transferred)
{

}

int main(int argc, char argv[])
{
    try
    {

        if(argc != 3)
        {
            std::cout << "invalid number of arguments" <<  endl;
            std::cout << "<sock.exe> <port> <status code>" << endl;
            return 1;
        }
        boost::asio::io_service io_service;

        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1313));
        std::cout << "listening on port" << endl;

        for (;;)
        {

            tcp::socket socket(io_service);
            acceptor.accept(socket);


            std::string message;

            boost::system::error_code ignored_error;

            boost::asio::streambuf response;
            boost::array<char, 5024> buffer;
            buffer.assign(0);// clearing array

            std::ostringstream ss;

            boost::asio::read_until(socket, response, "\n");

            std::string s( (std::istreambuf_iterator<char>(&response)), std::istreambuf_iterator<char>() );

            std::cout << "\n\nPeer IP: " << socket.remote_endpoint().address().to_string() << std::endl;
            std::cout << "---------------------------------------------" << endl;

            std::cout  << s <<endl;
            std::cout << "---------------------------------------------" << endl;
            message.clear();
            cout << "writing response" << endl;
            message = make_response();

            boost::asio::write(socket, boost::asio::buffer(message, sizeof(message)));
            cout << "ended writing"<< endl;

        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

使用.Net和C#

实现im连接的客户端如下
 private static void sendWebRequest()
        {

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://192.168.17.85:1313/RestBaby");
            req.Method = "POST";
            ServicePointManager.Expect100Continue = false;
            ServicePointManager.MaxServicePointIdleTime = 2000;
            req.KeepAlive = true;
            string postData = "This is a test that posts this string to a Web server.";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            req.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            req.ContentLength = byteArray.Length;

            // Get the request stream.
            Stream dataStream = req.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            WebResponse rep = req.GetResponse();
        }

我面临的问题是客户端发送的数据很容易在服务器上解析,并且在调用write函数后cout,但在客户端发生异常

  

基础连接已关闭:连接已关闭   出乎意料。

0 个答案:

没有答案