Simple ncurses Client

From: Materialised (materialised_at_privacy.net)
Date: 03/27/04


Date: Sat, 27 Mar 2004 20:31:31 +0000

Hi everyone,

I am having real difficulty, I have tried the ncurses mailing list but
it seems they are still down at the moment, since gnu.org got cracked.

My problem is as follows, for simplicitys sake I have made it simpler
than it actually is, although the principle should still remain the same.
I have a simple echo server, the user connects to the server, types a
string and the server echos the message back. Here is the code

void str_echo(int);

int main(void)
{
        int connfd, listenfd, yes =1;
        pid_t childpid;
        socklen_t clilen;
        struct sockaddr_in cliaddr, servaddr;

        if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {
                perror("socket");
                exit(1);
        }
        if (setsockopt(listenfd ,SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))
== -1) {
                perror("setsockopt");
                exit(1);
        }
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(9000);

        if(bind(listenfd, (SA *) &servaddr, sizeof(servaddr)) == -1) {
                perror("bind");
                exit(1);
        }
        if(listen(listenfd, 10) == -1) {
                perror("listen");
                exit(1);
        }

        for(; ;) {
                clilen = sizeof(cliaddr);
                if((connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) == -1) {
                        perror("accept");
                        exit(1);
                }
                printf("server: got connection from %s\n", inet_ntoa(cliaddr.sin_addr));
                if((childpid = fork()) == 0) {
                        close(listenfd);
                        str_echo(connfd);
                        exit(0);
                }
                close(connfd);
        }
        return 0;
}

void str_echo(int sockfd)
{
        ssize_t n;
        char buf[1000];
        int len;
        for( ; ;) {
                while((n = read(sockfd, buf, 1000)) > 0) {
                        len = strlen(buf);
                        if (send(sockfd, buf, len, 0) == -1)
                                perror("send");
                }
                if(n < 0 && errno == EINTR)
                        continue;
                else if (n < 0){
                        perror("str_echo");
                        exit(1);
                }
        }
}
(There are a few of my own defines in there, but you get the general drift)
It isnt the server which is the problem, I wish to write a client, using
curses, which will allow the user to enter data from the command line,
and also recieve data from the server, without being blocked. I have
tried using none blocking sockets, using fork(), and using select(), but
still I cant figure it out.
This is what I have done so far, (it doesnt work)
http://codegurus.org/~mick/source.html

-=-=-
... xSilent_BoBx wrote: Guys only have enough blood to power either the
brain or the penis but not both at the same time. That's why guys turn
into retards

Could someone give me some examples of what I am looking for?

Thanks
Mick