Receiving snmptraps in a C program - c

I want to write a C program which receives snmptraps sent from another machine and parses them.
I am able to do this from the command line, but have no idea on how to implement this in C.
I searched online and found most code was in ruby or the atleast C++ which had a traplistener class.
Is there any way to receive snmptraps from within a C program ?

One of the best library is to use net-snmp. It supports both C and C++ linkages.

Related

can I use simpy(Python simulation library) in C?

I was assigned project that make operation system scheduling program in C and show simulation result in university.
I want to use simpy so that find out how to use python in C.
but I'm not sure simpy work in C because it looks like python work in C, just low-level.
can I use simpy in C?

How to mix c and c++ form application in the same project?

I want a program that C program with a C++ form application using together in the same project.
for example:
When I clicked a button send entered text to C program. entered text to inside textBox in a C++ form app.
C program will save the text to computer with file operations.
so simply example:
textBox1="hello world"
button=clicked
string^ message = textBox->Text;
writerFunction(message);
void writerFunction(char m[50])
{
FILE *fp;
fp = fopen("text.txt","a");
fprintf(fp,"%s",m);
fclose(fp);
}
It looks like you are using C++ .NET (managed c++)
I'm guessing this from pointer operator. In standard c++
you would use '*' instead of '^'.
Please correct my if I'm wrong there.
Two options.
you are using managed c++ so you can call Win32 API but you will make your work harder. If you still intrested please check this link. It will get you started.
http://www.codeproject.com/Articles/9714/Win-API-C-to-NET
But I'd suggest you to use c++ .NET approach to save the string in file. Google will find many examples for you.
One to start:
http://msdn.microsoft.com/en-us/library/19czdak8.aspx
if you want to use old style c++ you can mix c and c++ without any problems. You will need to import libs to your project.
Libraries:
C
- stdio.h examples here (http://en.m.wikipedia.org/wiki/C_file_input/output)
C++
-ofstream: Stream class to write on files
-ifstream: Stream class to read from files
-fstream: Stream class to both read and write from/to files.
More details here: http://www.cplusplus.com/doc/tutorial/files/
Windows API
- you can use win32 API by importing header
Windows.h
Few examples here (http://www.daniweb.com/software-development/c/threads/31282/windows-api-functions-to-read-and-write-files-in-c)
Good luck. If you need more info please let me know.
The simplest way is just embed your C code in C++. It should work, unless any platform specific thing barrier you.
If you do need to have two (or more) programs and if they should run as two different processes in the OS, you should use a proper inter process communication technique. I know nothing about .Net stuff. However, you may use pipes, shared memory, memory mapped files and even sockets work too.
Or else, you can create a dynamic library based on C and call the function in your C++ form application.

Turning strings into code?

So let's say I have a string containing some code in C, predictably read from a file that has other things in it besides normal C code. How would I turn this string into code usable by the program? Do I have to write an entire interpreter, or is there a library that already does this for me? The code in question may call subroutines that I declared in my actual C file, so one that only accounts for stock C commands may not work.
Whoo. With C this is actually pretty hard.
You've basically got a couple of options:
interpret the code
To do this, you'll hae to write an interpreter, and interpreting C is a fairly hard problem. There have been C interpreters available in the past, but I haven't read about one recently. In any case, unless you reallY really need this, writing your own interpreter is a big project.
Googling does show a couple of open-source (partial) C interpreters, like picoc
compile and dynamically load
If you can capture the code and wrap it so it makes a syntactically complete C source file, then you can compile it into a C dynamically loadable library: a DLL in Windows, or a .so in more variants of UNIX. Then you could load the result at runtime.
Now, what normally would lead someone to do this is a need to be able to express some complicated scripting functions. Have you considered the possibility of using a different language? Python, Scheme (guile) and Lua are easily available to add as a scripting language to a C application.
C has nothing of this nature. That's because C is compiled, and the compiler needs to do a lot of building of the code before the code starts running (hence receives a string as input) that it can't really change on the fly that easily. Compiled languages have a rigidity to them while interpreted languages have a flexibility.
You're thinking of Perl, Python PHP etc. and so called "fourth generation languages." I'm sure there's a technical term in c.s. for this flexibility, but C doesn't have it. You'll need to switch to one of these languages (and give up performance) if you have a task that requires this sort of string use much. Check out Perl's /e flag with regexes, for instance.
In C, you'll need to design your application so you don't need to do this. This is generally quite doable, as for its non-OO-ness and other deficiencies many huge, complex applications run on well-written C just fine.

How to interface between C and gprolog?

I am in the somewhat unfortunate position of interfacing C and Prolog code. We have some data collection code in C, and some analysis code in Gnu-Prolog. So what is the best method to interface C and gprolog? I am currently trying to use the C library included in the gprolog package to call Prolog from C.
Note: I am working on ubuntu machines.
One of the problems I was facing was how to iterate over a list. I finally realized that though you could make a list out of n elements, you had to iterate over it in Prolog fashion - get the head and get the tail and recurse.
There's an entire chapter called Interfacing Prolog and C in the GNU-Prolog manual. I expect that you've seen this since you mention the manual in your comment, but since you seem to be asking for more information than what's given there, perhaps you could be more specific about where you're having trouble?

Call command line from C program

I am writing an command line application in C and from within the program, I would like to call other command-line applications. For example (and only as a clear example!), say the I would like to write a C app that calls the java compiler. For one, how would I do this? Is there a C function that directly calls the command line of something? For two, how would I take the normal text output that the java compiler displays and display it through the c program?
I'm sorry if this sounds a bit crazy, but I am very new to C and I have never done anything like this before. I would love to start writing a few of these utility style apps. If it matters any, I am currently running Windows, but would prefer a platform-independent method if possible, as I do use Linux from time to time.
You might look into system. I think you can use it in Windows as well as UNIX/Linux systems.

Resources