远程终端管理系统
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
服务器端:
Lsd.h
#ifndef _LSD_H_
#define _LSD_H_
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define TYPE_LOGIN 0 //鐧诲綍鏁版嵁鍖? #define TYPE_REG 1 //娉ㄥ唽鏁版嵁鍖? #define TYPE_MSG 2 //娑堟伅鏁版嵁鍖? #define TYPE_HEART 3 //蹇冭烦鏁版嵁鍖? #define TYPE_CMD 4 //鍛戒护鏁版嵁鍖? //#define TYPE_END 5
struct packet
{
int data_type;
int size;
char *data;
};
struct client
{
int state;
char client_name[9];
char passwd[9];
struct client *next;
};
int check_login(char *str);
struct sockaddr_in ip_port();
void client_pthread(int newsock);
void heart(char *ipbuf);
void browse(int newsock, char *str);
struct client *creat_user(struct client *head);
void write_user(struct client *head);
void Free(struct client *head);
int login(int sockfd, char *str);
void regis_file(char *str);
void registion(int sockfd, char *str);
void writebuf(char *string);
struct packet unpack(int sockfd, struct packet server); void signal();
int execmd(const char *cmd, int newsock);
#endif
Server.c:
#include "lsd.h"
time_t newtime;
time_t oldtime;
struct client *head = NULL; //鍏ㄥ眬閾捐〃澶存寚閽?
/* 涓诲嚱鏁?*/
int main()
{
int sockfd = -1;
int newsock = -1;
int ret = -1;
int len;
char ipbuf[20];
char buf[100];
pthread_t tid;
struct sockaddr_in serveraddr;
struct sockaddr_in cliaddr;
head = creat_user(head); //寤虹珛鍏ㄥ眬閾捐〃
newtime = time(NULL);
oldtime = time(NULL);
/* 寤虹珛濂楁帴瀛?*/
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("socket");
exit(0);
}
/* 缁欏 鎺ュ瓧璧嬩簣鍦板潃鍜岀 鍙?*/
serveraddr = ip_port();
ret = bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (ret < 0)
{
perror("bind");
exit(0);
}
/* 缁欓摼鎺ユ帓闃?*/
ret = listen(sockfd, 5);
if (ret < 0)
{
perror("listen");
exit(1);
}
/* 鎺ュ彈缃戠粶閾炬帴*/
while (1)
{
len = sizeof(cliaddr);
newsock = accept(sockfd, (struct sockaddr *)&cliaddr, &len);
if (newsock < 0)
{
perror("accept");
exit(0);
}
/* 灏?杩涘埗缃戠粶鍦板潃杞 崲鎴愬瓧绗︿覆缃戣矾鍦板潃*/
inet_ntop(AF_INET, &cliaddr.sin_addr.s_addr, ipbuf, sizeof(ipbuf));
/* 鏈嶅姟鍣ㄦ樉绀哄苟鍐欏叆鏃ュ織鏂囦欢*/
time_t t;
time(&t);
char *tm = ctime(&t);
sprintf(buf, "IP:[%s] STATE: Connected TIME: %s", ipbuf, tm);
printf("%s", buf);
writebuf(buf);
ret = pthread_create(&tid, NULL, (void *)client_pthread, (void *)newsock);