LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2005, 12:55 PM   #1
j0hn_galt
LQ Newbie
 
Registered: Jun 2005
Location: Bangalore , India
Distribution: Fedora Core 4, Suse 10.1
Posts: 27

Rep: Reputation: 15
Running a chat listening process infinitly.


i have programmed a shell of my own(its called bombshell!). most of the normal commands are programmed in C with the man 2nd section system calls.

now i have also programmed a chat program.

it consists of:

a listen program (listen when executed waits for a msg)
a shout program ( syntax : shout <hostname> <msg>)

now the problem is:
1.i wish to run this listen program in the background everytime the shell is started
2. if the listen program is exhausted. that is it recieves a message and terminates then it should restart hence enabling the user to always recieve messages

hence the basic senario is as follows: two users at two computers ,connected ,and running my shell, should be forever able to chat from the command line itself.
 
Old 12-05-2005, 09:56 AM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 121Reputation: 121
You can mess around with starting the listener in a shell startup script, but that would be troublesome and not the best solution.

I'd either create a process that could me called from the inetd and spawn a listener whent he request on port X (your chat port) comes in.

Or

Create a daemon that listens on port X and spawns a chat program when a request comes in.
 
Old 12-05-2005, 01:00 PM   #3
j0hn_galt
LQ Newbie
 
Registered: Jun 2005
Location: Bangalore , India
Distribution: Fedora Core 4, Suse 10.1
Posts: 27

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by crabboy
I'd either create a process that could me called from the inetd and spawn a listener whent he request on port X (your chat port) comes in.

Or

Create a daemon that listens on port X and spawns a chat program when a request comes in.
hmm...seems something which im not familiar with. could you please explain me or give me a few links where i could learn up one of those options and implement it. its a project and i need to give an explanation for it if im questioned.

as of now my temporary solution is similar to what you described above- messy.
1.the listen program starts immediately on program entry
2.if a message comes in it is necessary for the user to send back a msg to the sender to continue "listening". this i've done by starting the process-listen once again in the function-shout ( shout is used to send messages)
3. so now it works more like the military style! wherein only if the last message sent is something like "over and out" then the user can stop messaging and a listen process is always running in the background thus ready for another transaction.

too damn messy! especially if a single person is chatting with several people!
 
Old 12-06-2005, 12:06 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,281

Rep: Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916Reputation: 916
you can look into 'getpid()' and 'waitpid'. you can have a loop that as soon as the current pid is done, respawn and capture that pid.

just a suggestion.
 
Old 12-06-2005, 01:07 PM   #5
j0hn_galt
LQ Newbie
 
Registered: Jun 2005
Location: Bangalore , India
Distribution: Fedora Core 4, Suse 10.1
Posts: 27

Original Poster
Rep: Reputation: 15
@schneidz- very very interesting....sounds too good to be true. but the problem is that im starting out with the whole fork and exec stuff and its really confusing me. could you help me out by explaining me more of that loop thingy you jus said. heres my code. this is how far i got:

Code:
int main(int argc, char **argv){
    int i,j;
    int returnval;

    switch(fork())
    {
            case 0:execl("/root/listen","listen &",(char *)0);break;



    }
...
....//remaining code for the shell which doesnt use fork. 
//rather jus a simple character match and switch
the problem arises here:
1. if i use a default: wait(&returnval) my shell simply doesnt start.
2. without the default:wait(&returnval) i can run my shell . the listen process runs in the background. but when i use the chat command:
shout localhost hello
the listen process responds by showing the output and exits the shell.

it has to stay within the shell..... and please...pretty please explain me that waitpid getpid loop thingy!
 
Old 12-06-2005, 09:44 PM   #6
j0hn_galt
LQ Newbie
 
Registered: Jun 2005
Location: Bangalore , India
Distribution: Fedora Core 4, Suse 10.1
Posts: 27

Original Poster
Rep: Reputation: 15
now this is a temporary solution which i've come up with. if someone could explain the getpid() function and how i can use it to compare it with the waitpid() function then i can get this thing moving in a loop.
Code:
int main(int argc, char **argv){
    int i,j;
    pid_t child_pid;

    switch(fork())
    {
            case 0:execl("/root/listen","listen &",(char *)0);break;
            default:waitpid(child_pid,(int *)0,WNOHANG);


    }
//...rest of code
//..
//..
}

//in the chat funtion
int shout(){
//..
//..
close(sockfd);//after the fuction is over
 switch(fork()) //starting a listen process again
    {
            case 0:execl("/root/listen","listen &",(char *)0);break;
            default:waitpid(child_pid,(int *)0,WNOHANG);


    }
}
as you can see i fork a listen process everytime the shout function is called. this is primarily because i'm testing this shell on a single machine. hence when i say :
Quote:
shout localhost hello
the listen function is exhausted . hence i gotta fork another one in the shout function.

i hate this solution. firstly i keep getting bind errors. sometimes the messages are not recieved and most important how will i test this on a network.

i need to implement schneidz's solution since it seems the simplest to me. but im getting stuck at capturing the child pid and checking if its done.

anyone?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting PID of running process in C/C++ laikos Programming 16 10-30-2015 06:16 PM
System Not Listening for port 23, telnet installed and running john_lotz Linux - Networking 7 02-05-2009 10:12 AM
Running process sonu1082 Linux - Software 2 11-25-2004 05:32 AM
Process not running LinuxRam Linux - Newbie 4 09-09-2004 12:45 AM
Postfix running but not listening on 25 pnh73 Linux - Networking 1 05-27-2004 12:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration