并发服务器
软 件 学 院信息安全产品开发实践实验八报告陈枝-10431110342012-11-13
四 川 大 学 计 算 机 学 院、软 件 学 院
实 验 报 告
班级:__第 周
? 信息安全产品开发实践实验课时2实验项目并发服务器(二)实验时间第十周
2012年月日2012年月日实验目的 实现多线程服务器实验环境 ?X86,WindowsXP,VMware5.0,RedHat Linux 9.0实验内容(算法、程序、步骤和方法) 实验要求:
1 修改远程控制程序服务器程序,将其从循环模式或多进程模式修改为多线程模式
2实现一个多线程端口扫描程序:
要求:
能同时扫描5个IP地址;
针对每个iP地址,开设100个线程对其进行扫描;
如果端口打开,使用函数getservbyport获取其服务名,在屏幕上打印:IP port servername,如果是未知服务,则屏幕显示:ip port unkonown
实验一代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 8900
#define BUFSIZE 2048
struct ARG
{
int conn_sock;
struct sockaddr_in client;
};
int execute(char*command,char*buf)
{
FILE *fp;
int count;
char commandbuf[2056];
if ((NULL==command)||(NULL==buf))
{
perror("command or buf is empty\n");
return -1;
}
count =0;
memset(commandbuf,0,2056);
strcat(commandbuf,"sh -c ");
strcat(commandbuf,command);
fprintf(stderr,"the command is %s\n",commandbuf);
if (NULL==(fp=popen(commandbuf,"r")))
{
perror("create pipe error\n");
return -1;
}
while ((count<2047) && (EOF!=(buf[count++]=fgetc(fp))));
buf[count-1]='\0';
return count;
}
/*
int pthread_create(pthread_t * thread,pthread_attr_t * attr,void *(*start_routine)(void *),void *arg);
int pthread_exit(void *retval);
int pthread_cancel(pthread_t thread);
int pthread_join(pthread_t th,void **thread_return);
int pthread_detach(pthread_t th);
*/
void process_cli(int conn_sock,struct sockaddr_in client)
{
char sendbuf[BUFSIZE];
char recvbuf[BUFSIZE];
int sendnum;
int recvnum;
while(1)
{
memset(recvbuf,0,BUFSIZE);
memset(sendbuf,0,BUFSIZE);
if (0>=(recvnum=read(conn_sock,recvbuf,BUFSIZE)))
{
perror("the commucation error\n");
close(conn_sock);
return -1;
}
recvbuf[recvnum]='\0';
fprintf(stderr,"the command is:%s\n",recvbuf);
if (0==strcmp(recvbuf,"quit"))
{
f