프로그래밍/2024 네트워크 프로그래밍 12

12. shutdown을 이용한 우아한 연결 종료(half close)

먼저 서버부터 살펴보도록 하겠습니다.#include "mylib2.h" #include "fcntl.h" #define SERVER_FILENAME "server.bin" #define BUFFER_SIZE 1000 #define MESSAGE_SIZE 1000 int main(int argc, char *argv[]) { int fileFd; int listenFd, connFd; struct sockaddr_in clientAddress; socklen_t clientAddressLen; unsigned char buffer[BUFFER_SIZE]; int nRead; char message[MESSAGE_SIZE]; if(argc != 2){ printf("Usag..

11. udp 클라이언트/서버 통신 살펴보기

#include "mylib.h"#define BUFFER_SIZE 512void handle_error(char *);int main(int argc, char *argv[]){ int sockFd; struct sockaddr_in serverAddress, clientAddress; socklen_t clientAddressLen; char buffer[BUFFER_SIZE]; int nRecv; if(argc != 2){ printf("Usage: %s \n", argv[0]); exit(1); } if((sockFd = socket(PF_INET, SOCK_DGRAM, 0))  먼저 서버부터 살펴보도록 하겠습니다.많이 등장한 부분에 대한 내용은 간단하게 설명하고 넘어가겠습니다.Udp 통신에 ..

9. 구조체 헤더를 이용한 소켓통신 - 클라이언트와 헤더파일 살펴보기

헤더파일 calc.c먼저 헤더파일 분석입니다.구조체를 이용하여 계산 요청 및 응답 데이터를 정의하였으며, 데이터 크기와 정렬을 정확히 맞추기 위해 패킹을 사용했습니다.#pragma pack(1)을 사용하여 1바이트 단위로 정렬하여, 구조체의 멤버간의 패딩이 생기지 않게 강제했습니다. 이를 통해 데이터 크기를 줄이고, 일관적으로 내용을 표시할 수 있습니다. 먼저 클라이언트에서 서버로 계산 요청을 정의하는 구조체인 CALC_REQ_HDR_t입니다. 멤버는 연산에 사용되는 피연산자의 개수인 numOperand와 연산을 수행할 연산자를 나타내는 operator 변수로 이루어져 있습니다. 이를 통해, 서버에 자신이 수행할 연산의 내용을 전달합니다. 다음으로 서버에서 클라이언트로 계산 결과를 정의하는 구조체인 CA..

7. read를 사용한 에코 클라이언트 최적화

에코 클라이언트 코드#include #include #include #include #include #include #define BUFFER_SIZE 1024 void handle_error(char *); int main(int argc, char *argv[]) { int sockFd; struct sockaddr_in serverAddress; char buffer[BUFFER_SIZE]; int nRead; if(argc != 3){ printf("Usage: %s \n", argv[0]); exit(1); } if((sockFd = socket(PF_INET, SOCK_STREAM, 0)) 커스템 헤더 mylib.h#include #inc..