将服务器修改为循环模式,并发服务器.doc

并发服务器

软 件 学 院信息安全产品开发实践实验八报告陈枝-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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值