1
votes

I am trying to connect to my local UNIX server i made from another remote device. the Server is up and listening to the port i specified. i also added a new firewall rule to open that port but still my client cannot connect. it shows ERROR CONNECTION REFUSED

here is my server code

int main() {
  int fd, i,svclient,rval,msg;
  int clients[10], num_clients;
  fd_set read_set,write_set;
  char buf[100];

  struct sockaddr_in addr;

  if ( (fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket error");
    exit(-1);
  }


  bzero((char *) &addr, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(4001);

  //strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
  //strcpy(addr.sun_path, NAME);

  if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
    perror("bind error");
    exit(-1);
  }
  printf("Bind complet...\n");

  if (listen(fd, 20) == -1) {
    perror("listen error");
    exit(-1);
  }

  num_clients = 0;
  int size = sizeof(fd);

   while (1) {

    int clientfd;
    struct sockaddr_in client_addr;
    int addrlen=sizeof(client_addr);
    FD_ZERO(&read_set);
    FD_SET(fd, &read_set);

    for (i = 0; i < num_clients; i++) { //at first this part will not excute
      FD_SET(clients[i], &read_set);
    }

    select(fd + num_clients + 1, &read_set, NULL, NULL, NULL);


    if (FD_ISSET(fd, &read_set)) {
      if ( (clients[num_clients++] = accept(fd,(struct sockaddr*)&client_addr,&addrlen)) == -1) {
        perror("accept error");
        continue;
      }
      /*printf("incoming message..................... !\n \n");*/
      printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    }


    for (i = 0; i < num_clients; i++) {

      if (FD_ISSET(clients[i], &read_set)) {
        msg = read(clients[i], buf, sizeof(buf));
        if(msg > 0){
          buf[msg] = 0;
          int savedclnt = clients[i];
          printf("%s \n \n", buf);

          /*for(int p=0;p<num_clients;p++)
          {
            if( clients[p]!= savedclnt){
              write(clients[p],buf,msg);
            }

          }*/
        }

      }

    }
  }
}

and my client

    int main( )
{   

    struct uci_context *uci;
    uci = uci_init();
    int sockfd;
    int ret;
    struct sockaddr_in dest;
    struct addrinfo hint, *res = NULL;
    struct hostent *host;
    char *hostip;
    char *string;



    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        puts("Unble to create socket");
        exit(1);
    }

    hostip = ucix_get_option(uci, "pack_mon", "pack_monitoring", "address");
    string = ucix_get_option(uci, "pack_mon", "pack_monitoring", "port");

    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(atoi(string));

    memset(&hint, '\0', sizeof hint);
    hint.ai_family = PF_UNSPEC;
    hint.ai_flags = AI_NUMERICHOST;

    printf(" %s- %s\n", hostip, string );



    if(isdigit(hostip[0])){
        ret = getaddrinfo(hostip, NULL, &hint, &res);// this is more efficient than inet_addr

        if (ret) {

            exit(1);
        }
    }else if( (host = gethostbyname(hostip)) != 0){

        memcpy((char*)&dest.sin_addr , (char*)host->h_addr , (sizeof dest.sin_addr)+1);

    }else{

        exit(1);
        printf("cannot resolve ip address");
    }  


    if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) < 0 )
    {

        perror("ERROR Connecting" );
        exit(1);
    }else{

        printf("Port number %s is open.....\n",string);
    }


    char *message;
    message = "help";
    write(sockfd,message,strlen(message));


    close(sockfd);
    freeaddrinfo(res);
    return 0;
}

FIREWALL RULE

sudo iptables -I INPUT -p tcp --dport 4001 -j ACCEPT

Error is :

192.168.10.155- 4001

ERROR Connecting: Connection refused

and this logs are coming from this codes :

printf(" %s- %s\n", hostip, string );

perror("ERROR Connecting");

exit(1);

1
If you want to check errno (either directly or indirectly through e.g. perror) you need to do it immediately after the failed function. If you use any other function that might set errno (which printf might indirectly do) then the value of errno is undefined.Some programmer dude
First off, are you sure the firewall is open? There could be other rules blocking this before your rule? One way to make sure (maybe you are an expert on iptables but just to make sure..), is to setup something else on that port and test. If that works you are sure the problem is in your code. I only bring it up since you added the firewall rule and it sounds like the problem could be there or in the code. Seting up a netcat "webserver" is one way to test. Run: netcat -l 5001 < index.html Then you can try to connect to it and see if you get the page. If it works you know the code is brokenMrApnea
As for your problem, in the outcommented code if hostip[0] is a digit then you don't set dest.sin_addr. Assuming that you assume that hostip is an IP-address in that case, you don't need to call getaddrinfo for that. Just use inet_pton. Also, in the other case you should not really be using strncpy as the data isn't really a string.Some programmer dude
@FSDaniel when i run netcat -l 5001 < index.html and try to connect to it from another remote device, it shows CONNECTION REFUSED but when i connect from same device, it works.johnleonard onwuzuruigbo
Regarding the last comment, it indeed seems like you have a firewall issue. That is better solved by going to superuser.com and asking about that specifically instead.Some programmer dude

1 Answers

0
votes

Your client has no code to specify the IP address it wants to connect to. All the code that could do that has been commented out.

Update: Now your bug is here:

    strncpy((char*)&dest.sin_addr , (char*)host->h_addr , sizeof dest.sin_addr);

The strncpy function is only suitable for C-style strings. You need to use memcpy or something similar. This will only copy part of the IP address if any octet other than its last one (in network byte order) is zero.

Update: Now your bug is here:

    printf("%d\n", connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) < 0);
    perror("hmmmm" );
    exit(1);

This calls connect, then calls printf and then calls perror. The problem is, the call to printf can modify errno even if it succeeds. Thus your call to perror can print a totally irrelevant error message.