TCP套接字程序发送缓冲区

TCP socket program to send a Buffer.

本文关键字:缓冲区 程序 套接字 TCP      更新时间:2023-10-16

//下面的代码运行良好,但用于发送字符串。为了发送包含不同数据类型字段的标头的缓冲区(缓冲区地址),我必须进行哪些更改?(我是编程新手。所以,请原谅我的愚蠢)

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

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