반응형
#include <vector>
#include <cstddef> // std::byte를 위한 헤더
#include <sys/socket.h>
#include <algorithm> // std::copy를 위한 헤더
int main() {
int sockfd; // 소켓 파일 디스크립터
// sockfd 초기화하는 코드...
int header_length = 100; // 헤더의 길이
std::vector<std::byte> header(header_length); // 헤더를 저장할 동적 배열
while (true) {
ssize_t n = recv(sockfd, header.data(), header.size(), 0); // 헤더 받기
if (n == -1) {
// 에러 처리 코드...
}
header.resize(n); // 실제로 받은 데이터만큼 크기 조정
int body_length = getBodyLength(header); // 헤더에서 바디의 길이 파악
std::vector<std::byte> body(body_length); // 바디를 저장할 동적 배열
n = recv(sockfd, body.data(), body.size(), 0); // 바디 받기
if (n == -1) {
// 에러 처리 코드...
}
body.resize(n); // 실제로 받은 데이터만큼 크기 조정
// 헤더와 바디를 합치는 코드...
std::vector<std::byte> combined(header.size() + body.size());
std::copy(header.begin(), header.end(), combined.begin());
std::copy(body.begin(), body.end(), combined.begin() + header.size());
// combined를 이용한 처리 코드...
}
}
int getBodyLength(const std::vector<std::byte>& header) {
// header에서 바디의 길이를 파악하여 반환하는 코드...
}
c++ 통신 헤더와 바디 나눠 받기
반응형
'프로그래밍 _공부자료. > C++ 공부' 카테고리의 다른 글
c++ 데이타송수신 (0) | 2023.11.13 |
---|---|
std::byte 동적으로 메시지 수신받기 (0) | 2023.11.10 |
c++ 파일 라인 함수명 출력하는 매크로 (0) | 2023.11.07 |
문자열 은 똑같은데 하나의 숫자만 가변일때 해결법 c++ (0) | 2023.11.01 |
c++ 폴더를 생성하는 가장 간단한 방법 (0) | 2023.10.19 |
댓글