0

I'm trying make IPv6 server, but i have issue with socket binding. "could not bind socket"

All code:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
 int server_port = 8877;
 struct sockaddr_in6 server_address;
 memset(&server_address, 0, sizeof(server_address));
 server_address.sin6_family = AF_INET6;
 server_address.sin6_port = htons(server_port);
 server_address.sin6_addr = in6addr_any;
 int sockfd;
 if (sockfd = socket(AF_INET6, SOCK_STREAM, 0) < 0) {
 printf("could not create listen socket\n");
 return 1;
 }
 if (bind(sockfd, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
 printf("could not bind socket\n");
 return 1;
 }
 int numberOfClients = 1;
 if (listen(sockfd, numberOfClients) < 0) {
 printf("could not open socket for listening\n");
 return 1;
 }
 struct sockaddr_in client_address;
 int client_len = 0;
 char buff4[INET_ADDRSTRLEN];
 while (1) {
 int sock;
 if ((sock =
 accept(sock, (struct sockaddr *)&client_address,
 0)) < 0) {
 printf("could not open a socket to accept data\n");
 return 1;
 }
 //printf("client connected with ip address: %s\n", inet_ntop(AF_INET, &(client_address.sin_addr), buff4, INET_ADDRSTRLEN));
 int n = 0;
 int len = 0, maxlen = 100;
 char buffer[maxlen];
 char *pbuffer = buffer;
 printf("client connected with ip address: %s\n",
 inet_ntoa(client_address.sin_addr));
 while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {
 pbuffer += n;
 maxlen -= n;
 len += n;
 printf("received: '%s'\n", buffer);
 // echo received content back
 send(sock, buffer, len, 0);
 }
 close(sock);
 }
 close(sockfd);
 return 0;
}
Ron Maupin
6,5564 gold badges33 silver badges39 bronze badges
asked Mar 10, 2018 at 15:22
3
  • 1
    use perror to display the reason why you cannot bind the socket. My guess is that either the port is already in use or that you are trying to bind to a privileged port but have no permission. Commented Mar 10, 2018 at 16:00
  • can you rewrite the error message to fprintf(stderr, "could not bind to socket: %s", strerror(errno)); so that you get to see the reason why it fails? Or just use perror instead? Commented Mar 10, 2018 at 16:00
  • This outputs Socket operation on non-socket. Commented Mar 10, 2018 at 22:40

1 Answer 1

2

The problem here is your order of operations.

You have written:

 if (sockfd = socket(AF_INET6, SOCK_STREAM, 0) < 0) {

You expected this to assign the return value of socket() to sockfd. But instead, it compares that return value to 0, and whether that value is less than 0 is what is actually stored in sockfd.

Before comparing, you should use an extra pair of parentheses to make explicit that you want to do the assignment and only then do the comparison:

 if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {

Better yet, make the code more maintainable by making it more obvious what is going on, by assigning first and then comparing separately.

 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
 if (sockfd < 0) {
answered Mar 10, 2018 at 22:47
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.