목록TCP IP 소켓프로그래밍 (7)
Creative Code
※thread1.c파일 #include #include #include void* thread_main(void *arg); int main(int argc, char *argv[]) { pthread_t t_id; // 쓰레드 식별자 int thread_param = 5; // 쓰레드에 전달할 인자 if (pthread_create(&t_id, NULL, thread_main, (void*)&thread_param) != 0) { puts("pthread_create() error"); return -1; } // 메인 쓰레드가 10초 동안 대기 sleep(10); puts("end of main"); return 0; } void* thread_main(void *arg) { int i; int cn..
※gethostbyname.c파일 #include #include #include #include #include void error_handling(char *message); int main(int argc, char *argv[]) { int i; struct hostent *host; // 명령행 인자 체크 if (argc != 2) { printf("Usage : %s \n", argv[0]); exit(1); } // 호스트 정보 조회 host = gethostbyname(argv[1]); if (!host) error_handling("gethost... error"); printf("Official name: %s\n", host->h_name); // 별칭(alias) 목록 출력 for ..
※get_buf.c파일 #include #include #include #include void error_handling(char *message); int main(int argc, char *argv[]) { int sock; int snd_buf, rcv_buf, state; socklen_t len; // 소켓 생성 sock = socket(PF_INET, SOCK_STREAM, 0); // Output buffer size (송신 버퍼 크기) 조회 len = sizeof(snd_buf); state = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void*)&snd_buf, &len); if (state) error_handling("getsockopt() erro..
※echo_client2.c파일 #include #include #include #include #include #include #define BUF_SIZE 1024 void error_handling(char *message); int main(int argc, char *argv[]) { int sock; char message[BUF_SIZE]; int str_len, recv_len, recv_cnt; struct sockaddr_in serv_adr; if (argc != 3) { printf("Usage : %s \n", argv[0]); exit(1); } // 소켓 생성 sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == -1) error_hand..
※echo_server.c파일 #include #include #include #include #include #include #define BUF_SIZE 1024 void error_handling(char *message); int main(int argc, char *argv[]) { int serv_sock, clnt_sock; char message[BUF_SIZE]; int str_len, i; struct sockaddr_in serv_adr; struct sockaddr_in clnt_adr; socklen_t clnt_adr_sz; // 명령행 매개변수 체크 if (argc != 2) { printf("Usage : %s \n", argv[0]); exit(1); } // 서버 소켓 생..
※endian_conv.c파일 #include #include int main(int argc, char *argv[]) { unsigned short host_port=0x1234; unsigned short net_port; unsigned long host_addr=0x12345678; unsigned long net_addr; net_port=htons(host_port); net_addr=htonl(host_addr); printf("Host ordered port: %#x \n", host_port); // 빅엔디안 printf("Network ordered port: %#x \n", net_port); // 리틀엔디안 printf("Host ordered address: %#lx \n", h..