No route to host 에러가 납니다.
글쓴이: study / 작성시간: 금, 2021/12/10 - 7:02오전
이곳의 예제를 이용해서 간단히 TCP server 와 client를 만들었습니다.
https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/
예제대로 하면 컴파일 에러가 나더군요.
그래서, server쪽 code는 아래와 같이 조금 수정을 했습니다.
#include <stdio.h> #include <netdb.h> #include <netinet/in.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/types.h> #define PORT 12345 #define MAX 80 /* Function designed for chat between client and server */ void func(int sockfd) { char buff[MAX]; int n; for (;;) { bzero(buff, MAX); /* read the message from client and copy it in buffer */ read(sockfd, buff, sizeof(buff)); /* print buffer which contains the client contents */ printf("From client : %s\t To client : ", buff); bzero(buff, MAX); n = 0; /* copy server message in the buffer */ while ((buff[n++] = getchar()) != '\n') ; /* and send that buffer to client */ write(sockfd, buff, sizeof(buff)); /* if msg contains "Exit" then server exit and chat ended */ if (strncmp("exit", buff, 4) == 0) { printf("Server Exit ....\n"); break; } } } int main() { int sockfd, connfd, len; struct sockaddr_in servaddr, cli; /* socket creation and verification */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket creation failed...\n"); exit(0); } else { printf("socket successfully created...\n"); } bzero(&servaddr, sizeof(servaddr)); /* assign IP and PORT */ servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); if ((bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) != 0) { printf("socket bind failed...\n"); exit(0); } else { printf("Socket successfully binded...\n"); } /* Now server is ready to listen and verification */ if ((listen(sockfd, 5)) != 0) { printf("Listen failed ...\n"); exit(0); } else { printf("Server listening ...\n"); } len = sizeof(cli); /* Accept the data packet from client and verification */ connfd = accept(sockfd, (struct sockaddr *)&cli, &len); if (connfd < 0) { printf("Server accept failed...\n"); exit(0); } else { printf("Server accept the client...\n"); } /* Function for chatting between client and server */ func(connfd); /* After chatting, close the socket */ close(sockfd); }
client쪽 code도 아래와 같이 약간 수정을 했습니다.
#include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <arpa/inet.h> #include <sys/socket.h> #include <unistd.h> #define MAX 80 #define PORT 12345 void func(int sockfd) { char buff[MAX]; int n; for (;;) { bzero(buff, sizeof(buff)); printf("Enter the string : "); n = 0; while ((buff[n++] = getchar()) != '\n') ; write(sockfd, buff, sizeof(buff)); bzero(buff, sizeof(buff)); read(sockfd, buff, sizeof(buff)); printf("From Server : %s", buff); if ((strncmp(buff, "exit", 4)) == 0) { printf("Client Exit ...\n"); break; } } } int main() { int sockfd, connfd; struct sockaddr_in servaddr, cli; /* socket create and verification */ sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket creation failed...\n"); exit(0); } else { printf("socket successfully created...\n"); } bzero(&servaddr, sizeof(servaddr)); /* assign IP, PORT */ servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr("192.168.1.203"); servaddr.sin_port = htons(PORT); /* connect the client socket to server socket */ if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) { printf("connection with the server failed ...\n"); printf("error : %s\n", strerror(errno)); exit(0); } else { printf("connected to the server ...\n"); } /* function for chat */ func(sockfd); /* close the socket */ close(sockfd); return 0; }
그리고 server는 192.168.1.203에서 실행하고 client는 192.168.1.219에서 실행을 해보았는데요
Client쪽에서 "No route to host"라는 에러가 발생하네요.
Server, client양쪽에서 iptables -F를 실행해서, 이제는 iptables쪽에서는 아무 설정이 없는 상태입니다.
조언 부탁드립니다.
Forums:
C언어 네트워크 소켓 프로그래밍이라는 건 참 여러
C언어 네트워크 소켓 프로그래밍이라는 건 참 여러 가지 이유로 망할 수가 있지요.
그런데 저렇게 간단한 수준의 프로그램이 접속조차 제대로 안 된다면 프로그램보단 네트워크 문제일 가능성이 높아 보입니다.
서버와 클라이언트가 같은 네트워크에 있는 것이 맞나요? 어떻게 구성되어 있는 건지 전혀 설명이 없으니 감을 잡을 수가 없네요.
예 같은 네트워크에 있습니다.
원래 질문에서 말씀드렸듯이 Server와 Client는 모두 192.168.1.0 network상에 연결되어 있고,
ping은 제대로 됩니다.
iptables -F 명령으로 양쪽에 iptables 구성은 모두 clear해 놓은 상태입니다.
조금 이상한 것은..
조금 더 테스트를 해보고 있었는데요,
똑 같은 프로그램은 2개의 ubuntu 시스템간에 진행하면 성공하고
2개의 CentOS 8 시스템간에 진행하면 문제가 있네요.
OS자체가 문제일리는 없을 것 같고, 배포판의 기본설정차이가 문제가 아닐까 생각하는데,
아직은 정확한 문제는 못 찾았어요.
일단 테스트 할때는 iptables -F로 iptables을 clear하고,
SELINUX는 disable했습니다.
답을 찾았습니다.
CentOS에서 안되던 문제를 찾은 것 같습니다.
firewalld 서비스를 중단시키니까 잘 되네요
사용한 명령어는 "systemctl stop firewalld" 였습니다.
댓글 달기