Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is the reference manual code to construct socket address endpoints
struct sockaddr_in {
               sa_family_t    sin_family; /* address family: AF_INET */
               in_port_t      sin_port;   /* port in network byte order */
               struct in_addr sin_addr;   /* internet address */
           };

           /* Internet address. */
           struct in_addr {
               uint32_t       s_addr;     /* address in network byte order */
           };


however i need to code this standard blueprint; into a program from linux page
ip(7) - Linux manual page[^]



Now i will share my code and discuss what was misunderstood.

struct sockaddr_in clntsock
{
clntsock.sin_family = AF_INET
clntsock.sin_port = htons (9735)
clntsock.sin_addr = INADDR_ANY

}

the first thing i misunderstood or coded incorrectly was the clntsock function pointed to structure passing the assemly through a roulette.

What information must i place there to make it work "clntsock"?
was i missing parentheses or something?
i stillg et error od debugger there that im missing some { < ( indicatior or function


Mainly why is the original code template missing the left side structures?
does adding "clntsock" recplace them?
or how do i write the code with both so i can completely fill this tempalte?

i just dont understand why im getting syntax errors writing this simple strucutre code

what must i do to fill in these pieces if im incorrecly coding them wrong?

What I have tried:

I tried both left to right methods
but i nver was able to combine them
what method can i code them correctly?
Posted
Updated 17-Jul-20 6:08am

1 solution

For one thing, it should be
C
clntsock.sin_addr.s_addr = hton(INADDR_ANY);


Here's a function that will create a socket that listens for connections to get you going:
C
int init_socket_on_port(unsigned short port)
{
    struct sockaddr_in serv_addr;
    const int one = 1;
    int sockfd;
    if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket()");
        return -1;
    }

    memset( &serv_addr, 0, sizeof serv_addr);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(port);
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);

    if( bind(sockfd, (struct sockaddr *) &serv_addr, sizeof serv_addr) < 0)
    {
        perror("bind");
        close(sockfd);
        return -1;
    }

    listen(sockfd, 1);
    printf("listening ... on socket \n");
    return sockfd;
}


You'll probably want to change the error handling,as perror() may not be the right thing, and you'll still need to call accept() on the returned socket.
 
Share this answer
 
v2

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