2017-2018-1 20155230 《信息安全系统设计基础》第八周课上作业补全、课下作业
本周因电脑做到一半没电使第三次作业没有做完和提交,所以写博客补上。
1、Socket编程
基于socket 使用教材的csapp.h csapp.c,实现daytime(13)服务器(端口我们使用13+后三位学号)和客户端
服务器响应消息格式是
“
客户端IP:XXXX
服务器实现者学号:XXXXXXXX
当前时间: XX:XX:XX
”
上方提交代码
提交一个客户端至少查询三次时间的截图测试截图
提交至少两个客户端查询时间的截图测试截图
客户端
#include "csapp.h"
int main(int argc, char **argv)
{
int clientfd, port;
char *host, buf[MAXLINE];
rio_t rio;
if (argc != 3) {
fprintf(stderr, "usage: %s <host> <port>\n", argv[0]);
exit(0);
}
host = argv[1];
port = atoi(argv[2]);
clientfd = Open_clientfd(host, port);
Rio_readinitb(&rio, clientfd);
while (Fgets(buf, MAXLINE, stdin) != NULL) {
Rio_writen(clientfd, buf, strlen(buf));
Rio_readlineb(&rio, buf, MAXLINE);
Fputs(buf, stdout);
}
Close(clientfd);
exit(0);
}
服务器
#include <stdio.h>
#include <stdlib.h>
#include "csapp.h"
#include <time.h>
void echo(int connfd);
int main(int argc, char **argv)
{
int listenfd, connfd, port, clientlen;
struct sockaddr_in clientaddr;
struct hostent *hp;
char *haddrp;
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(0);
}
port = atoi(argv[1]);
listenfd = Open_listenfd(port);
while (1) {
clientlen = sizeof(clientaddr);
connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
/* determine the domain name and IP address of the client */
hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
haddrp = inet_ntoa(clientaddr.sin_addr);
printf("server connected to %s (%s)\n", hp->h_name, haddrp);
echo(connfd);
Close(connfd);
}
exit(0);
}
void echo(int connfd)
{
size_t n;
char buf[MAXLINE];
rio_t rio;
Rio_readinitb(&rio, connfd);
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) {
printf("客户端IP:127.0.0.1\n");
printf("服务器实现学号:20155230\n");
printf("server received %d bytes\n", n);
time_t t;
time(&t);
printf("当前时间:%s\n",ctime(&t));
Rio_writen(connfd, buf, n);
}
}
课下作业
1、课上作业3的Y86:
0x0000: | Disassembly of section .text:
|
0x0000: | 00000000 <bubble_p>:
0x0000: | 0: 56 push %esi
0x0000: | 1: 53 push %ebx
0x0000: | 2: 8b 44 24 10 mov 0x10(%esp),%eax
0x0000: | 6: 8b 54 24 0c mov 0xc(%esp),%edx
0x0000: | a: 8d 70 ff lea -0x1(%eax),%esi
0x0000: | d: 85 f6 test %esi,%esi
0x0000: | f: 7e 2d jle 3e <bubble_p+0x3e>
0x0000: | 11: 8d b4 26 00 00 00 00 lea 0x0(%esi,%eiz,1),%esi
0x0000: | 18: 31 c0 xor %eax,%eax
0x0000: | 1a: 8d b6 00 00 00 00 lea 0x0(%esi),%esi
0x0000: | 20: 8b 4c 82 04 mov 0x4(%edx,%eax,4),%ecx
0x0000: | 24: 8b 1c 82 mov (%edx,%eax,4),%ebx
0x0000: | 27: 39 d9 cmp %ebx,%ecx
0x0000: | 29: 7d 07 jge 32 <bubble_p+0x32>
0x0000: | 2b: 89 5c 82 04 mov %ebx,0x4(%edx,%eax,4)
0x0000: | 2f: 89 0c 82 mov %ecx,(%edx,%eax,4)
0x0000: | 32: 83 c0 01 add $0x1,%eax
0x0000: | 35: 39 f0 cmp %esi,%eax
0x0000: | 37: 7c e7 jl 20 <bubble_p+0x20>
0x0000: | 39: 83 ee 01 sub $0x1,%esi
0x0000: | 3c: 75 da jne 18 <bubble_p+0x18>
0x0000: | 3e: 5b pop %ebx
0x0000: | 3f: 5e pop %esi
|
0x0000: | Disassembly of section .text.startup:
|
0x0000: | 00000000 <main>:
0x0000: | 0: 31 c0 xor %eax,%eax
|
|
2、多线程实现课上3:
- 使用thread函数实现同时进行
while (1) {
connfdp = Malloc(sizeof(int));
*connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen);
Pthread_create(&tid, NULL, thread, connfdp);
}
void *thread(void *vargp)
{
int connfd = *((int *)vargp);
Pthread_detach(pthread_self());
Free(vargp);
echo(connfd);
Close(connfd);
return NULL;
}
3、多进程实现课上3:
- 使用fork函数增加进程
while (1) {
connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);
if (Fork() == 0) {
Close(listenfd); /* Child closes its listening socket */
echo(connfd); /* Child services client */
Close(connfd); /* Child closes connection with client */
exit(0); /* Child exits */
}
Close(connfd); /* Parent closes connected socket (important!) */
}