Hello everyone!
I made a small code to observe if my ftp port is up.
At first i compiled and runned under ubuntu and acted as ecxpected.
But when i compiled it cross it behaves not as ecxpected?
Why?
Here is my code:
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#define MAXBUF 1024
#define PORT 21
#define HOST "127.0.0.1"
main() {
pid_t pid, sid;
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
} else if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
loop(); // Firing up the loop
exit(EXIT_SUCCESS);
}
loop() {
while (1) { // The loop
sleep(5);
hostcheck();
sleep(5);
}
}
hostcheck() // Anybody home?
{ int sockfd, bytes_read;
struct sockaddr_in dest;
char buffer[MAXBUF];
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
system("logger Error opening Socket !!\n");
}
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
if ( inet_addr(HOST, &dest.sin_addr.s_addr) == 0 )
{
system("logger Error getting Host !!\n");
}
dest.sin_port = htons(PORT);
if ( connect(sockfd, (struct sockaddr *)&dest, sizeof(dest)) != 0 )
{
system("logger Server is offline !!\n");
somefunction
}
else {
system("logger Server is online !!\n");
somefunction
}
close(sockfd);
return 0;
}
I know it is a little bit lame but in Ubuntu it does the Job!
The c# Makefile: ( its from Helloworld)
# build helloworld executable when user executes "make"
ftpwatch: ftpwatch.o
$(CC) $(LDFLAGS) ftpwatch.o -o ftpwatch
ftpwatch.o: ftpwatch.c
$(CC) $(CFLAGS) -c ftpwatch.c
# remove object files and executable when user executes "make clean"
clean:
rm *.o ftpwatch
In ubuntu it sucessfulle checks if the router port 21 ist up.
Several time is stoped and restartet ftp and allways there was the right output an action of corse!
For mor testing the action has taken out for easier reading :]]
In openwrt it allways say server online!
Any Ideas on this?
Thanks!
(Last edited by derdigge on 3 Nov 2012, 05:40)