TCP套接字程序发送缓冲区
TCP socket program to send a Buffer.
//下面的代码运行良好,但用于发送字符串。为了发送包含不同数据类型字段的标头的缓冲区(缓冲区地址),我必须进行哪些更改?(我是编程新手。所以,请原谅我的愚蠢)
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()方面,在获得整个结构之前,计算需要多少字节会出现问题。这通常需要添加一些协议,例如为每条消息添加一个长度前缀。
相关文章:
- 从OpenGL应用程序中提取颜色/深度缓冲区
- IOCTL 驱动程序系统缓冲区始终为空
- 使用协议缓冲区创建通用反序列化程序
- 用于了解输入和输出流缓冲区实际工作方式的程序
- 我正在编写一个简单的客户端套接字应用程序,但在连接后服务器收到一个空缓冲区
- 将缓冲区转换为在现有程序中工作的iStream
- 将数据分配给了数组分配,但程序显示了以缓冲区超支的输出
- 该程序是针对协议缓冲区运行时库的2.6.1版本编译的,该版本与已安装的版本(3.5.0)不兼容
- 协议缓冲区2和3在同一C Linux应用程序中
- 缓冲区溢出到其他 exe 的内存中?还是从远程桌面程序到 csrss.exe?
- CRT 检测到应用程序在堆缓冲区(新建/删除)类结束后写入内存
- 为什么我的程序一直给我堆缓冲区溢出,即使我没有超出数组的界限或覆盖任何值
- 设备驱动程序内存缓冲区处理器缓存问题
- CGI程序的自定义输出缓冲区,运算符重载
- 平面缓冲区验证程序行为
- 简单协议缓冲区程序与G 编译时可起作用,但不clang
- 为什么在我的测试程序中,从字符缓冲区读取一个int时,"std::copy"5x(!)比"
- 当数据被写入协议缓冲区时,我如何让Java触发C++程序,反之亦然
- 输出缓冲区和Cntrl+C以退出程序
- 如何修复此错误?CRT检测到应用程序在堆缓冲区结束后写入内存