- 论坛徽章:
- 0
|
本代理程序的目的想实现 接受远程请求,启动网元连接程序vcit,然后传递远程任务和接收网元返回报告。
采用双管道的方式,但是fork的子进程 在执行execl出错:execl error: No such file or directory,
该代码片断开始是能执行的, 加入socket之后就不行了,请高人帮忙,指点迷津!
// File: prg6_1.c
#include <stdio.h> /* These are the usual header files */
#include <strings.h> /* for bzero() */
#include <unistd.h> /* for close() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 2006 /* Port that will be opened */
#define BACKLOG 5 /* Number of allowed connections */
#define MAXDATASIZE 1000
#define MAXLINE 256
void process_cli(int connectfd, struct sockaddr_in client)
{
int num;
int i,n;
char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE];
char line[MAXLINE];
int fd1[2],fd2[2];
int vcitopened = -1;
pid_t pid;
printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) ); /* prints client's IP */
while (num = recv(connectfd, recvbuf, MAXDATASIZE,0)) {
recvbuf[num] = '\0';
printf("Received client message: %s",recvbuf);
if (strncmp(recvbuf, "CLOSE", 5) == 0)
{
if (vcitopened == 1)
{
if (!fd1[1])
{
write(fd1[1],"quit",4);
close(fd1[0]);
close(fd1[1]);
close(fd2[0]);
close(fd2[1]);
}
strcpy(sendbuf,"connection closed.");
send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
vcitopened = -1;
}
}
else
if (strncmp(recvbuf, "OPEN", 4) == 0)
{
if(vcitopened == 1)
{
strcpy(sendbuf, "close pre connection first!");
send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
}
else
{
//open vcit
if (pipe(fd1) < 0 || pipe(fd2) < 0)
{
perror("pipe error\n");
exit(1);
}
if((pid = fork()) < 0)
{
perror("pipe error\n");
exit(1);
}
else if (pid > 0) /*parent*/
{
close(fd1[0]); /* pipe fd1: parent -> child */
close(fd2[1]); /* pipe fd2: child -> parent */
}
else
{
close(fd1[1]); /* pipe fd1: parent -> child */
close(fd2[0]); /* pipe fd2: child -> parent */
if(fd1[0] != STDIN_FILENO)
{
if(dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
{
perror("child: dup2 error");
exit(1);
}
close(fd1[0]);
}
if(fd2[1] != STDOUT_FILENO)
{
if(dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
{
perror("child: dup2 error");
exit(1);
}
close(fd2[1]);
}
//if( execl("./vcit" "vcit", (char *) 0) < 0)
if( execl("/bin/ls" "ls","-l", (char *) 0) < 0)
{
perror("child: execl error");
close(fd1[0]);
close(fd1[1]);
close(fd2[1]);
close(fd2[0]);
vcitopened = -1;
strcpy(sendbuf, "ne open failed!");
send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
exit(1);
}
else
vcitopened = 1;
} /* pid < 0 */
strcpy(sendbuf, "open ne connection!");
send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
//vcitopened = 1;
//recvbuf[0] = '\0';
}
}
else
{
if(vcitopened == -1)
{
strcpy(sendbuf, "open connection first!");
send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
}
else
{
if(pid > 0)
{
n = strlen(recvbuf);
if (write(fd1[1], recvbuf, n) != n)
{
perror("parent: write error!");
exit(1);
}
while((n = read(fd2[0], line, MAXLINE)) > 0)
send(connectfd, line, n , 0);
}
strcpy(sendbuf, "send cmd.");
send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
}
}
}
close(connectfd); /* close connectfd */
}
int main()
{
int listenfd, connectfd; /* socket descriptors */
struct sockaddr_in server; /* server's address information */
struct sockaddr_in client; /* client's address information */
int sin_size;
/* Create TCP socket */
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
/* handle exception */
perror("Creating socket failed.");
exit(1);
}
int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
/* handle exception */
perror("Bind error.");
exit(1);
}
if(listen(listenfd,BACKLOG) == -1){ /* calls listen() */
perror("listen() error\n");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while (1) {
if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {
perror("accept() error\n");
exit(1);
}
process_cli(connectfd, client);
}
close(connectfd);
close(listenfd); /* close listenfd */
return 0;
}
[ 本帖最后由 microchu 于 2006-8-14 11:42 编辑 ] |
|