C ++:客户端服务器通信:并非所有元素都使用套接字

时间:2016-08-31 13:38:04

标签: c++ linux sockets server client

我在linux系统上玩一些客户端服务器示例。 其中一个是此网站:http://tldp.org/LDP/LG/issue74/tougher.html

通过这个例子,我将一个字符串流发送到套接字。 问题是,我没有得到双方的串流的所有元素(Cliend的服务器)。有人为什么不知道?

// Definition of the Socket class

#ifndef Socket_class
#define Socket_class


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <string>
#include <arpa/inet.h>


const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int MAXRECV = 500;

class Socket
{
 public:
  Socket();
  virtual ~Socket();

  // Server initialization
  bool create();
  bool bind ( const int port );
  bool listen() const;
  bool accept ( Socket& ) const;

  // Client initialization
  bool connect ( const std::string host, const int port );

  // Data Transimission
  bool send ( const std::string ) const;
  int recv ( std::string& ) const;


  void set_non_blocking ( const bool );

  bool is_valid() const { return m_sock != -1; }

 private:

  int m_sock;
  sockaddr_in m_addr;


};


#endif




// Implementation of the Socket class.


#include "Socket.h"
#include "string.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>



Socket::Socket() :
  m_sock ( -1 )
{

  memset ( &m_addr,
       0,
       sizeof ( m_addr ) );

}

Socket::~Socket()
{
  if ( is_valid() )
    ::close ( m_sock );
}

bool Socket::create()
{
  m_sock = socket ( AF_INET,
            SOCK_STREAM,
            0 );

  if ( ! is_valid() )
    return false;


  // TIME_WAIT - argh
  int on = 1;
  if ( setsockopt ( m_sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 )
    return false;


  return true;

}



bool Socket::bind ( const int port )
{

  if ( ! is_valid() )
    {
      return false;
    }



  m_addr.sin_family = AF_INET;
  m_addr.sin_addr.s_addr = INADDR_ANY;
  m_addr.sin_port = htons ( port );

  int bind_return = ::bind ( m_sock,
                 ( struct sockaddr * ) &m_addr,
                 sizeof ( m_addr ) );


  if ( bind_return == -1 )
    {
      return false;
    }

  return true;
}


bool Socket::listen() const
{
  if ( ! is_valid() )
    {
      return false;
    }

  int listen_return = ::listen ( m_sock, MAXCONNECTIONS );


  if ( listen_return == -1 )
    {
      return false;
    }

  return true;
}


bool Socket::accept ( Socket& new_socket ) const
{
  int addr_length = sizeof ( m_addr );
  new_socket.m_sock = ::accept ( m_sock, ( sockaddr * ) &m_addr, ( socklen_t * ) &addr_length );

  if ( new_socket.m_sock <= 0 )
    return false;
  else
    return true;
}


bool Socket::send ( const std::string s ) const
{
  int status = ::send ( m_sock, s.c_str(), s.size(), MSG_NOSIGNAL );
  if ( status == -1 )
    {
      return false;
    }
  else
    {
      return true;
    }
}


int Socket::recv ( std::string& s ) const
{
  char buf [ MAXRECV + 1 ];

  s = "";

  memset ( buf, 0, MAXRECV + 1 );

  int status = ::recv ( m_sock, buf, MAXRECV, 0 );

  if ( status == -1 )
    {
      std::cout << "status == -1   errno == " << errno << "  in Socket::recv\n";
      return 0;
    }
  else if ( status == 0 )
    {
      return 0;
    }
  else
    {
      s = buf;
      return status;
    }
}



bool Socket::connect ( const std::string host, const int port )
{
  if ( ! is_valid() ) return false;

  m_addr.sin_family = AF_INET;
  m_addr.sin_port = htons ( port );

  int status = inet_pton ( AF_INET, host.c_str(), &m_addr.sin_addr );

  if ( errno == EAFNOSUPPORT ) return false;

  status = ::connect ( m_sock, ( sockaddr * ) &m_addr, sizeof ( m_addr ) );

  if ( status == 0 )
    return true;
  else
    return false;
}

void Socket::set_non_blocking ( const bool b )
{

  int opts;

  opts = fcntl ( m_sock,
         F_GETFL );

  if ( opts < 0 )
    {
      return;
    }

  if ( b )
    opts = ( opts | O_NONBLOCK );
  else
    opts = ( opts & ~O_NONBLOCK );

  fcntl ( m_sock,
      F_SETFL,opts );

}




// Definition of the ServerSocket class

#ifndef ServerSocket_class
#define ServerSocket_class

#include "Socket.h"


class ServerSocket : private Socket
{
 public:

  ServerSocket ( int port );
  ServerSocket (){};
  virtual ~ServerSocket();

  const ServerSocket& operator << ( const std::string& ) const;
  const ServerSocket& operator >> ( std::string& ) const;

  void accept ( ServerSocket& );

};


// Implementation of the ServerSocket class

#include "ServerSocket.h"
#include "SocketException.h"


ServerSocket::ServerSocket ( int port )
{
  if ( ! Socket::create() )
    {
      throw SocketException ( "Could not create server socket." );
    }

  if ( ! Socket::bind ( port ) )
    {
      throw SocketException ( "Could not bind to port." );
    }

  if ( ! Socket::listen() )
    {
      throw SocketException ( "Could not listen to socket." );
    }

}

ServerSocket::~ServerSocket()
{
}


const ServerSocket& ServerSocket::operator << ( const std::string& s ) const
{
  if ( ! Socket::send ( s ) )
    {
      throw SocketException ( "Could not write to socket." );
    }

  return *this;

}


const ServerSocket& ServerSocket::operator >> ( std::string& s ) const
{
  if ( ! Socket::recv ( s ) )
    {
      throw SocketException ( "Could not read from socket." );
    }

  return *this;
}

void ServerSocket::accept ( ServerSocket& sock )
{
  if ( ! Socket::accept ( sock ) )
    {
      throw SocketException ( "Could not accept socket." );
    }
}



    #endif


// Definition of the ClientSocket class

#ifndef ClientSocket_class
#define ClientSocket_class

#include "Socket.h"


class ClientSocket : private Socket
{
 public:

  ClientSocket ( std::string host, int port );
  virtual ~ClientSocket(){};

  const ClientSocket& operator << ( const std::string& ) const;
  const ClientSocket& operator >> ( std::string& ) const;

};


#endif


// Implementation of the ClientSocket class

#include "ClientSocket.h"
#include "SocketException.h"


ClientSocket::ClientSocket ( std::string host, int port )
{
  if ( ! Socket::create() )
    {
      throw SocketException ( "Could not create client socket." );
    }

  if ( ! Socket::connect ( host, port ) )
    {
      throw SocketException ( "Could not bind to port." );
    }

}


const ClientSocket& ClientSocket::operator << ( const std::string& s ) const
{
  if ( ! Socket::send ( s ) )
    {
      throw SocketException ( "Could not write to socket." );
    }

  return *this;

}


const ClientSocket& ClientSocket::operator >> ( std::string& s ) const
{
  if ( ! Socket::recv ( s ) )
    {
      throw SocketException ( "Could not read from socket." );
    }

  return *this;
}


int main()
{
std::stringstream streamIn, streamOut;  // stream for server
    std::stringstream ssInput, ssOut;       // stream for client

    int size = 250;


    for(int i=0; i < size; i++)
    {
        ssInput << i << " ";
    }

    std::cout << "\nssInput.str()" << ssInput.str() << std::endl;

    // Create the socket
    ServerSocket server ( 30000 );

    ClientSocket client_socket ( "localhost", 30000 );

    ServerSocket new_sock;
    server.accept ( new_sock );

    client_socket << ssInput;

    new_sock >> streamIn;

    std::cout << "\nstreamIn.str()" << streamIn.str() << std::endl;


    for(int i=0; i < size; i++)
    {
        streamOut << i << " ";
    }

    std::cout << "\nstreamOut.str()" << streamOut.str() << std::endl;

    new_sock << streamOut;


    client_socket >> ssOut;


    std::cout << "\nssOut.str()" << ssOut.str() << std::endl;

}

这是输出:

ssInput.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

streamIn.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

streamOut.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249

ssOut.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

... THX

4 个答案:

答案 0 :(得分:1)

TCP流以数据包传输。 recv(和read)接收流的一部分,即来自一个或多个数据包的数据。

您在第一次成功recv之后停止,但您应该再次调用它,直到没有更多数据(或直到收到某些字节 - 这取决于您的协议 *真的)。

同样适用于send。它返回发送的字节数,可以theoretically小于您想要发送的数量。您应该检查发送了多少字节,并根据需要再次调用send以发送剩余的字节。

*通过协议我的意思是服务器和客户端之间的通信必须有某种 协议

例如,您可以说&#34;数据应按行传输。一行被定义为一个0或更多字符的序列,后跟一个&#39; \ n&#39;在这种情况下,您的服务器需要反复拨打recv并收集数据,直到遇到“&#39; \ n”字符。

或者您可以说&#34;数据应以逻辑数据包传输。数据包按网络顺序定义为2个字节,指定消息的长度,后跟消息(非空终止)&#34;。然后调用recv,从前2个字节中获取消息长度,并在需要时重复调用recv以收集那么多字节。

您还需要为客户端 - >服务器(请求)和服务器 - >客户端(响应)单独指定。

最后,在关闭连接时指定(客户端或服务器)和

答案 1 :(得分:0)

抱歉这是错误的代码库: 这是正确的:

#include "ServerSocket.h"
#include "SocketException.h"

ServerSocket::ServerSocket(int port)
{
    if (!Socket::create())
    {
        throw SocketException("Could not create server socket.");
    }

    if (!Socket::bind(port))
    {
        throw SocketException("Could not bind to port.");
    }

    if (!Socket::listen())
    {
        throw SocketException("Could not listen to socket.");
    }

}

ServerSocket::~ServerSocket()
{
}

const ServerSocket& ServerSocket::operator <<(const std::string& s) const
{
    if (!Socket::send(s))
    {
        throw SocketException("Could not write to socket.");
    }

    return *this;

}

const ServerSocket& ServerSocket::operator >>(std::string& s) const
{
    if (!Socket::recv(s))
    {
        throw SocketException("Could not read from socket.");
    }

    return *this;
}

void ServerSocket::accept(ServerSocket& sock)
{
    if (!Socket::accept(sock))
    {
        throw SocketException("Could not accept socket.");
    }
}

const ServerSocket& ServerSocket::operator << ( const std::stringstream& ss ) const
{

    if ( ! Socket::write ( ss ) )
    {
      throw SocketException ( "Could not write stream to socket." );
    }

  return *this;

}


const ServerSocket& ServerSocket::operator >> ( std::stringstream& ss ) const
{

    if ( ! Socket::read ( ss ) )
    {
      throw SocketException ( "Could not read stream from socket." );
    }

  return *this;
}




#include "Socket.h"
//#include "string.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <string>
#include <iostream>
#include <sstream>

Socket::Socket() :
        m_sock(-1)
{

    memset(&m_addr, 0, sizeof(m_addr));

}

Socket::~Socket()
{
    if (is_valid())
        ::close(m_sock);
}

bool Socket::create()
{
    m_sock = socket( AF_INET,
    SOCK_STREAM, 0);

    if (!is_valid())
        return false;

    // TIME_WAIT - argh
    int on = 1;
    if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (const char*) &on,
            sizeof(on)) == -1)
        return false;

    return true;

}

bool Socket::bind(const int port)
{

    if (!is_valid())
    {
        return false;
    }

    m_addr.sin_family = AF_INET;
    m_addr.sin_addr.s_addr = INADDR_ANY;
    m_addr.sin_port = htons(port);

    int bind_return = ::bind(m_sock, (struct sockaddr *) &m_addr,
            sizeof(m_addr));

    if (bind_return == -1)
    {
        return false;
    }

    return true;
}

bool Socket::listen() const
{
    if (!is_valid())
    {
        return false;
    }

    int listen_return = ::listen(m_sock, MAXCONNECTIONS);

    if (listen_return == -1)
    {
        return false;
    }

    return true;
}

bool Socket::accept(Socket& new_socket) const
{
    int addr_length = sizeof(m_addr);
    new_socket.m_sock = ::accept(m_sock, (sockaddr *) &m_addr,
            ( socklen_t *) &addr_length);

    if (new_socket.m_sock <= 0)
        return false;
    else
        return true;
}

void Socket::close()
{
    if (is_valid())
        ::close(m_sock);
}

bool Socket::send(const std::string s) const
{
    std::cout << "Socket :: send :: s.size()\t" << s.size() << std::endl;

    int status = ::send(m_sock, s.c_str(), s.size(), MSG_NOSIGNAL);
    if (status == -1)
    {
        std::cout << "Error :: Socket::send" << std::endl;
        return false;
    }
    else
    {
        return true;
    }
}

int Socket::recv(std::string& s) const
{
    char buf[MAXRECV + 1];

    s = "";

    std::memset(buf, 0, MAXRECV + 1);

    int status = ::recv(m_sock, buf, MAXRECV, 0);

    if (status == -1)
    {
        std::cout << "status == -1   errno == " << errno
                << "  in Socket::recv\n";
        return 0;
    }
    else if (status == 0)
    {
        return 0;
    }
    else
    {
        s = buf;
        return status;
    }
}

bool Socket::write(const std::stringstream& ss) const
{
    int status = ::write(m_sock, ss.str().c_str(), sizeof(ss));
    if (status == -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

int Socket::read(std::stringstream& ss) const
{
    char buf[MAXRECV + 1];
    std::string temp;

    std::memset(buf, 0, MAXRECV + 1);

    int status = ::read(m_sock, buf, MAXRECV);

    if (status == -1)
    {
        std::cout << "status == -1   errno == " << errno
                << "  in Socket::read stream\n";
        return 0;
    }
    else if (status == 0)
    {
        return 0;
    }
    else
    {
        temp.assign(buf);
        ss << temp;

        return status;
    }
}

bool Socket::connect(const std::string host, const int port)
{
    if (!is_valid())
        return false;

    m_addr.sin_family = AF_INET;
    m_addr.sin_port = htons(port);

    int status = inet_pton( AF_INET, host.c_str(), &m_addr.sin_addr);

    if ( errno == EAFNOSUPPORT)
        return false;

    status = ::connect(m_sock, (sockaddr *) &m_addr, sizeof(m_addr));

    if (status == 0)
        return true;
    else
        return false;
}

void Socket::set_non_blocking(const bool b)
{

    int opts;

    opts = fcntl(m_sock,
    F_GETFL);

    if (opts < 0)
    {
        return;
    }

    if (b)
        opts = (opts | O_NONBLOCK);
    else
        opts = (opts & ~O_NONBLOCK);

    fcntl(m_sock,
    F_SETFL, opts);

}

答案 2 :(得分:0)

i have modify the write an read method. The write method seems to work. The read method doesnt.

bool Socket::write(const std::stringstream& ss) const
{
    int total_bytes_written = 0;

    while (total_bytes_written != 1024)
    {
                //  int status = ::write(m_sock, ss.str().c_str(), sizeof(ss));
        int bytes_written = ::write(m_sock, ss.str().c_str() + total_bytes_written, 500 - total_bytes_written);

        if (bytes_written == -1)
        {
            return false;
        }
        if(bytes_written == 0)
        {
            std::cout << "Socket :: write :: 2 :: DONE" << std::endl;
            return true;
        }
        std::cout << "Socket :: write :: 1 :: ss.str():\t" <<ss.str() << "\tsizeof(ss)\t"<< sizeof(ss) << "\tss.str().size()\t" << ss.str().size() <<std::endl;
        total_bytes_written += bytes_written;
    }
        return false;

}


int Socket::read(std::stringstream& ss) const
{
    int MAXRECV = 500;
    char buf[MAXRECV + 1];
    std::string temp;

    std::memset(buf, 0, MAXRECV + 1);

    int bytesRead = 0;
    int result;

    std::cout << "Socket :: read :: 1" << std::endl;
    while (bytesRead < MAXRECV)
    {
        std::cout << "Socket :: read :: 2" << std::endl;

        result = ::read(m_sock, buf , MAXRECV );
        if (result < 1)
        {
            std::cout << "status == -1   errno == " << errno
                    << "  in Socket::read stream\n";
            return 0;
        }
        if (result == 0)
        {
            std::cout << "Socket :: read :: 3 :: DONE" << std::endl;
            return 0;
        }


        bytesRead += result;
        temp.assign(buf);
        std::cout << "Socket :: read :: 4 :: bytesRead\t" << bytesRead <<"\ttemp::\t" << temp << std::endl;
        ss << temp;
    }

    return result;

}

This is the output:

ssInput.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 E|N|D


Socket :: write :: 1 :: ss.str():   0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 E|N|D sizeof(ss)  188 ss.str().size() 895
Socket :: write :: 2 :: DONE
Socket :: read :: 1
Socket :: read :: 2
Socket :: read :: 4 :: bytesRead    500 temp::  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15

streamIn.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15

streamOut.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 
Socket :: write :: 1 :: ss.str():   0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249   sizeof(ss)  188 ss.str().size() 890
Socket :: write :: 2 :: DONE
Socket :: read :: 1
Socket :: read :: 2
Socket :: read :: 4 :: bytesRead    500 temp::  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15

ssOut.str()0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 15

答案 3 :(得分:0)

我认为现在正在运行: 作为每个元素之间的分隔符,我使用了'|' char并完成传输我检查每个元素,如果它包含一个“E-N-D”字符串。

bool Socket::write(const std::stringstream& ss) const
{

    std::string stemp;
    std::stringstream tempSS;
    tempSS << ss.rdbuf();

    do
    {
        tempSS >> stemp;
        stemp+= "|";                        // seperator

        int bytes_written = ::write(m_sock, stemp.c_str() , stemp.length() );

        if (bytes_written == -1)
        {
            return false;
        }

    }while(stemp.find("E-N-D") ==  std::string::npos);

    return true;

}


int Socket::read(std::stringstream& ss) const
{
    std::string stemp;

    int result;

    do
    {
        char buf[MAXRECV + 1];
        std::memset(buf, 0, MAXRECV + 1);

        result = ::read(m_sock, buf , MAXRECV );
        if (result < 1)
        {
            std::cout << "status == -1   errno == " << errno
                    << "  in Socket::read stream\n";
            return 0;
        }
        stemp.assign(buf);
    }while(stemp.find("E-N-D") ==  std::string::npos);

    return result;

寻求帮助