我有一个小 C 脚本的问题,它应该作为服务器运行并为每条到达的消息启动一个弹出窗口。execl语法是正确的,因为如果我尝试一个小脚本
main() { execl(...); }
有用。
当我将它放入 while(1) 循环时,它不起作用。其他一切都在工作,比如printf
或字符串操作,但不是execl
. 即使我叉它也不起作用。我怎样才能让它工作?
我已经尝试过fork()
,但它也不起作用。
这是完整的服务器 C 代码。
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#define BUFLEN 512
#define PORT 9930
void diep(char *s) {
perror(s);
exit(1);
}
int main() {
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other), broadcastPermission;
char buf[100], zeni[BUFLEN];
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
broadcastPermission = 1;
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, sizeof(broadcastPermission)) < 0)
diep("setsockopt() failed");
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");
while (1) {
if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1) diep("recvfrom()");
//printf("Received packet from %s:%d\nData: %s\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
strcpy(zeni, "");
strcat(zeni, "zenity --warning --title Hack!! --text ");
strcat(zeni, buf);
printf("cmd: %s\n", zeni);
//system (zeni);
execl("/usr/bin/zenity", "/usr/bin/zenity", "--warning", "--title", "Warn!", "--text", buf, (char *) NULL);
}
close(s);
return 0;
}