will
will

Reputation: 1407

Arm Development of a Client Server program

I have an old program from class that was a 1 client, 1 server written super basic 'chat' program in c, the code is below. I'm trying to compile it now for angstrom (running on an ARM chip), and am getting these errors. I installed the angstrom tool chain, do I need a special versions of sys/socket.h (or a different header, forgot which header the read/write/close functions were located in) etc that are made for arm?

Error:

~/Desktop/test $ arm-angstrom-linux-gnueabi-g++ main.c -o main -lnsl
main.c: In function 'int main()':
main.c:66: error: invalid conversion from 'int*' to 'socklen_t*'
main.c:66: error:   initializing argument 3 of 'int accept(int, sockaddr*, socklen_t*)'
main.c:75: error: 'read' was not declared in this scope
main.c:77: error: 'write' was not declared in this scope
main.c:79: error: 'close' was not declared in this scope
main.c:81: error: 'unlink' was not declared in this scope

I directly copied the code from the class, so the first error may just be something silly, but the last couple say they can't even find a lot of the functions I need...but then somehow it recognizes the accept command, and tells me I'm using the wrong arguments :/ Any ideas?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>  /* define socket */
#include <netinet/in.h>  /* define internet socket */
#include <netdb.h>       /* define internet socket */

#define SERVER_PORT 9999        /* define a server port number */

int main()
{
    int sd, ns, k;
    struct sockaddr_in server_addr = { AF_INET, htons( SERVER_PORT ) };
    struct sockaddr_in client_addr = { AF_INET };
    int client_len = sizeof( client_addr );
    char buf[512], *host;

    /* create a stream socket */
    if( ( sd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
    {
    perror( "server: socket failed" );
    exit( 1 );
    }

    /* bind the socket to an internet port */
    if( bind(sd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1 )
    {
    perror( "server: bind failed" );
    exit( 1 );
    }

    /* listen for clients */
    if( listen( sd, 1 ) == -1 )
    {
    perror( "server: listen failed" );
    exit( 1 );
    }

    printf("SERVER is listening for clients to establish a connection\n");

    if( ( ns = accept( sd, (struct sockaddr*)&client_addr,
                       &client_len ) ) == -1 )
    {
        perror( "server: accept failed" );
        exit( 1 );
    }

    printf("accept() successful.. a client has connected! waiting for a message\n");

    /* data transfer on connected socket ns */
    while( (k = read(ns, buf, sizeof(buf))) != 0)
    {    printf("SERVER RECEIVED: %s\n", buf);
         write(ns, buf, k);
    }
    close(ns);
    close(sd);
    unlink(server_addr.sin_addr);

    return(0);
}

Upvotes: 0

Views: 1315

Answers (1)

According to its man page read(2) (or type man 2 read on your terminal) you need

 #include <unistd.h>

I hope that your cross-development tool chain is configured well enough to find the right unistd.h header relevant to your target system. Ypu might pass -H to your cross-compiler to check what is included.

And I strongly suggest to pass -Wall to your [cross-] compiler (to get almost all warnings).

You should also take into account the accept(2) man page (notably the NOTES section).

Upvotes: 1

Related Questions