Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

c - Why bind function is returning -1

So i am following the book of UNIX network programming, and tried to write a simple daytime server from chapter 1 and client but the bind function is always returning an error, what I'm doing wrong can anyone help??

server.c

/*
 * Daytime Server
*/

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>

void printError(char *str)
{
    printf("%s", str);
    exit(0);
}

int main(int argc, char **argv)
{
    int listenfd, connfd;
    struct sockaddr_in servaddr;
    char buff[4096];
    time_t ticks;

    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printError("Error at line 32 socket fuct.");
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(13);

    if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        printError("Error at bind function");
    }
    

    if (listen(listenfd, 1024) < 0)
    {
        printError("Error at listen fuct.");
    }

    while (1)
    {
        if ((connfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) < 0)
        {
            printError("Error at accept fuct.");
        }

        ticks = time(NULL);
        snprintf(buff, sizeof(buff), "%.24s
", ctime(&ticks));

        if (write(connfd, buff, strlen(buff)) < 0)
        {
            printError("Error at write fuct.");
        }

        close(connfd);
    }

    return EXIT_SUCCESS;
}

client.c

/*
 * Daytime Client
*/

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

void printError(char *str)
{
    printf("%s", str);
    exit(0);
}

int main(int argc, char **argv)
{
    int socketfd, n;
    char recvline[4097];
    struct sockaddr_in servaddr;

    if (argc != 2)
    {
        printError("Requires ip address of the server");
    }

    if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printError("Unable to create a Connection");
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_port = htons(13);
    servaddr.sin_family = AF_INET;

    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    {
        printError("Not valid IP");
    }

    if (connect(socketfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        printError("Connection Error");
    }

    while ((n = read(socketfd, recvline, 4096)) > 0)
    {
        recvline[n] = 0;
        if (fputs(recvline, stdout) == EOF)
        {
            printError("Fputs Error");
        }
    }

    if (n < 0)
    {
        printError("Not readable");
    }

    return EXIT_SUCCESS;
}

Running server is always returning -1 on bind function. and running client always prints connection error.

Thank you in advance for help.

question from:https://stackoverflow.com/questions/65946404/why-bind-function-is-returning-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should check with errno to find out WHY bind() is failing when it returns -1. You can use perror() to print a human-readable description of errno to the console, or at least strerror() to get that description in a string buffer that you can then do whatever you want with.

But the most likely reason for WHY bind() is failing is that you are trying to bind() your server to port 13. On most systems, ports 0-1023 are reserved for system services, so you would need to run your app with admin rights to listen on those ports.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...