Listeners!!!!!

This is a discussion on Listeners!!!!! within the Linux Programming forums, part of the Platform Specific Boards category; Hi: I am looking for a way to set up a listener on a linux machine that will listen to ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    19

    Exclamation Listeners!!!!!

    Hi:

    I am looking for a way to set up a listener on a linux machine that will listen to a port, such as port 8000, for incoming communication, and then execute a .bat file if the incoming communication indicates to do so.

    This program would have to run from the startup of the machine till shutdown and would require to be as efficient as possible because one of the commands in the .bat file would be to run another C program(which gets very complicated to explain so please trust me on the part that the second C program needs to be run by the .bat file)

    OS: Linux Kernel

    Code examples, if possible, would be greatly appreciated as well as any suggestions or solutions.

    Note: I know that the .bat should be .sh on linux, just force of habbit to use .bat, but they work

    Thanks muchly

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I haven't learned networking, but my guess is that linux has its own way of accessing ports so you may want to move your question over to the linux board.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    One way you could do this is to implement a socket server that listens on that specified port and then based on the input it receives take appropriate action by executing the .bat file which could in turn execute the other C program.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    19

    Exclamation Reponse to tvenki

    You are thinking exactly or very close to the same thing I was thinking of in my orginal message. I know approximately what I need to do, I just am lacking in the department of how to do it. If you have any suggestions please let me know.

    All suggestions are greatly appreciated

    Thanks

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    19

    Exclamation Beej

    Ok I spent some time and looked at Beej and I went from somewhat confused to totally confused on a number of points.

    1. When some of the methods are called the variables used are sometimes not declared in his examples, which is confusing the heck out of me.

    2. It only shows starting the listener by the method listen(), but my confusion is that after you declare how long does it listen before it stops?

    3. In order to recieve, which I am assuming would be in the same program and after the listener is started, does it continue looking for something to recieve before it moves on, or do I need to put it into some sort of loop?

    4. Then, changing the topic a bit, how can I get C to run a .sh file when I recieve the appropiate information?

    5. Even farther off topic now but still very much needed. Can I compile a C program in windows and then transfer it over to a linux machine to run? Reason for this is because the linux machine I have doesn't have enough memory for a compiler.

    Any suggestions or solutions are greatly appreciated.

    Thanks

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    19

    Talking Forget the before mentioned stuff, need help on easier topic

    I think I have found a solution, below is a copy of the code I am using if anyone is interested. Now I am stuck with one very huge problem.

    [B]Does anyone know where I can find a C compiler?[\B]

    I know it is not one of the harder problems, but as some may have guessed, I am fairly new to the world of C, and what I do know was picked up in three days of frantic programming about a month ago. Amazingly it worked but back then I had a C compiler available, I do not have one now.

    My OS is Linux Kernel or WinXP, I would prefer to compile on my WindowXP machine, due to memory issues, my linux machine only has 150MB of memory left on it from a 256MB harddrive. I know it's small. Unfortunately I am not sure if it matters what OS you compile on in C, I know in other languages it doesn't matter but I thought I'd check first.

    Thanks for all your assistance, it is greatly appreciated!!

    [CODE]
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>

    int main()
    {
    int sockfd, newsockfd, clilen, n;
    int portno = 8010;
    char buffer[256];
    struct sockaddr_in serv_addr,cli_addr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
    error("ERROR opening socket");
    }
    bzero((char *) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(portno);
    serv_addr.sin_addr.s_addr = INADDR_ANY;

    if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    {
    error("ERROR on binding");
    }
    while(1==1)
    {
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
    if(newsockfd < 0)
    {
    error("ERROR on accept");
    }
    bzero(buffer,256);
    n = read(newscokfd, buffer, 255);
    if(n < 0)
    {
    error("ERROR reading from socket");
    }
    else
    {
    system("./RunFile.sh");
    n = write(newsockfd,"Success",7);
    if(n < 0)
    {
    error("ERROR writing to socket");
    }
    }
    }
    return 0;
    }
    [\CODE]

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    1) go to a computer shop and buy a second hand harddrive. 4GB would be more than adequate for a decent linux install and this would cost very little.

    or

    dual boot your windows box if it has enough hard drive space.

    2) install linux with gcc - the c compiler.

    3) take time to read beej (or other network programming book) and understand by example.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Saravanan.T.S.
    Beginner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templates and listeners
    By okinrus in forum C++ Programming
    Replies: 1
    Last Post: 06-09-2004, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21