No route to host 에러가 납니다.

study의 이미지

이곳의 예제를 이용해서 간단히 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쪽에서는 아무 설정이 없는 상태입니다.
조언 부탁드립니다.

익명 사용자의 이미지

C언어 네트워크 소켓 프로그래밍이라는 건 참 여러 가지 이유로 망할 수가 있지요.

그런데 저렇게 간단한 수준의 프로그램이 접속조차 제대로 안 된다면 프로그램보단 네트워크 문제일 가능성이 높아 보입니다.

서버와 클라이언트가 같은 네트워크에 있는 것이 맞나요? 어떻게 구성되어 있는 건지 전혀 설명이 없으니 감을 잡을 수가 없네요.

study의 이미지

원래 질문에서 말씀드렸듯이 Server와 Client는 모두 192.168.1.0 network상에 연결되어 있고,
ping은 제대로 됩니다.

iptables -F 명령으로 양쪽에 iptables 구성은 모두 clear해 놓은 상태입니다.

study의 이미지

조금 더 테스트를 해보고 있었는데요,
똑 같은 프로그램은 2개의 ubuntu 시스템간에 진행하면 성공하고
2개의 CentOS 8 시스템간에 진행하면 문제가 있네요.

OS자체가 문제일리는 없을 것 같고, 배포판의 기본설정차이가 문제가 아닐까 생각하는데,
아직은 정확한 문제는 못 찾았어요.

일단 테스트 할때는 iptables -F로 iptables을 clear하고,
SELINUX는 disable했습니다.

study의 이미지

CentOS에서 안되던 문제를 찾은 것 같습니다.
firewalld 서비스를 중단시키니까 잘 되네요

사용한 명령어는 "systemctl stop firewalld" 였습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.