PDA

View Full Version : char * problem



eleanor
5th July 2008, 12:42
Hi:

This is how I get info from the server. The server sends it in "struct risp" format

void MainWindow::getSysStat() {
/* create the system info strusture that will hold information about the server */
sysInfo = (struct risp *)malloc(sizeof(struct risp));
//sysInfo->running_kernel = (char *)malloc(sizeof(char) * LONGEST_KERNEL_NAME);
//sysInfo->fsStat->name = (char *)malloc(sizeof(char) * LONGEST_NAME);
//sysInfo->fsStat->devName = (char *)malloc(sizeof(char) * LONGEST_NAME);

/* wait until the socket gets connected */
if(socket->waitForConnected(-1)) {
/* wait until data gets available */
if(socket->waitForReadyRead(-1)) {
/* read from the socket */
char *dataRead = (char *)malloc(sizeof(char)*sizeof(struct risp));
if((socket->read(dataRead, sizeof(struct risp))) == -1)
qDebug() << "Error reading data from server";

/* cast the data to match the risp structure */
sysInfo = (struct risp *)dataRead;
//qDebug() << "total cpu is: " << sysInfo->total_cpu;
}
}

}

Here is struct risp:


struct risp {
/* cpu info */
unsigned long cpu_usage; /* /proc/ */
unsigned long total_cpu; /* /proc/cpuinfo */
//char proc_name[LONGEST_PROC_NAME]; /* /proc/ */
//char *cpu_processes[NUMBER_OF_PROCESSES]; /* /proc/ */

/* ram info */
unsigned int ram_usage; /* /proc/meminfo */
unsigned int total_ram; /* /proc/meminfo */
//char *ram_processes[NUMBER_OF_PROCESSES]; /* /proc/ */

/* swap info */
unsigned int swap_usage; /* /proc/meminfo */
unsigned int total_swap; /* /proc/swaps */

/* filesystems */
struct fsMountList *fsStat;
unsigned short int numFs;

/* kernel info */
char *running_kernel; /* /proc/ */

float uptime; /* /proc/uptime -> the first number is the uptime in seconds */
unsigned short int num_users; /* /proc */
unsigned short int num_processes;
unsigned short int num_running_processes; /* /proc/stat -> procs_running */
char arips_included; /* 0/1 */

};


The problem is this:


qDebug("Kernel: %s", sysInfo->running_kernel);
it displays noting like ti should: it should display: "2.5.25"

So does anybody see what's the problem ?

caduel
5th July 2008, 15:06
i) readyRead just means some data is available...
you have to wait til enough data has been read.
Insert some debug statements that print the number of bytes read.
Probably the number is lower than the size of your struct.

There are examples on how to do that in the Qt docs.

ii) you must not transmit POINTERS over tcp/ip.
The pointer value points to some string on the server side.
This pointer value is MEANINGLESS on the client side.
(It is undef. behaviour to dereference it. Usually your program will crash.)

HTH