|
Home > Archive > Unix Programming > December 2005 > help needed: remote application
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 |
help needed: remote application
|
|
| giannizzz 2005-12-17, 5:55 pm |
| Hi all,
thanks in advance for any suggestions you might want to give me.
I am trying to develop a client/server application in C/unix for an
academic course in operative systems.
The server should be a kind of deamon, providing access via internet to a
text-based application (i.e. bc).
What I managed to do so far is a kind of remote shell: I can connect to
the server that creates a child for any client connected and that redirect
the stdout and sterr to the socket.
After a fork I call execlp(buff,buff,0) where buff contains the command
sent by the client. It works fine if I cann commands like ls or ps, but
nothing happens if I try to call bc: the client remains stuck on the
recv().
I am probably having the wrong approach and I can't find the solution,
would you give me an hint?
I know that I should only use sockets and system calls to do this project,
the teacher never talked about pty...
Hope you'll give me some useful directions.
Thanks a lot
--
questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it
| |
| Bjorn Reese 2005-12-17, 5:55 pm |
| giannizzz wrote:
> sent by the client. It works fine if I cann commands like ls or ps, but
> nothing happens if I try to call bc: the client remains stuck on the
> recv().
If you run bc without any arguments then it waits for input from stdin.
--
mail1dotstofanetdotdk
| |
| giannizzz 2005-12-17, 5:55 pm |
| Bjorn Reese ha scritto:
> If you run bc without any arguments then it waits for input from stdin.
but when I run bc from the normal shell I get this:
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
I'd expect it to be redirected to the socket like i.e. the output of ls
and so be received by the client, but the client doesn't receive
anything...
how to make it do what I want?
this is the very simple code of the server function that handles the
session:
int handle_client(int newsd) {
char buff[MAXDIM+1];
int status,pid;
close(0);close(1);close(2);
if (dup(newsd) != 0 || dup(newsd) != 1 || dup(newsd) != 2)
perror("dup error");
while(1){
read(newsd, buff, MAXDIM);
if (strcmp(buff,"quit")==0) {
send(newsd,"session ended\n",MAXDIM,0);
return 0;}
if ((pid = fork())==0) { //child
execlp(buff,buff,0);
perror("exec");
exit(0);
}
else wait(&status); //parent
}
}
client loop:
while(1) {
scanf("%s",buff); //gets the command by
keyboard
send(ds_sock, buff, MAXDIM,0);
if (strcmp(buff, "quit")==0){
printf("disconnecting...\n");
exit(0);
}
bzero(&buff1, sizeof(buff1));
recv(ds_sock, buff1, sizeof(buff1),0);
printf("%s\n",buff1);
}
hope someone can help me, thanks
--
questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it
| |
| Pascal Bourguignon 2005-12-18, 2:49 am |
| nononono@email.tt (giannizzz) writes:
> Bjorn Reese ha scritto:
>
>
>
> but when I run bc from the normal shell I get this:
>
> bc 1.06
> Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
> This is free software with ABSOLUTELY NO WARRANTY.
> For details type `warranty'.
>
> I'd expect it to be redirected to the socket like i.e. the output of ls
> and so be received by the client, but the client doesn't receive
> anything...
>
> how to make it do what I want?
[pjb@thalassa pjb]$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2
3
[pjb@thalassa pjb]$ echo 1+2|bc
3
[pjb@thalassa pjb]$ (sleep 10; echo 1+2;sleep 5;echo 2*3)|bc
<...ten seconds...>
3
<...five seconds...>
6
[pjb@thalassa pjb]$
Do you really want to see the copyright warning?
bc detects when it's connected to a terminal and then show the
copyright, but when it's connected to a file or a socket, it doesn't,
assuming he's speaking to another program that's only interested in
arithmetics, not in legalities.
If you want to get the legalese, I guess you'll have to use ptys.
--
__Pascal Bourguignon__ http://www.informatimago.com/
The world will now reboot. don't bother saving your artefacts.
| |
| giannizzz 2005-12-18, 7:48 am |
| Pascal Bourguignon ha scritto:
> Do you really want to see the copyright warning?
well, no, it doesn't matter really...
so you mean I should do something to prevent the recv to block when I call
bc cause it is actually coming nothing from the server?
--
questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it
| |
| Pascal Bourguignon 2005-12-18, 5:57 pm |
| nonono@email.tt (giannizzz) writes:
> Pascal Bourguignon ha scritto:
>
>
> well, no, it doesn't matter really...
>
> so you mean I should do something to prevent the recv to block when I call
> bc cause it is actually coming nothing from the server?
Indeed, the non-interactive bc protocol is:
client bc
---------(connect)------->
---------(expression)---->
<------(result)-----------
---------(expression)---->
<------(result)-----------
...
---------(disconnect)---->
Now you could design your client-server interaction to not depend on
the order of the request and responses, using for example select(2) to
know when you can read or write to the socket.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Klingon function calls do not have "parameters" -- they have
"arguments" and they ALWAYS WIN THEM."
| |
| giannizzz 2005-12-26, 5:56 pm |
|
> Indeed, the non-interactive bc protocol is:
> client bc
> ---------(connect)------->
> ---------(expression)---->
> <------(result)-----------
> Now you could design your client-server interaction to not depend on
> the order of the request and responses, using for example select(2) to
> know when you can read or write to the socket.
thanks a lot for your help, using select is probably a good idea
...but I've got anothe problem: when bc is running on the server side and
I send something to be processed (i.e. an addition), the result is ALWAYS
a few lines saying "illegal character: ^@", I'm such a bad programmer...
no idea of why I get that... maybe someone in here does know why?
--
questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it
| |
| Bjorn Reese 2005-12-27, 7:49 am |
| giannizzz wrote:
> ..but I've got anothe problem: when bc is running on the server side and
> I send something to be processed (i.e. an addition), the result is ALWAYS
> a few lines saying "illegal character: ^@", I'm such a bad programmer...
> no idea of why I get that... maybe someone in here does know why?
Strings in C are represented by a number of characters followed by a
terminating zero (ASCII value 0). If you pass this terminating zero to
bc, it will complain about it. For example, 'echo "1+1\0" | bc' will
produce the warning you describe.
The problematic line in your example is this:
send(ds_sock, buff, MAXDIM,0);
It will send all the bytes in the buffer, regardless of how many were
written by the preceeding scanf(). Try to change the third argument
from MAXDIM to strlen(buff)+1. I did not look more thoroughly at your
example, so you have to verify yourself that the child reads and handles
the string appropriately.
--
mail1dotstofanetdotdk
|
|
|
|
|