首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TCP套接字程序发送缓冲区。

TCP套接字程序发送缓冲区。
EN

Stack Overflow用户
提问于 2014-04-24 18:50:04
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
运行
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2014-04-24 21:26:20

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23266755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档