www.pudn.com > tcp_romote_mp.rar > tcpserver.c, change:2009-11-01,size:3567b


#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<string.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h> 
#include <sys/types.h> 
#include <signal.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <sys/wait.h> 

#define PORT 8900
#define BUF_SIZE 2048

int execute(char* command,char* buf);

void sig_child(int signo)
{
	pid_t pid;
	int stat,i;
	while((pid = waitpid(-1,&stat,WNOHANG))>0);
	return;
}

int main()
{
	struct sockaddr_in client;
	struct sockaddr_in server;
	char send_buf[BUF_SIZE];
	char recv_buf[BUF_SIZE];
	char cmd[2048];
	int listend;
	int opt;
	int connectd;
	int sendnum;
	int recvnum;
	int length;
	int port;
	int pid;
	
	memset(send_buf,0,2048);
	memset(recv_buf,0,2048);

	port = PORT;
	opt = SO_REUSEADDR;

	signal(SIGCHLD,sig_child);

	if (-1==(listend=socket(AF_INET,SOCK_STREAM,0)))
	{
		perror("creat listen socket error\n");
		exit(1);
	}	

	setsockopt(listend,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); 

	#ifdef DEBUG
	printf("the listen id is %d\n",listend);
	#endif
	
	memset(&server,0,sizeof(struct sockaddr_in));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = htonl(INADDR_ANY);
	server.sin_port = htons(port);

	if (-1==bind(listend,(struct sockaddr*)&server,sizeof(struct sockaddr)))
	{
		perror("binding error\n");
		close(listend);
		exit(1);

	}

	if (-1==listen(listend,5))
	{
		perror("listen error\n");
		exit(1);
	}
	
	length = sizeof(struct sockaddr_in);

	while(1)
	{
		if (-1==(connectd=accept(listend,(struct sockaddr*)&client,&length)))
		{
			perror("creat connect socket error\n");
			continue;
		}
	
	   	#ifdef DEBUG
			printf("the connect id is %d",connect);
			printf("the client ip addr is %s",inet_ntoa(client.sin_addr));
	   	#endif

		memset(recv_buf,0,2048);
		memset(send_buf,0,2048);

		pid = fork();
		if(fork()>0)
		{
			close(connectd);
			continue;
		}
		if(fork()<0)
		{
			printf("error in fork!");
			break;
		}
		if(fork()==0) 
		{ 
			close(listend); 
			while(1)
			{
				if(0>(recvnum = recv(connectd,recv_buf,sizeof(recv_buf),0)))
				{
					perror("error in recving data...\n");
					close(connectd);
					continue;
				}	
				recv_buf[recvnum]='\0';
		
				if (0==strcmp(recv_buf,"quit"))
				{
					perror("quitting remote controling\n");
					close(connectd);
					break;
				}
				printf("the message is: %s\n",recv_buf);

				strcpy(cmd,"/bin/");
				strcat(cmd,recv_buf);
				execute(cmd,send_buf);

				if ('\0'==*send_buf)
				{
					memset(cmd,0,sizeof(cmd));
					strcpy(cmd,"/sbin/");
					strcat(cmd,recv_buf);
					execute(cmd,send_buf);
			
					if ('\0'==*send_buf)
					{	
						memset(cmd,0,sizeof(cmd));
						strcpy(cmd,"/usr/bin/");
						strcat(cmd,recv_buf);
						execute(cmd,send_buf);
					}
				
					if ('\0'==*send_buf)
					{	
						memset(cmd,0,sizeof(cmd));
						strcpy(cmd,"/usr/sbin/");
						strcat(cmd,recv_buf);
						execute(cmd,send_buf);
					}
				}
				if ('\0'==*send_buf)
					sprintf(send_buf,"command is not vaild,check it please\n");

				printf("the server message is:%s\n",send_buf);

				if (0>send(connectd,send_buf,sizeof(send_buf),0))
				{
					perror("sending data error\n");
					close(connectd);
					continue;
				}
				close(connectd);
				exit(1);
			}
			close(listend);
			return 0;
		} 
	}
}
int execute(char* command,char* buf)
{
	FILE * 	fp;
	int count;

	if (NULL==(fp = popen(command,"r")))
	{
		perror("creating pipe error\n"); 
		exit(1);

	}
	
	count = 0 ;

	while(((buf[count] = fgetc(fp))!=EOF)&&count<2047)
	count++;
	buf[count]='\0';

	pclose(fp);
	return count;
	
}