View Full Version : Listeners!!!!!

08-11-2003, 01:34 PM

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:confused:

08-11-2003, 03:48 PM
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.

08-11-2003, 05:01 PM
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.

08-11-2003, 07:30 PM
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


08-12-2003, 03:35 AM
Beej (http://www.ecst.csuchico.edu/~beej/guide/net/)

08-12-2003, 11:45 AM
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.


08-12-2003, 08:39 PM
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!!

#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");
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd < 0)
error("ERROR on accept");
n = read(newscokfd, buffer, 255);
if(n < 0)
error("ERROR reading from socket");
n = write(newsockfd,"Success",7);
if(n < 0)
error("ERROR writing to socket");
return 0;

08-13-2003, 02:30 AM
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.


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.

08-13-2003, 03:28 AM
goto www.gametutorials.com