Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to make a small program in C language to whom when i run should Ping specific range of IP addresses on my LAN system.For example my range is from 192.168.0.41 to 192.168.0.59 so every
time if i want to check the number of users online i need to pickup and dial at lab or secondly if phone is busy i use to Ping each IP address and it is very hectic.So i want to make a program to whom when i run should ping whole series of 18 IP and it will tell that number of users alive
and dead.Secondly it will be more better if it also give me alive IP addresses.So please help me.Donot tell me to use IP Scanner i know many of them but i want to design my own small and simple.i donot need fency software i just need information about users. Please tell me how we can
use Dos commands of Ping ,netstat,ipconfig in Borland C.

Standard C libraries provide a function called system() that allows you to run shell commands. So you could say something like system("ping 192.168.0.41"); to ping one of the addresses.
I write this from memory so the syntax may be off.
Jeff

Actually, I think the API for running a program is CreateProcess(). However, there is a little bit of work involved. If you want to use system() it's easier, if not quite as flexible.

It's easy. For all systems you can use system(), exec*() (there are quite a few of them), spawn*() (also a few). For Win16 (Win 3.xx or earlier) you can use WinExec() and for Win32 you _should_ use CreateProcess(). Here are the differences:
system() is the simplest function to execute a shell command. You just pass a string, like you would enter it on the command line and system() executes it. If you want to have the output of, let's say, ping, you just change the stdout for that programm:
system("ping 192.168.0.1 > output.txt");exec*() and spawn*(): I don't know the exact syntax of them, since I've never used any one of them. They are much more flexible than exec(), you can, for example pass an array of command line arguments to programs you want to spawn.
WinExec() and LoadModule() are like exec*() and spawn*(), but they allow a greater control over the newly spawned program. Unlike the above functions, they're available under Windows only.
CreateProcess() finally is a hell of a lot more flexible than all other functions. It allows you the exactly control the spawned program. You can debug it, control standard input/output/error, check out, when the new process terminates and much more. This function is available under Win32 (Win95,98,ME,NT,2000,XP) only.
You must know, what's best for you, in the simplest case you would call system() and redirect the output to a file:
system("ping xxx > file.txt");
Then you would open that file.txt, it will contain the exact output of the ping command.

i am using Borland C++ version 3.1 In help of C++ i did not find any details about system() please guide me how to use it and what header file it requires

The system() call is as simple as the command line itself, here is the syntax:
int system(char *cmd);
cmd is a null-terminated string containing a command line command to be executed. You can pass anything, you would enter on the command line. The return value is the return value of your command processor (command.com in DOS, cmd.exe in WinNT and your shell program (bash, for example) in Linux). If the command fails, the return value is -1 and errno is set appropriately.
And as i said, you can redirect the output of called programs by adding a greater-than sign (>).
Example 1: Call netstat and exit
int main() {
system("netstat -ano");
return 0;
}Example 2: Ping local host and save results
int main() {
FILE *fl;system("ping 127.0.0.1 > temp.txt");
fl = fopen("temp.txt","rt");
if (fl) {
/* Process contents of temp.txt */
fclose(fl);
}
return 0;
}Remember, system() does not return, untel the command was executed.

I have installed Borland C in WinXP i tried ur programm in C but it did not work and gave following errors.It was compiled with out error but while running it ave error.
Does it matter that i run it in XP mode or i switchover to pure DOS mode in order to run this programme.
The file name is ping which i made.
*****************
Linking PING.EXE:
Linker Warning: No module definition file specified: using defaults
Linker Error: Undefined symbol _system in module PING.CPP
******************
thanks for ur kind help

![]() |
Disabling Ctrl+Alt+Del in...
|
C++ Error Spawning cl.exe
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |