Unix Programming - Simple ncurses Client

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > Simple ncurses Client





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Simple ncurses Client
Materialised

2004-03-27, 3:37 pm

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
Materialised

2004-03-27, 3:37 pm

Materialised wrote:
> 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

Sorry ignore the tag line, stupid tagzilla... lol


-=-=-
.... This tagline is identical to the one you are reading.
Unauthorized use of this post, materials, characters, images, sounds,
odors, severed limbs, noodles, wierd dreams, strange looking fruit,
oxygen, and certain parts of Jupiter are strictly forbidden. If I find
you violating, or molesting my property in any way, I will employ a pair
of burly convicts to find you, kidnap you, and perform god-awful sexual
experiments on you until you lose the ability to sound out vowels. I
don't know why you are still reading this, but by doing so you have
proven that you have far too much time on your hands, and you should go
plant a tree, or read a book or something
Thomas Dickey

2004-03-28, 7:34 am

In comp.unix.programmer Materialised <materialised@privacy.net> wrote:
> 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.


I see mail all the time.

> 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


There's no ncurses-related code in this example.

--
Thomas E. Dickey
http://invisible-island.net
ftp://invisible-island.net
Materialised

2004-03-28, 7:34 am

Thomas Dickey wrote:
> In comp.unix.programmer Materialised <materialised@privacy.net> wrote:
>
>
>
>
>
> I see mail all the time.
>
>
>
>
> There's no ncurses-related code in this example.
>

Look at the link.

-=-=-
.... Dedicated to Free Software
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com