Click here to Skip to main content
15,881,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
i have udp chat in C, but on terminal is written: Bind error : Permission denied, I tried a lot of different ports but nothing is working. What can be a problem?

What I have tried:

<pre> sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  if(sock_fd == -1){
    perror("Socket error ");
    return 1;
  }

  if (argc < 4) {
    printf("%s <local port> <remote host> <remote port>\n", argv[0]);
    return 1;
  }

  peer_addr.sin_family = AF_INET;
  peer_addr.sin_port = htons(remote_port);
  if (inet_aton(argv[2], &peer_addr.sin_addr) == 0) {
    printf("Error");
    return 1;
  }

  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(local_port);

  int local_bind = bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
  if(local_bind == -1){
    perror("Bind error ");
    return 1;
  }
  
  chat(sock_fd, &peer_addr);
  close(sock_fd);

  return 0;
}



<pre>void chat(int sock_fd, struct sockaddr_in *peer){
  
    struct timeval tv;
    ssize_t bytes;
    char inp_buffer[1024];
    char out_buffer[1024];
    fd_set readfds;
  
   while(1){
      FD_ZERO(&readfds);
      FD_SET(STDIN, &readfds);
      FD_SET(sock_fd, &readfds);
    
      tv.tv_sec = 5;
      tv.tv_usec = 0;
      select(sock_fd + 1, &readfds, NULL, NULL, &tv);


      if (FD_ISSET(STDIN, &readfds)){
         bytes = read(0, out_buffer, sizeof(out_buffer));
        if (bytes < 0) {
          printf("Error\n");
          break;
        }
        printf("Sending: %.*s\n", (int)bytes, out_buffer);
        bytes = sendto(sock_fd, out_buffer, bytes, 0,(struct sockaddr *)peer, sizeof(struct sockaddr_in));
        if (bytes < 0) {
          printf("Error \n");
          break;
        }
      }


    
      if(FD_ISSET(sock_fd, &readfds)){
         bytes = recvfrom(sock_fd, inp_buffer, sizeof(inp_buffer),0, NULL, NULL);
        if (bytes < 0) {
          printf("Error recvfrom\n");
          break;
        }
        if (bytes > 0) {
          printf("Received: %.*s\n", (int)bytes, inp_buffer);

        }
         if(!strcmp(inp_buffer,"Exit\n")){
            printf("Received Exit => closing\n");
            break;
         }
   }
}
}
Posted
Updated 18-Jun-21 1:15am
v2
Comments
Richard MacCutchan 18-Jun-21 7:01am    
What values are you using?
Member 15245946 18-Jun-21 7:02am    
./chat 127.0.0.1 1345 1346
Richard MacCutchan 18-Jun-21 8:11am    
But in your code you are using argv[2] as the host address, but your call sets that to 1345. And you refer to remote_port and local_port but you never initialise them to any values (that I can see).
Greg Utas 18-Jun-21 7:44am    
I'm guessing you have some kind of firewall running on your device that thinks your software could be malware, in which case you'll have to tell the firewall to give your program permission to access those ports.
Member 15245946 18-Jun-21 13:36pm    
Thanks
It's working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900