Problem in getting "log" using "UDPSocket" in a c++ program?!
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
initLogger: To initialize logging task in a process.
logger: To report various pieces of program running (including "function name" and "return value") in log file - that is initialized in "initlogger".
My project consists of two parts: a service daemon program and a shared object (so) library to do some functions using the service that is running in the background!
I call the "initlogger" two times:
First time, in the service program with "LOG_FILE" mode (initlogger(LOG_FILE)), so that the "log.txt" file is created and
then the "logger" function is called multiple times to write the logs into the "log.txt".
Second time, in one of the library functions with "LOG_UDP" mode (initlogger(LOG_UDP)) and then the "logger" function is called several times again.
Currently, I've run the service and the "log.txt" is created and it logs everything correctly;
I have a test program to call the library's functions (that use the service) and want to see the related logs,
Now, My question is that how should I see the library logs?!
You can send log messages all day long through a UDP socket, however if you do not have a peer application at the IP address ("127.0.0.1") and that is bound and willing to receive messages at port number (10000), then each and every one of the messages you send will end up in the "bit bucket". So unless you have some code that is calling recvFrom(), you have only completed 50% of the task of logging messages via UDP.
P.S. I hate being too critical of one's work, but aside from the usage of "new" in your code, the rest of it looks like C code, not C++.
You can send log messages all day long through a UDP socket, however if you do not have a peer application at the IP address ("127.0.0.1") and that is bound and willing to receive messages at port number (10000), then each and every one of the messages you send will end up in the "bit bucket". So unless you have some code that is calling recvFrom(), you have only completed 50% of the task of logging messages via UDP.
P.S. I hate being too critical of one's work, but aside from the usage of "new" in your code, the rest of it looks like C code, not C++.
Thanks,
Could you introduce me a peer application to recieve the messages and how to use it?!
Because I've not already have such experience, my search on finding such applications would not be well!
As to your post, I understood that I should write a peer code to logger to use "recvFrom" function in it, likely with IP and Port, and receive the logs through it!
Is my understanding true?!!
If not, could you elaborate more please?
I don't want to be involved with such programming (not enough time) and an easy way like using a software would be better!
I only want to debug the program using logs;
Earlier you provided a URL with information concerning the Practical Socket; once you have downloaded the .h and .cpp files, you should avert your eyes from the .cpp file, and instead focus on the .h file. The latter describes the API for using the UDP Socket class.
A typical UDP server, that uses the PracticalSocket, would perform steps like:
Code:
#include "PracticalSocket.h"
#include <string>
#include <iostream>
int main(int argc, char** argv)
{
unsigned short port = 10000;
try
{
UDPSocket socket(port);
for (;;) // forever...
{
char recvLogMsg[1024];
std::string fromAddr;
unsigned short fromPort;
int lenMsg = socket.recvMsg(recvLogMsg, sizeof(recvLogMsg) - 1, fromAddr, fromPort);
if (lenMsg > 0)
{
recvLogMsg[lenMsg] = '\0';
// do something with received message (ie. log it somewhere)...
}
else if (lenMsg < 0)
{
// error
}
}
}
catch (SocketException& se)
{
std::cerr << se.what() << std::endl;
return -1;
}
}
Last edited by dwhitney67; 01-21-2011 at 04:01 AM.
Earlier you provided a URL with information concerning the Practical Socket; once you have downloaded the .h and .cpp files, you should avert your eyes from the .cpp file, and instead focus on the .h file. The latter describes the API for using the UDP Socket class.
A typical UDP server, that uses the PracticalSocket, would perform steps like:
Code:
#include "PracticalSocket.h"
#include <string>
#include <iostream>
int main(int argc, char** argv)
{
unsigned short port = 10000;
try
{
UDPSocket socket(port);
for (;;) // forever...
{
char recvLogMsg[1024];
std::string fromAddr;
unsigned short fromPort;
int lenMsg = socket.recvMsg(recvLogMsg, sizeof(recvLogMsg) - 1, fromAddr, fromPort);
if (lenMsg > 0)
{
recvLogMsg[lenMsg] = '\0';
// do something with received message (ie. log it somewhere)...
}
else if (lenMsg < 0)
{
// error
}
}
}
catch (SocketException& se)
{
std::cerr << se.what() << std::endl;
return -1;
}
}
Hi and Thank you very much;
By building this program, I will have a process that listens to the received messages from logger program continuously and writes them in a file for example.
Now, I have another questions:
Beforehand, sorry for my clumsily questions;
I will send the log messages on the same computer (namely, 127.0.0.1) at port 10000.
But I don't know which value should I pass as the forth arqument of "socket.recvFrom()" function that is, "fromPort"?
(This question triggered because the lack of knowledge about network & socket programming, ports, and ... sorry!!)
I think I should pass "127.0.0.1" (the same computer) string to "fromAddr" parameter, but what about "fromPort" value?
I mean, how should I define a port number for logger program to be known to your provided program?
And the other question:
Apart from using this program and getting log on linux, I also have my project binaries (service & library)running on Windows (with Windows-specific APIs and ...) so I don't want to compile it again!
If I build your program, it will run on windows as an always running process and log the messages.
Also, I've downloaded many sofwares like "z-log.exe", "log2stats.exe", etc to learn using them with my logger program. Is it possible to change their mode to UDP and define an appropriate IP and Port to listen the sent messages from logger and inscribe them into their database?
So far, I've not used a log server software and I'm very eager to try it too...
I'll greatly appreciate you if share me your knowledge again.
I will send the log messages on the same computer (namely, 127.0.0.1) at port 10000.
But I don't know which value should I pass as the forth arqument of "socket.recvFrom()" function that is, "fromPort"?
(This question triggered because the lack of knowledge about network & socket programming, ports, and ... sorry!!)
I think I should pass "127.0.0.1" (the same computer) string to "fromAddr" parameter, but what about "fromPort" value?
I mean, how should I define a port number for logger program to be known to your provided program?
You don't need to specify the fromAddr and fromPort information when calling recvFrom(); that information is returned to you after the successful completion of the function call. The way I coded it in the example provided earlier is how you would set up the variables and the function call. Whether you decide to use the fromAddr/fromPort values that are returned is up to you.
Quote:
Originally Posted by jeremy28
And the other question:
Apart from using this program and getting log on linux, I also have my project binaries (service & library)running on Windows (with Windows-specific APIs and ...) so I don't want to compile it again!
If I build your program, it will run on windows as an always running process and log the messages.
Also, I've downloaded many sofwares like "z-log.exe", "log2stats.exe", etc to learn using them with my logger program. Is it possible to change their mode to UDP and define an appropriate IP and Port to listen the sent messages from logger and inscribe them into their database?
I cannot help with Windows, for I do not develop s/w for that platform, nor do I ever wish to.
If you are not able to develop the s/w applications on your own, you may want to consider hiring a freelance s/w developer. Just make sure that you provide the developer with the specific requirements for your project.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.