我正在编写一个简单的服务器守护进程,并与/etc/init.d/server一起运行,它运行正常,但当我想用system()函数运行一个简单的QT GUI时,它无法运行它并返回256作为返回码。
如果同样的守护进程I正从终端运行,那么它工作正常,system()函数也成功,返回值为0,并且GUI弹出。
我不明白的问题是什么……plzzzz任何人都能帮助我..........
我正在使用ubuntu-9.10,下面是代码……
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
int main()
{
//local variables
int sockfd,clifd,ret;
int client_flag = 0;
int server_flag = 1;
struct sockaddr_in server_addr,client_addr;
socklen_t client_len ;
int server_len;
daemon(0, 0);
/* open log file */
setlogmask(LOG_UPTO(LOG_INFO));
openlog("server:", LOG_CONS | LOG_PID, LOG_LOCAL2);
//creating the socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
perror("Error socket creation:");
return sockfd;
}
//filling the socket address detail
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8181);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_len = sizeof(server_addr);
//binding the socket
ret = bind(sockfd, (struct sockaddr *)&server_addr,server_len);
if(ret < 0){
perror("Error in bind");
return ret;
}
//creating the listening queue
ret = listen(sockfd, 10);
if(ret < 0){
perror("Error in listen");
return ret;
}
while(1){
//accepting the connection from client
client_len = sizeof(client_addr);
clifd = accept(sockfd, (struct sockaddr *)&client_addr, &client_len);
if(clifd < 0){
perror("Error in accept in server_init ");
return clifd;
}
client_flag = 0;
//reading the client_flag
ret = read(clifd, &client_flag,sizeof(client_flag));
if(ret < 0){
perror("Error in read");
return ret;
}
// if flag is true i want to run the GUI "console"
//console is Qt4 application
if(client_flag) {
syslog(LOG_NOTICE," *************** \n");
/* Here its returning 256 is if it is running from boot time and if after boot i will restrat it from terminal like "/etc/init.d/server restart" then it will return 0 success and GUI will popped up properly.*/
ret = system("/usr/sbin/test/console 1") ;
syslog(LOG_NOTICE," *************** %d\n",ret);
}
//writing the server_flag
ret = write(clifd, &server_flag,sizeof(server_flag));
if(ret < 0){
perror("Error in write");
return ret;
}
close(clifd);
}
closelog();
close(sockfd);
return 0;
}发布于 2010-01-25 20:54:22
请勿从引导运行图形应用程序。他们将无法访问X服务器。运行一个无头守护进程,并编写一个GUI应用程序来与其交互,该应用程序在GUI登录时启动。
发布于 2010-05-12 17:38:03
对于服务器端,使用QtCoreApplication。可能有一些现成的解决方案来支持基于Qt的守护进程:
http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Utilities/qtservice/
https://stackoverflow.com/questions/2132312
复制相似问题