TCP套接字程序发送一个Buffer。

时间:2014-04-24 10:50:04

标签: c++ sockets tcp

//以下代码工作正常,但用于发送字符串。为了发送包含具有不同数据类型字段的标头的缓冲区(缓冲区的地址),我必须做出哪些更改? (我是编程新手。所以,请原谅我的菜鸟)

ClientSocket.cpp

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 connect." );
    }

}


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;
}






simple_client_main.cpp

#include "ClientSocket.h"
#include "SocketException.h"
#include <iostream>
#include <string>

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

      ClientSocket client_socket ("169.254.103.63", 30000 );

      std::string reply;

      try
    {
      client_socket << "Test message.";
      client_socket >> reply;
    }
      catch ( SocketException& ) {}

      std::cout << "We received this response from the server:\n\"" << reply << "\"\n";;

    }
  catch ( SocketException& e )
    {
      std::cout << "Exception was caught:" << e.description() << "\n";
    }

  return 0;
}




ServerSocket.cpp

// 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." );
    }
}




simple_server_main.cpp

#include "ServerSocket.h"
#include "SocketException.h"
#include <string>
#include <iostream>

int main ( int argc, int argv[] )
{
  std::cout << "running....\n";

  try
    {
      // Create the socket
      ServerSocket server ( 30000 );

      while ( true )
    {

      ServerSocket new_sock;
      server.accept ( new_sock );

      try
        {
          while ( true )
        {
          std::string data;
          new_sock >> data;
          new_sock << data;
        }
        }
      catch ( SocketException& ) {}

    }
    }
  catch ( SocketException& e )
    {
  std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
    }

  return 0;
}

1 个答案:

答案 0 :(得分:0)

您必须添加新的send()和recv()函数,这些函数尚未显示。套接字可以发送二进制数据,但您必须告诉它大小(以字节为单位)。在recv()方面,在获得整个结构之前,需要确定需要多少字节的问题。这通常需要为每条消息添加一些协议,例如长度前缀。