发信人: tsingxiao()
整理人: girlrong(1999-11-13 15:08:00), 站内信件
|
抛砖引玉 :-)
/* t_svr's LINUX editon, author: TsingXiao, 1999.07.12 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#define DEF_PORT 5556 /* default port */
#define BACKLOG 10 /* as default */
#define BUF_LEN 255 /* s_buf's length */
#define NO 1
#define YES 0
int ret_no; /* return number */
int port; /* Local listening port */
void print_usage();
void print_usage()
{
printf("Usage: svr [port-number/--help]\n");
printf("This small stuff is ONLY a toy! Can execute foreign commands. \n");
printf("You can specify port to listen on.\n");
printf("author: [email protected], 1999\n");
}
main(int argv, char* argc[])
{
int sockfd; /* local socket */
int cnt_fd; /* remote socket */
int sin_size;
struct sockaddr_in addr_in; /* local addr */
struct sockaddr_in r_addr_in; /* remote addr */
if (argv == 1) {
port = DEF_PORT; /* default port */
} else {
port = atoi(argc[1]);
if ((port <= 0) || (port > 99999)) {
port = DEF_PORT;
}
if (strstr(argc[1], "--help") != NULL) {
print_usage();
exit(0);
}
}
printf("Port: %d\n", port);
sockfd = socket(AF_INET, SOCK_STREAM, 0); /* do some error checking! */
addr_in.sin_family = AF_INET; /* host byte order */
addr_in.sin_port = htons(port); /* short, network byte order */
addr_in.sin_addr.s_addr = INADDR_ANY; /* auto-fill my IP */
/* use inet_addr("127.0.0.1") to specify local IP */
bzero(&(addr_in.sin_zero), 8); /* zero the rest of the struct */
ret_no = bind(sockfd, (struct sockaddr *)&addr_in, sizeof(struct sock addr));
if (ret_no < 0) {
printf("bind error, code: %d\nAbort!\n", errno);
exit(-1);
}
ret_no = listen(sockfd, BACKLOG);
if (ret_no < 0) {
printf("listen error, code: %d\nAbort!\n", errno);
exit(-1);
}
sin_size = sizeof(struct sockaddr_in);
while (1) { /* main loop */
if ((cnt_fd = accept(sockfd, (struct sockaddr *)&r_addr_in, &sin_siz e)) == -1) {
printf("accept error, code: %d\nContinue!\n", errno);
continue;
}
printf("Got connection from %s\n", inet_ntoa(r_addr_in.sin_addr));
if (!fork()) { /* this is the child process */
int quit; /* need close connection ? */
int c_ret_no; /* private return number */
fd_set readfds;
quit = NO;
while (quit) { /* select never timeout */
FD_ZERO(&readfds);
FD_SET(cnt_fd, &readfds);
select(cnt_fd + 1, &readfds, NULL, NULL, NULL);
if (FD_ISSET(cnt_fd, &readfds)) { /* remote client has send a msg, process it! */
char s_buf[BUF_LEN];
bzero(s_buf, BUF_LEN);
c_ret_no = recv(cnt_fd, s_buf, BUF_LEN, 0);
if (c_ret_no < 0) {
printf("recv error, code: %d\nContinue!\n", errno);
continue;
}
printf("\n-*-*-*-*-*-*-*-*-*-*-*-\nReceived: %s\n-*-*-*-*-*-*-*-* -*-*-*-\n", s_buf);
if (strstr(s_buf, "bye") != NULL) { /* close connection */
quit = YES;
continue;
}
c_ret_no = system(s_buf);
bzero(s_buf, BUF_LEN);
if (c_ret_no < 0) {
printf("Execute error!\n");
strcpy(s_buf, "Execute error!");
} else {
printf("Execute OK!\n");
strcpy(s_buf, "Execute OK!");
}
c_ret_no = send(cnt_fd, s_buf, strlen(s_buf), 0);
if (c_ret_no < 0) {
printf("send error, code: %d\nContinue!\n", errno);
continue;
} /* process end */
}
}
close(cnt_fd);
printf("Lose connection from %s\n", inet_ntoa(r_addr_in.sin_addr));
exit(0); /* child process exit */
}
close(cnt_fd);
while (waitpid(-1, NULL, WNOHANG) > 0); /* clean up child process */
}
close(sockfd);
}
-- 我
既不能达而兼善天下
只好穷而独善自身
青山处处 斯民如土矣……
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.96.243.173]
|
|