资源简介
Steam绘制开源请勿用于商业用途后果自负
代码片段和文件信息
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include “client.hpp“
#include
#pragma comment(lib “ws2_32“)
#include
namespace network
{
client::client(unsigned short port)
: sock(INVALID_SOCKET) port(port)
{
}
client::~client()
{
if (this->sock != INVALID_SOCKET)
{
this->uninitialize();
}
}
bool client::make_interaction(interaction_handler finalize_handler)
{
if (!this->initialize())
{
this->output_wsa_error(error_type::connect);
return false;
}
bool result = finalize_handler(this);
this->uninitialize();
return result;
}
int client::read(unsigned char* buffer std::size_t assumed_size)
{
unsigned short data_length = 0;
int data_length_size = this->raw_read(reinterpret_cast(&data_length) sizeof(unsigned short));
if (!data_length_size || data_length_size != sizeof(unsigned short))
{
return 0;
}
if (assumed_size && data_length != assumed_size)
{
return 0;
}
unsigned char data_buffer[16384];
for (int data_read = 0 offset = 0 data_to_read = data_length; data_to_read > 0; data_to_read -= data_read offset += data_read)
{
data_read = this->raw_read(data_buffer + offset data_to_read);
if (!data_read || data_read == SOCKET_ERROR)
{
return 0;
}
}
memcpy(buffer data_buffer data_length);
return data_length;
}
bool client::write(unsigned char const* buffer std::size_t size)
{
unsigned short data_length = static_cast(size);
if (!this->raw_write(reinterpret_cast(&data_length) sizeof(unsigned short)))
{
return false;
}
unsigned char* data_buffer = new unsigned char[size];
memcpy(data_buffer buffer size);
if (!this->raw_write(data_buffer size))
{
delete[] data_buffer;
return false;
}
delete[] data_buffer;
return true;
}
bool client::initialize()
{
if (WSAStartup(MAKEWORD(2 0) new WSADATA))
{
return false;
}
this->sock = socket(AF_INET SOCK_STREAM IPPROTO_TCP);
if (this->sock == INVALID_SOCKET)
{
return false;
}
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(this->port);
addr.sin_addr.s_addr = inet_addr(“192.168.1.44“); // inet_addr(“127.0.0.1“);
std::fill(addr.sin_zero addr.sin_zero + sizeof(addr.sin_zero) 0);
return (connect(this->sock reinterpret_cast(&addr) sizeof(sockaddr_in)) != SOCKET_ERROR);
}
bool client::uninitialize()
{
return (shutdown(this->sock SD_BOTH) != SOCKET_ERROR && closesocket(this->sock) != SOCKET_ERROR && WSACleanup() == 0);
}
int client::raw_read(unsigned char* buffer std::size_t size)
{
int bytes_read = recv(this->sock reinterpret_cast(buffer) size 0);
if (bytes_read == 0 || bytes_read == SOCKET_ERROR)
{
if (bytes_read == SOCKET_ERROR)
{
this->output_wsa_error(error_type::read);
}
return bytes_read;
}
else if (bytes_read < static_cast(size))
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-11-27 12:22 XignCode3-bypass-real\
文件 2518 2017-11-27 12:22 XignCode3-bypass-real\.gitattributes
文件 2233 2017-11-27 12:22 XignCode3-bypass-real\.gitignore
文件 547 2017-11-27 12:22 XignCode3-bypass-real\README.md
目录 0 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\
文件 4879 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\XignCode Client.vcxproj
文件 1264 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\XignCode Client.vcxproj.filters
文件 3676 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\XignCode.cpp
文件 750 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\XignCode.hpp
文件 5037 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\client.cpp
文件 836 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\client.hpp
文件 87 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\export.def
文件 551 2017-11-27 12:22 XignCode3-bypass-real\XignCode Client\main.cpp
目录 0 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\
目录 0 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\Resources\
文件 1406 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\Resources\xigncode.ico
文件 4738 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\XignCode Host.vcxproj
文件 1812 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\XignCode Host.vcxproj.filters
文件 3508 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\XignCode.cpp
文件 336 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\XignCode.hpp
文件 10134 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\icon1.ico
文件 701 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\main.cpp
文件 904 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\resource.hpp
文件 3262 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\resource.rc
文件 1899 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\server.cpp
文件 253 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\server.hpp
文件 4016 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\session.cpp
文件 642 2017-11-27 12:22 XignCode3-bypass-real\XignCode Host\session.hpp
目录 0 2017-11-27 12:22 XignCode3-bypass-real\XignCode Library\
文件 1336 2017-11-27 12:22 XignCode3-bypass-real\XignCode Library\ReadMe.txt
文件 3937 2017-11-27 12:22 XignCode3-bypass-real\XignCode Library\XignCode Library.vcxproj
............此处省略12个文件信息
- 上一篇:甘肃地级城市区域,河流shp
- 下一篇:一种新的模式匹配(模糊搜索)算法
评论
共有 条评论