20155302 第八周课上补充及课下实践

2017-2018-1 20155302 《信息安全系统设计基础》第8周学习总结

课上内容补做

1.在Linux下完成“求命令行传入整数参数的和”测试代码传入自己的8位学号

在课堂提交的时候由于没有看清测试要求,于是随意测试了一组数据没有按要求测试学号,故补上课下截图。

实验代码:

#include<stdio.h>
#include<stdlib.h>
main(int argc,char *argv[])
{
	int sum=0,i;
	for(i=0;;i++)
	{
		if(argv[i]=='\0')
			break;
		sum=sum+atoi(argv[i]);
	}
printf("sum=%d",sum);
}

实验截图:

2.把第一个练习中的代码在X86-64(Ubuntu)中反汇编,给出汇编代码和机器码的截图,把X86-64汇编翻译成Y86-64汇编,并给出相应机器码的截图(使用附件中的Y86-64模拟器)

objdump来查看机器码

cat指令来查看汇编代码

安装y86模拟器过程:


y86截图:

3.基于socket 使用教材的csapp.h csapp.c,实现daytime(13)服务器(端口我们使用13+后三位学号)和客户端 服务器响应消息格式是“客户端IP:XXXX服务器实现者学号:XXXXXXXX当前时间: XX:XX:XX”

实验代码:
echoclient客户端:

/*
 * echoclient.c - An echo client
 */
/* $begin echoclientmain */
#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) {
	
	time_t t;
    struct tm * lt;
    size_t n; 
    printf("\n客户端IP:127.0.0.1\n");
    printf("服务器实现者学号:20155302\n");
    
    time (&t);//获取Unix时间戳
    lt = localtime (&t);//转为时间结构
    printf ("当前时间为:%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果。注意月份取值区间为[0,11],所以要+1;年份的修改。
	Rio_writen(clientfd, buf, strlen(buf));
	Rio_readlineb(&rio, buf, MAXLINE);
	Fputs(buf, stdout);
    }
    Close(clientfd); //line:netp:echoclient:close
    exit(0);
}
/* $end echoclientmain */

echoseveri服务器代码:

/* 
 * echoserveri.c - An iterative echo server 
 */ 
/* $begin echoserverimain */
#include "csapp.h"

void echo(int connfd, char *haddrp);

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, haddrp);
	Close(connfd);
    }
    exit(0);
}
/* $end echoserverimain */

echo.c:

/*
 * echo - read and echo text lines until client closes connection
 */
/* $begin echo */
#include "csapp.h"
#include <time.h>

void echo(int connfd, char *haddrp)
{
    time_t t;
    struct tm * lt;
    size_t n; 
    char buf[MAXLINE]; 
    rio_t rio;

    Rio_readinitb(&rio, connfd);
    while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { //line:netp:echo:eof
    //printf("server received %d bytes\n", n);
    printf("\n客户端IP:%s\n",haddrp);
    printf("服务器实现者学号:20155302\n");
    
    time (&t);//获取Unix时间戳
    lt = localtime (&t);//转为时间结构
    printf ("当前时间为:%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果。注意月份取值区间为[0,11],所以要+1;年份的修改。
    
	
	Rio_writen(connfd, buf, n);
    }
}
/* $end echo */

实验截图:

课下实践1

1 完成家庭作业4.47,4.48,4.49 2 相应代码反汇编成X86-64汇编3 把上述X86-64汇编翻译成Y86汇编,并给出相应机器码

c语言代码:

#include<stdio.h>

void bubble_a(long *data,long count)
{
	long i,last,t;
	for(last = count-1;last>0;last--)
	{
		for(i = 0;i<last;i++)
		{
			if(*(data+i+1)<*(data+i))
			{
				t=*(data+i+1);
				*(data+i+1)=*(data+i);
				*(data+i)=t;
			}
		}
	}
}
int main()
{
	int i;
	long data[10] = {2,3,4,5,6,7,8,9,0,1};
	bubble_a(data,10);
	for(i=0;i<10;i++)
	{
		printf("%d\n",*(data+i));
	}
	return 0;
}

汇编源代码:

bubble_a:
	pushq	%rbp
	movq	%rsp, %rbp
	movq	%rdi, -40(%rbp)
	movq	%rsi, -48(%rbp)
	movq	-48(%rbp), %rax
	subq	$1, %rax
	movq	%rax, -16(%rbp)
	jmp	.L2
.L6:
	movq	$0, -24(%rbp)
	jmp	.L3
.L5:
	movq	-24(%rbp), %rax
	addq	$1, %rax
	leaq	0(,%rax,8), %rdx
	movq	-40(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rdx
	movq	-24(%rbp), %rax
	leaq	0(,%rax,8), %rcx
	movq	-40(%rbp), %rax
	addq	%rcx, %rax
	movq	(%rax), %rax
	cmpq	%rax, %rdx
	jge	.L4
	movq	-24(%rbp), %rax
	addq	$1, %rax
	leaq	0(,%rax,8), %rdx
	movq	-40(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rax
	movq	%rax, -8(%rbp)
	movq	-24(%rbp), %rax
	addq	$1, %rax
	leaq	0(,%rax,8), %rdx
	movq	-40(%rbp), %rax
	addq	%rax, %rdx
	movq	-24(%rbp), %rax
	leaq	0(,%rax,8), %rcx
	movq	-40(%rbp), %rax
	addq	%rcx, %rax
	movq	(%rax), %rax
	movq	%rax, (%rdx)
	movq	-24(%rbp), %rax
	leaq	0(,%rax,8), %rdx
	movq	-40(%rbp), %rax
	addq	%rax, %rdx
	movq	-8(%rbp), %rax
	movq	%rax, (%rdx)
.L4:
	addq	$1, -24(%rbp)
.L3:
	movq	-24(%rbp), %rax
	cmpq	-16(%rbp), %rax
	jl	.L5
	subq	$1, -16(%rbp)
.L2:
	cmpq	$0, -16(%rbp)
	jg	.L6
	nop
	popq	%rbp
	ret
.LFE0:
.LC0:
main:
.LFB1:
	pushq	%rbp
	movq	%rsp, %rbp
	subq	$112, %rsp
	movq	%fs:40, %rax
	movq	%rax, -8(%rbp)
	xorl	%eax, %eax
	movq	$2, -96(%rbp)
	movq	$3, -88(%rbp)
	movq	$4, -80(%rbp)
	movq	$5, -72(%rbp)
	movq	$6, -64(%rbp)
	movq	$7, -56(%rbp)
	movq	$8, -48(%rbp)
	movq	$9, -40(%rbp)
	movq	$0, -32(%rbp)
	movq	$1, -24(%rbp)
	leaq	-96(%rbp), %rax
	movl	$10, %esi
	movq	%rax, %rdi
	call	bubble_a
	movl	$0, -100(%rbp)
	jmp	.L8
.L9:
	movl	-100(%rbp), %eax
	cltq
	leaq	0(,%rax,8), %rdx
	leaq	-96(%rbp), %rax
	addq	%rdx, %rax
	movq	(%rax), %rax
	movq	%rax, %rsi
	movl	$.LC0, %edi
	movl	$0, %eax
	call	printf
	addl	$1, -100(%rbp)
.L8:
	cmpl	$9, -100(%rbp)
	jle	.L9
	movl	$0, %eax
	movq	-8(%rbp), %rcx
	xorq	%fs:40, %rcx
	je	.L11
	call	__stack_chk_fail
.L11:
	leave
	ret
.LFE1:

y86:

全部y86代码:

0x000:                      | .pos 0
0x000: 30f40006000000000000 | 	irmovq stack,%rsp
0x00a: 803902000000000000   | 	call main
0x013: 00                   | 	halt
0x014:                      | bubble_a:
0x014: a05f                 | 	pushq	%rbp
0x016: 2045                 | 	rrmovq	%rsp, %rbp
0x018: 4075d8ffffffffffffff | 	rmmovq	%rdi, -40(%rbp)
0x022: 4065d0ffffffffffffff | 	rmmovq	%rsi, -48(%rbp)
0x02c: 5005d0ffffffffffffff | 	mrmovq	-48(%rbp), %rax
0x036: 30f80100000000000000 | 	irmovq  $1,%r8
0x040: 6180                 | 	subq	%r8, %rax
0x042: 4005f0ffffffffffffff | 	rmmovq	%rax, -16(%rbp)
0x04c: 701602000000000000   | 	jmp	L2
0x055:                      | L6:
0x055: 30f90000000000000000 | 	irmovq  $0,%r9
0x05f: 4095e8ffffffffffffff | 	rmmovq	%r9, -24(%rbp)
0x069: 70d501000000000000   | 	jmp	L3
0x072:                      | L5:
0x072: 5005e8ffffffffffffff | 	mrmovq	-24(%rbp), %rax
0x07c: 30fa0100000000000000 | 	irmovq  $1,%r10
0x086: 60a0                 | 	addq	%r10, %rax
0x088: 2008                 | 	rrmovq  %rax,%r8
0x08a: 6088                 | 	addq    %r8,%r8
0x08c: 6088                 | 	addq	%r8,%r8
0x08e: 6088                 | 	addq	%r8,%r8
0x090: 2082                 | 	rrmovq  %r8,%rdx
0x092: 5005d8ffffffffffffff | 	mrmovq	-40(%rbp), %rax
0x09c: 6020                 | 	addq	%rdx, %rax
0x09e: 50200000000000000000 | 	mrmovq	(%rax), %rdx
0x0a8: 5005e8ffffffffffffff | 	mrmovq	-24(%rbp), %rax
0x0b2: 2008                 | 	rrmovq  %rax,%r8
0x0b4: 6088                 | 	addq    %r8,%r8
0x0b6: 6088                 | 	addq    %r8,%r8
0x0b8: 6088                 | 	addq    %r8,%r8
0x0ba: 2081                 | 	rrmovq  %r8,%rcx
0x0bc: 5005d8ffffffffffffff | 	mrmovq	-40(%rbp), %rax
0x0c6: 6010                 | 	addq	%rcx, %rax
0x0c8: 50000000000000000000 | 	mrmovq	(%rax), %rax
0x0d2: 2009                 | 	rrmovq  %rax,%r9
0x0d4: 202a                 | 	rrmovq  %rdx,%r10
0x0d6: 619a                 | 	subq	%r9, %r10
0x0d8: 75b501000000000000   | 	jge	L4
0x0e1: 5005e8ffffffffffffff | 	mrmovq	-24(%rbp), %rax
0x0eb: 30f80100000000000000 | 	irmovq  $1,%r8
0x0f5: 6080                 | 	addq	%r8, %rax
0x0f7: 2008                 | 	rrmovq  %rax,%r8
0x0f9: 6088                 | 	addq    %r8,%r8
0x0fb: 6088                 | 	addq    %r8,%r8
0x0fd: 6088                 | 	addq    %r8,%r8
0x0ff: 2082                 | 	rrmovq  %r8,%rdx
0x101: 5005d8ffffffffffffff | 	mrmovq	-40(%rbp), %rax
0x10b: 6020                 | 	addq	%rdx, %rax
0x10d: 50000000000000000000 | 	mrmovq	(%rax), %rax
0x117: 4005f8ffffffffffffff | 	rmmovq	%rax, -8(%rbp)
0x121: 5005e8ffffffffffffff | 	mrmovq	-24(%rbp), %rax
0x12b: 30f80100000000000000 | 	irmovq  $1,%r8
0x135: 6080                 | 	addq	%r8, %rax
0x137: 2008                 | 	rrmovq  %rax,%r8
0x139: 6088                 | 	addq    %r8,%r8
0x13b: 6088                 | 	addq    %r8,%r8
0x13d: 6088                 | 	addq    %r8,%r8
0x13f: 2082                 | 	rrmovq  %r8,%rdx
0x141: 5005d8ffffffffffffff | 	mrmovq	-40(%rbp), %rax
0x14b: 6002                 | 	addq	%rax, %rdx
0x14d: 5005e8ffffffffffffff | 	mrmovq	-24(%rbp), %rax
0x157: 2008                 | 	rrmovq  %rax,%r8
0x159: 6088                 | 	addq    %r8,%r8
0x15b: 6088                 | 	addq    %r8,%r8
0x15d: 6088                 | 	addq    %r8,%r8
0x15f: 2081                 | 	rrmovq  %r8,%rcx
0x161: 5005d8ffffffffffffff | 	mrmovq	-40(%rbp), %rax
0x16b: 6010                 | 	addq	%rcx, %rax
0x16d: 50000000000000000000 | 	mrmovq	(%rax), %rax
0x177: 40020000000000000000 | 	rmmovq	%rax, (%rdx)
0x181: 5005e8ffffffffffffff | 	mrmovq	-24(%rbp), %rax
0x18b: 2008                 | 	rrmovq  %rax,%r8
0x18d: 6088                 | 	addq    %r8,%r8
0x18f: 6088                 | 	addq    %r8,%r8
0x191: 6088                 | 	addq    %r8,%r8
0x193: 2082                 | 	rrmovq  %r8,%rdx
0x195: 5005d8ffffffffffffff | 	mrmovq	-40(%rbp), %rax
0x19f: 6002                 | 	addq	%rax, %rdx
0x1a1: 5005f8ffffffffffffff | 	mrmovq	-8(%rbp), %rax
0x1ab: 40020000000000000000 | 	rmmovq	%rax, (%rdx)
0x1b5:                      | L4:
0x1b5: 30f90100000000000000 | 	irmovq  $1,%r9
0x1bf: 50a5e8ffffffffffffff | 	mrmovq  -24(%rbp),%r10
0x1c9: 609a                 | 	addq	%r9,%r10
0x1cb: 40a5e8ffffffffffffff |     rmmovq  %r10,-24(%rbp)
0x1d5:                      | L3:
0x1d5: 5005e8ffffffffffffff | 	mrmovq	-24(%rbp), %rax
0x1df: 5085f0ffffffffffffff | 	mrmovq  -16(%rbp),%r8
0x1e9: 2009                 | 	rrmovq  %rax,%r9
0x1eb: 6189                 | 	subq	%r8,%r9
0x1ed: 727200000000000000   | 	jl	L5
0x1f6: 30fa0100000000000000 | 	irmovq  $1,%r10
0x200: 50b5f0ffffffffffffff | 	mrmovq  -16(%rbp),%r11
0x20a: 61ab                 | 	subq	%r10,%r11
0x20c: 40b5f0ffffffffffffff | 	rmmovq  %r11,-16(%rbp)
0x216:                      | L2:
0x216: 30f80000000000000000 | 	irmovq  $0,%r8
0x220: 5095f0ffffffffffffff | 	mrmovq  -16(%rbp),%r9
0x22a: 6189                 | 	subq	%r8, %r9
0x22c: 765500000000000000   | 	jg	L6
0x235: 10                   | 	nop
0x236: b05f                 | 	popq	%rbp
0x238: 90                   | 	ret
0x239:                      | LFE0:
0x239:                      | LC0:
0x239:                      | main:
0x239:                      | LFB1:
0x239: a05f                 | 	pushq	%rbp
0x23b: 2045                 | 	rrmovq	%rsp, %rbp
0x23d: 30f87000000000000000 | 	irmovq  $112,%r8
0x247: 6184                 | 	subq	%r8, %rsp
                            | #	mrmovq	%fs:40, %rax 
                            | #	rmmovq	%rax, -8(%rbp)
0x249: 6300                 | 	xorq	%rax, %rax
0x24b: 30f80200000000000000 | 	irmovq  $2, %r8
0x255: 4085a0ffffffffffffff | 	rmmovq	%r8, -96(%rbp)
0x25f: 30f80300000000000000 | 	irmovq  $3, %r8
0x269: 4085a8ffffffffffffff | 	rmmovq	%r8, -88(%rbp)
0x273: 30f80400000000000000 | 	irmovq  $4, %r8
0x27d: 4085b0ffffffffffffff | 	rmmovq	%r8, -80(%rbp)
0x287: 30f80500000000000000 | 	irmovq  $5, %r8
0x291: 4085b8ffffffffffffff | 	rmmovq	%r8, -72(%rbp)
0x29b: 30f80600000000000000 | 	irmovq  $6, %r8
0x2a5: 4085c0ffffffffffffff | 	rmmovq	%r8, -64(%rbp)
0x2af: 30f80700000000000000 | 	irmovq  $7, %r8
0x2b9: 4085c8ffffffffffffff | 	rmmovq	%r8, -56(%rbp)
0x2c3: 30f80800000000000000 | 	irmovq  $8, %r8
0x2cd: 4085d0ffffffffffffff | 	rmmovq	%r8, -48(%rbp)
0x2d7: 30f80900000000000000 |     irmovq  $9, %r8
0x2e1: 4085d8ffffffffffffff | 	rmmovq	%r8, -40(%rbp)
0x2eb: 30f80000000000000000 | 	irmovq  $0, %r8
0x2f5: 4085e0ffffffffffffff | 	rmmovq	%r8, -32(%rbp)
0x2ff: 30f80100000000000000 | 	irmovq  $1, %r8
0x309: 4085e8ffffffffffffff | 	rmmovq	%r8, -24(%rbp)
0x313: 2059                 | 	rrmovq  %rbp,%r9
0x315: 30faa0ffffffffffffff | 	irmovq  $-96,%r10
0x31f: 61a9                 | 	subq    %r10,%r9
0x321: 2090                 | 	rrmovq  %r9,%rax
0x323: 30f90a00000000000000 | 	irmovq  $10,%r9
0x32d: 2096                 | 	rrmovq  %r9,%rsi
0x32f: 2007                 | 	rrmovq	%rax, %rdi
0x331: 801400000000000000   | 	call	bubble_a
0x33a: 30f800000000ffffffff | 	irmovq	0xffffffff00000000,%r8
0x344: 50959cffffffffffffff | 	mrmovq  -100(%rbp),%r9
0x34e: 6289                 | 	andq	%r8,%r9
0x350: 40959cffffffffffffff | 	rmmovq	%r9, -100(%rbp) 
0x35a: 701404000000000000   | 	jmp	L8
0x363:                      | L9:
0x363: 30f8ffffffff00000000 | 	irmovq  0xffffffff,%r8
0x36d: 50959cffffffffffffff | 	mrmovq  -100(%rbp),%r9
0x377: 6289                 | 	andq    %r8,%r9
0x379: 2090                 | 	rrmovq	%r9, %rax
                            | 	
0x37b: 6289                 | 	andq    %r8,%r9
0x37d: 30fa0000000000000000 | 	irmovq  $0,%r10
0x387: 61a9                 | 	subq    %r10,%r9
0x389: 729e03000000000000   |     jl      zhengshu
0x392: 30fb00000000ffffffff | 	irmovq    0xffffffff00000000,%r11
0x39c: 60b0                 | 	addq    %r11,%rax
0x39e:                      | zhengshu:
0x39e: 2008                 | 	rrmovq  %rax,%r8
0x3a0: 6088                 | 	addq    %r8,%r8
0x3a2: 6088                 | 	addq    %r8,%r8
0x3a4: 6088                 | 	addq    %r8,%r8
0x3a6: 2082                 | 	rrmovq  %r8,%rdx
0x3a8: 30f9a0ffffffffffffff | 	irmovq  $-96,%r9
0x3b2: 205a                 | 	rrmovq  %rbp,%r10
0x3b4: 609a                 | 	addq    %r9,%r10
0x3b6: 20a0                 | 	rrmovq  %r10,%rax
0x3b8: 6020                 | 	addq	%rdx, %rax
0x3ba: 50000000000000000000 | 	mrmovq	(%rax), %rax
0x3c4: 2006                 | 	rrmovq	%rax, %rsi
0x3c6: 30f73902000000000000 | 	irmovq	$LC0, %rdi
0x3d0: 30f00000000000000000 | 	irmovq	$0, %rax
                            | #	call	printf
0x3da: 50859cffffffffffffff |     mrmovq  -100(%rbp),%r8
0x3e4: 30f9ffffffff00000000 | 	irmovq  0xffffffff,%r9
0x3ee: 30fa0100000000000000 | 	irmovq  $1,%r10
0x3f8: 608a                 | 	addq    %r8,%r10
0x3fa: 629a                 | 	andq    %r9,%r10
0x3fc: 30fb00000000ffffffff | 	irmovq  0xffffffff00000000,%r11
0x406: 628b                 | 	andq    %r8,%r11
0x408: 60ba                 | 	addq    %r11,%r10
0x40a: 40a59cffffffffffffff | 	rmmovq  %r10,-100(%rbp)
0x414:                      | L8:
0x414: 50959cffffffffffffff | 	mrmovq  -100(%rbp),%r9
0x41e: 30f8ffffffff00000000 | 	irmovq  0xffffffff,%r8
0x428: 6289                 | 	andq	%r8,%r9
0x42a: 30fa0900000000000000 | 	irmovq  $9,%r10
0x434: 61a9                 | 	subq	%r10,%r9
0x436: 716303000000000000   | 	jle	L9
0x43f: 30f800000000ffffffff | 	irmovq  0xffffffff00000000,%r8
0x449: 6280                 | 	andq    %r8,%rax
                            | #	mrmovq	-8(%rbp), %rcx
                            | #	mrmovq  %fs:40,%r8
                            | #	xorq	%r8, %rcx      
                            | #	je	L11
                            | 
0x44b:                      | L11:
0x44b: 2054                 | 	rrmovq  %rbp,%rsp
0x44d: b05f                 | 	popq    %rbp
0x44f: 90                   | 	ret
0x450:                      | LFE1:
                            | 
0x600:                      | .pos 0x600
0x600:                      | stack:

课下实践2

把课上练习3的daytime服务器分别用多进程和多线程实现成并发服务器并测试

客户端代码始终没有变化:

#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) {
    
    time_t t;
    struct tm * lt;
    size_t n; 
    printf("\n客户端IP:127.0.0.1\n");
    printf("服务器实现者学号:20155302\n");
    
    time (&t);//获取Unix时间戳
    lt = localtime (&t);//转为时间结构
    printf ("当前时间为:%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果。注意月份取值区间为[0,11],所以要+1;年份的修改。
    Rio_writen(clientfd, buf, strlen(buf));
    Rio_readlineb(&rio, buf, MAXLINE);
    Fputs(buf, stdout);
    }
    Close(clientfd); //line:netp:echoclient:close
    exit(0);
}

多进程实现服务器代码:

#include "csapp.h"
void echo(int connfd,char *haddrp);

void sigchld_handler(int sig) //line:conc:echoserverp:handlerstart
{
    while (waitpid(-1, 0, WNOHANG) > 0)
    ;
    return;
} //line:conc:echoserverp:handlerend

int main(int argc, char **argv) 
{
    int listenfd, connfd, port;
    char *haddrp;
    socklen_t clientlen=sizeof(struct sockaddr_in);
    struct sockaddr_in clientaddr;

    if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    exit(0);
    }
    port = atoi(argv[1]);

    Signal(SIGCHLD, sigchld_handler);
    listenfd = Open_listenfd(port);
    while (1) {
    connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);
    if (Fork() == 0) { 
        haddrp = inet_ntoa(clientaddr.sin_addr);
        Close(listenfd); /* Child closes its listening socket */
        echo(connfd,haddrp);    /* Child services client */ //line:conc:echoserverp:echofun
        Close(connfd);   /* Child closes connection with client */ //line:conc:echoserverp:childclose
        exit(0);         /* Child exits */
    }
    Close(connfd); /* Parent closes connected socket (important!) */ //line:conc:echoserverp:parentclose
    }
}

实验截图:

在下一次运行时会出现服务器打不开而显示端口已被进程占用此类问题,解决方法请参考:https://www.cnblogs.com/lcword/p/6046261.html直接杀死进程,有的同学可能出现查不到被什么占用了,那就暴力解决关掉虚拟机!再重启就好了!

多线程实现代码:

#include "csapp.h"

void *thread(void *vargp);

void echo(int connfd,char *haddrp);

char *haddrp;

int main(int argc, char **argv) 

{

    int listenfd, *connfdp, port, clientlen=sizeof(struct sockaddr_in);

    struct sockaddr_in clientaddr;

    pthread_t tid; 



    if (argc != 2) {

    fprintf(stderr, "usage: %s <port>\n", argv[0]);

    exit(0);

    }

    port = atoi(argv[1]);



    listenfd = Open_listenfd(port);

    while (1) {

    connfdp = Malloc(sizeof(int));

    haddrp = inet_ntoa(clientaddr.sin_addr);

    *connfdp = Accept(listenfd, (SA *) &clientaddr, &clientlen);

    Pthread_create(&tid, NULL, thread, connfdp);

    }

}



/* thread routine */

void *thread(void *vargp) 

{  

    int connfd = *((int *)vargp);

    Pthread_detach(pthread_self()); 

    Free(vargp);

    echo(connfd,haddrp);

    Close(connfd);

    return NULL;

}

实验截图:

posted @ 2017-11-12 23:48  乐舞歌不是可爱送  阅读(181)  评论(0编辑  收藏  举报
Live2D