




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验报告 课程名称: 学 院: 专业: LINUX程序设计 计算机学院 计算机14班级: 4班 专业 word可编辑 姓名:高宇 学号: 201401120406 年 月日 山东科技大学教务处制 实验报告 组别 姓名 高宇 同组实验者 实验项目名称 实验一熟悉Linux命令 实验日期 第11周周四 3,4节 教师评语 实验成绩 指导教师 、实验目的 掌握和学习Linux的基本命令 、实验内容和步骤 登录ubuntu界面打来shell终端可以通过快捷键 Ctrl+Alt+T打开 在终端是输入pwd显示当前工作目录,通过使用Is命令来查看当前目录下 的内容,此外可分别测试-a,-I , -F ,
2、-A , -IF 灯选项。 实验截图如下: r r - haLTery Lenovo :-手 pwd /home/happyltfe happyl_i_,feiGaLTery- Lenovo :Is Desk-topDownToad sMusc Publi-c Videos DocumentsexrnipTS desktopP!_etuie雪 TenipTaiEes happyH1TeGalLXery-Lenovo :Is - a Desktop + TCEauthor 5. ty * selected_edttor dmre ml local sudo_as_admtn_su .bash_h
3、is tory DoG.unent s FiozUlXia TenpTdes bash_TogoutDownloads Videos .bashrcexamples . desktop Pictures Xauthortty 匸Nuhe:. g$ nkdtr test happyUfeQGaUery-Lenovo:$ cd test happyltfe0GaUery-Lenovo:-/testS pwd /hone/happylife/ happylife0GaHery-Lenovo/test$ cp /etc/passwd /hone/happylife/test happyltfeGall
4、ery-Lenovo:Is passed happyUfegCaUery-Lenovo:-/test$ Is 4 total 4 -rw-r r- 1 happyltfe happylife 2250 12月 4 16:55 passwd 尝试向当前目录下的 passwd文件和/etc/passwd 文件写入一些数据,查看 是否能成功写入。可通过 echo 字符串” 文件的命令 实现。用cat 命令浏览文件passwd的内容,用more命令进行浏览翻页操作,再用less 命令浏览文件内容。 实验截图如下: happyltfeGallery-Lenovo:/tests happyUfeflGal
5、lery-Lenovo: happyl IfeflGaUery-Lenovo/testS happyltfe0Gallery-Lenovo:*/test$ echo Xallery*/test$ cat /hone/happyltfe/test/psswd roottx:0:0:root:/root:/btn/bash dlaenon: x: 1:1: daenon:/usr/sbtn:/usr/ sbtn/nolog t n happyUfegGaUerydencvok/test$ less /hcne/happ/ltfe/test/passd hippyUfegGallenr-Lenowe
6、cho CallerylSSJ* /usr/passwd bash: /usr/passwd: Perntssion dented happyUfefiGaUery-Lenovoi-/test51 用Is查看test目录下的文件的权限,用mv命令更改passwd文件名为 test.txt,并测试chown和chgrp 更改文件属性为 root组,并尝试用chomd 将文件权限更改为”-rw- ”. 实验截图: happyltfe|GaUery-Lenovo/test$ Is -I total 4 rw-r-r- 1 happyltfe happyltfe 2262 12月 4 17:06 pa
7、ssed happylifeCaUery-Lenovo:*/test$ rv passwd test.txt happylife0CaUery-Lenovo/test5 Is testAxt happyltfegCaUery-Lenovo:*/test$ chown root test.txt chowr: changing ownership of rtest.txt: Operation not pernitted happyUfegGaUery-Lenovo/test$ chgrp root txt.txt chgrp: cannot access txt.txt: No such fi
8、le or directory hdppyltfeCaUery4enovo:-/test$ chnod 689 test.txt happylife0GaUery-Lenovo:*/test$ Is testtxt happyllfe0CaUery-Lenovo:*/test$ Is 4 |total 4 ”rw 1 hnrmi/Hfa 肚亦川矗 7? 17日_A (7斤十act txt (6)用rm命令删除test目录下的所有文件,再用rmdir命令删除test目录。(想一想有没 有一条命令将目录及目录下的所有文件删除,写出这条命令) happyltfeQGallery - Lenovo:
9、*-$ nkdir: ntssing operand Try nkdir help for more happy ItfeQCiaUery - Lenovo: -$ happyllf eQGallery - Lenovo: *- Desktop Downleads Documents examples.desktop hdppylifeQGallery-Lenovo:$ rm: tannot remove test happyltfe0Gallery*LenovoS happyltfeQGallery-Lenovo:$ Desktop Downleads Documents examples.
10、desktop happyUfedGallery-Lenovo: *$ mkdtr information, nkdir test Is Music Public Pictures Templates rn怙冒t :Is a directory rndir test Is Music Public Pictures Templates t00 03:00;00 00:00:00 sbin/irit splas kthreadd Icsnf t trqd/a kwnrk pr/fi:AH rcusched rcubh Htgrati.on/9 watchdog/0 watchdog/1 illg
11、ra ti on/1 ksof 11tqd/1: kwurker/1r on watchdog/i Fitgration/2 ksoftirqd/2 kworker/2:9H watchdog/3 ntgration/3; ksofttrqd/3 kworker/3:0H kdevtnpfs netns perf khungtaskd writeback ksnd khugcpaged crypto kinte grityd btuse tJ kbloc kd sff nd- devfreq_wq kworker/1:2 (8)使用df命令查看当前系统已安装的文件系统的空间使用情况,记录结果。
12、 1 p_r a1 happyUfe0CaUery-Lenovo$ df Ftlesysten lK-blocks Used Available Use% Mounted on udev 4025212 0 4825212 B% /dev tnpfs 888946 9824 799116 2% /run /dev/sdaB 44883336 49345 冊 36916464 m 1 tnpfs 4044696 212 4044484 IX /dev/shn tnpfs 5120 4 S116 1% /run/lock tnpfs 4Q44696 0 4044696 8X /sys/fs/cgr
13、oup tnpfs 88#include csys/cirian.tfi* /iiEr/src/linux-hedder5-4.fl-5S-generic/scripts/recordncount.c:#incLude c5ys/stat:.h /usr/src/Knux headers 4.4.6-53-g)eric/scrlpts/recardPK(xint.c:*tnclude /usr/src/Unux-hHders-4.4.ft-53-gefierlc/scrlpts/rordncount.c:tlnclude /iisr/src/Unux-hedid#rs-4,4.#include
14、 recordncountih /usr/src/linux-headers-A.fl-SS-generic/scripts/kconftg/niconf.cztfinclude /usr/s rc/ltnux heade r 4r 4+6 53-generic/scrlpts/kconftg/nconf.c :ttnclude /usr/src/Unux-hfraders-4.4.ft-S3-geflert/scrlpti/kconflg/nconfc:tinclude rific/piajnf.(2:#irKUjck vliinits.h 解释:匹配查找列表输出/usr/src下的.c结尾
15、的文件中含有 #include 的内容。 (14) 执行cal和date命令,说说这两条指令的用途。cal:输出当前日历,date :输出当前 的时间。 (15) 执行命令clear和logout ,退出系统。(想一想有没有其他的方法,写出这种方法)还 可以用命令:exit (16) 执行命令 shutdown ,关闭系统。(想一想有没有更简单的命令,写出这条命令)还 可以用命令:init 0 三、实验结论 通过本次实验我对linux 一些基本操作有了认识,初步认识的linux系统的 运行环境等。初步认识的一些linux下的基础命令。 实验报告 组别 姓名 同组实验者 实验项目名称 实验二Sh
16、ell编程 实验日期 第12周周四 3,4节 教师评语 实验成绩 指导教师 1. 实验目的 (1)学习和掌握Vi编辑器的基本使用方法 (2)学习和掌握编写简单的shell程序 2. 实验内容和步骤 2.1vi的使用 (1)以root用户身份登录进入Linux系统 (2)启动vi,切换到输入模式,输入一段英文: Oh my god! No matter what happe ned I II stay with you! Today, tomorrow (3)尝试匹配其中的一串字符,写出这命令及执行结果。 (4)尝试替换其中的一串字符,与出这命令及执行结果 (5) 尝试复制/删除其中的一行或几行
17、文本,写出命令及执行结果 (6) 尝试复制/删除其中的一个单词或几个字符,写出命令及执行结果 (7) 尝试使用鼠标复制文本,写出命令及执行结果。 (8) 尝试存盘退出操作,写出命令。 可以用的命令:wq或:x 2.2简单的shell程序 (1) 使用Vi将程序清单2-1的程序输入。 (2) 尝试运行该程序,如果不能运行,原因是什么,怎样修改。 (3) 执行程序,写出程序的执行结果。 (4) 阅读程序,掌握shell函数的编写方法,补全函数file_run。 for i in Is do if test -x $i;the n echo $i fi done 可以使用循 (5) 修改程序中的函数
18、uers(),使得显示在线用户时不分行。(提示: 环,对每个在线用户分别处理 Echo “ there are- Who|wcers online” for i in“ who arewho|awk print $1” do echo -n $i echo - n done 3. 实验总结 通过本次实验对linux下的文件编辑器vi有初步了解,掌握了基本使用方法,此 外初识shell编程,对脚本文件有所认识。 4. 程序清单 MYDATE=date +%d/%m/%y THIS_HOST=host name -s USER=whoami file_r un() echo you must co
19、mplete it uers() echo there are who|wc -I users on li ne echo -e who arenwho|awk print $1、 while : do tput clear cat read CHOICE case $CHOICE in 1) Is -l|awk d/ print $8 J J 2) file_ru n J J 3) uers J J H|h) cat aaa This is the help scree n,nothing here yet to help you! aaa J J Q|q) exit 0 *) echo -
20、e t007 un kow n user resp onse esac echo -e -n nn tHit the return key to continue read DUMMY done 实验报告 组别 姓名 高宇 同组实验者 实验项目名称 实验三文件操作 实验日期 第13周周四 3,4节 教师评语 实验成绩 指导教师 1、实验目的 (1) 学习和掌握gcc等Linux的开发调试环境。 (2) 学习并掌握Linux的文件操作。 (3) 编写并实现实验步骤6要求的程序。 2、实验内容和步骤 (1) 使用Vi将程序清单3-1和3-2的程序输入,并在当前目录下创建文件“le.in ”和文件
21、“le.out,尽可能的使文件“le.in大一些。 (2) 利用gcc分别编译这两个程序,写出编译命令和执行结果,如果不成功,尝试利用gdb 调试。 Gcc p list3_1 Iist3_1.c Gcc p list3_2 list3_2.c (3) 仔细观察这两个程序,比较标准C的文件操作和Linux的系统调用open、read、write 等的使用区别 答:一个是底层的系统调用,另一个是库函数调用,其效率略有差距,同时参数也不同。 (4) 按照说明重新修改程序3-2,并使用time命令察看程序执行的时间效率上有何区别。 修改之前的系统时间 : rootlocalhost # time .
22、/ Iist3_2 real0m18.791s user0m0.824s sys0m17.317s 修改之后的时间: rootlocalhost # time ./list3_2 real0m0.049s userOmO.OOOs sys0m0.036s (5) 输入、编译并运行程序3-3和3-4,写出执行结果,并比较他们fseek和lseek在使用方 法上的异同 程序3的 运行结果是:在root目录下创建了文件 hole.file而且 文件的内容为: abcdefghij (6) 学习并分别使用标准C的文件操作函数和Linux的系统调用创建一个对学生基本信息进 行操作(插入、修改和删除)的C
23、程序,学生基本信息以结构体的形式存储在文件中, struct stu info的定义如下 struct stu_i nfo char stu_ nu m12; char n ame10; short int sex;/*0 为女生,1 为男生 */ char mobile_pho ne12; ; 3、实验结论 学习了在linux环境下进行简单的 C语言程序设计。对linux下编程有所认识 4、程序清单 /程序清单3-1 #i nclude #in clude int main( void) char c; FILE *in ,*out; if(in = fope n( file
24、, in ,r)=NULL) perror(file ope n error!); exit(O); out = fope n( file.out,w); while(c = fgetc(i n) )!=EOF) fputc(c,out); /程序清单3-2 #in clude #in elude #in elude vfen tl.h int mai n() / char block1024; char c; int in, out; int n read; in = ope n(file.i n, O_RDONLY); out = ope n(file.out, O_WRONLY|O_CR
25、EAT, S_IRUSR|S_IWUSR); /将注释打开,并将两条语句的后一句注释掉,重新编译执行。 /while( (n read = read(i n,block,sizeof(block) 0) while( (n read = read(i n, write(out, /程序清单3-3 #in elude #in elude #in elude vfen tl.h #in elude #in elude ehar buf1 = abedefghij; ehar buf2 = ABCDEFGHIJ; void err_exit(char *err_s) perror(err_s); e
26、xit(1); int main( void) FILE *fp; if(fp = fope n( hole.file,w) = NULL) err_exit(file open fail!); if(fwrite(buf1,sizeof(buf1),1,fp)!=1) err_exit(file write buf1 error!); if(fseek(fp,40,SEEK_SET)=-1) err_exit(fseek error!); if(fwrite(buf2,strle n( buf2),1,fp)!=1) err_exit(file write buf2 error!); fcl
27、ose(fp); /程序清单3-4 #in clude #in elude #in elude vfen tl.h #in elude #in elude char buf1 = abcdefghij; char buf2 = ABCDEFGHIJ; void err_exit(char *err_s) perror(err_s); exit(1); int main( void) int fd; if(fd = ope n(hole.file,O_WRONLY|O_CREAT/*|O_APPEND,0644*/) = -1) err_exit(file open fail!); if(wri
28、te(fd,buf1,10)!=10) err_exit(file write bufl error!); if(lseek(fd,40,SEEK_SET)=-1) err_exit(lseek error!); if(write(fd,buf2,10)!=10) err_exit(file write buf2 error!); 实验报告 组别 姓名 高宇 同组实验者 实验项目名称 实验四进程控制 实验日期 第ong14周 周四3,4节 教师评语 实验成绩指导教师 1、实验目的 (1) 学习和掌握fork等系统调用的基本使用方法。 (2) 利用Linux中的管道实现父子进程间的同步。 2、实
29、验内容与步骤 (1) 使用Vi将程序清单4-1的程序输入、编译并运行,学习和掌握fork的基本调用方法。 (2) 使用Vi将程序清单4-2、4-3和4-4的程序输入、利用gcc分别编译这三个程序,写出 编译命令和编译结果,如果不成功,尝试利用gdb调试。 rootlocalhost # gcc -o 4_2 4_2.c 4_2.c:43:2: warning: no n ewli ne at end of file rootlocalhost # ./4_2 child failed. 4-3 rootlocalhost # gcc -o father father.c father.c: I
30、n function main: father.c:7: error: l undeclared (first use in this function) father.c:7: error: (Each undeclared identifier is reported only once father.c:7: error: for each function it appears in.) father.c:8: error: stray 129 in program father.c:8: error: syn tax error before nu meric con sta nt
31、father.c:8: error: stray 132 in program father.c:10:2: warning: no newline at end of file rootlocalhost # gcc -o child child.c child.c:7:2: warning: no newline at end of file rootlocalhost # ./child hello hello child, child. 谈谈这么做的好处 (3) 写出编译这三个程序的makefile,然后利用make进行编译, 可以节省劳动时间,让三个程序同时编译,简化了操作。 (4)
32、 运行这三个程序,写出运行结果。 4 2的运行结果: 4_2.c:43:2: warning: no n ewli ne at end of file rootlocalhost # ./4_2 Pare nt is using pipe write. child, child. 4 3的运行结果: rootlocalhost # ./father Pare nt is using pipe write. 4 4的运行结果是: rootlocalhost # ./child hello child! hello child! child, child. (5) 屏幕上显示出的执行结果是哪一个进
33、程执行的结果? Father.c (6) 父进程中的printf有向屏幕输出吗?为什么? 没有,因为父进程的标准输出已经被重定向。 (7) 利用父子进程间的管道通信方式,改写实验3步骤6要求的程序。要求启用两个进程,其 中父进程接受用户对文件的操作命令然后通过管道发给子进程,子进程完成对文件的 实际操作。 3、实验结论 对进程有所了解,学习了 linux下进程的操作方法,了解了 dup的工作原理。 4、程序清单 /程序清单4-1 #in clude #i nclude #i nclude err_exit.h int global = 5; int main( void) pi
34、d_t pid; char *stri ng = these are values before fork:; int local = 10; printf(before fork * * *nn); if(pid = fork()0) err_exit(fork error); if(pid = 0) stri ng = I am child.; prin tf(nMy pid is %d,%sn pid = %dn global = %dn local = %dn, getpid(),stri ng,pid,global,local); global+; else stri ng = I
35、am pare nt.; prin tf(nMy pid is %d,%sn pid = %dn global = %dn local = %dn,getpid(),stri ng,pid,global,local); local+; pri ntf(%sn Now,global = %d,local = %dn ,stri ng,global,local); exit(EXIT_SUCCESS); /清单4-2管道程序 # defi ne STD_INPUT 0 # defi ne STD_OUTPUT 1 /定义标准输入设备描述符 /定义标准输出设备描述符 int fd2; main ()
36、 static char process1= father ” ,process2= child pipe(fd);/定义管道 pipeline(process1,process2);/ 调用自定义函数pipeline() exit(1);/程序结束 pipeli ne(char* process1,char* process2) int i; if (i=fork()=-1)II创建进程,失败退出 perror( “ process fork error!” ); if (i) exit(1); close(fd0); /关闭管道输入描述符 else close(STD_OUTPUT); d
37、up(fd1); close(fd1); execl(process1, process1, 0); / father failed” printf( ); / / close(fd1); close(STD_INPUT); / dup(fd0); / 关闭标准输出描述符1 指定标准输出描述符1为管道写指针 /关闭原始管道写指针 用程序father覆盖当前程序 / execl()执行失败 /关闭管道输出描述符 关闭标准输入描述符 0 指定标准输入描述符 0为管道读指针 close(fd0); execl(process2,process2,0); / /关闭原始管道读指针 用程序child覆盖
38、当前程序 printf( “ child failed.); / execl()执行失败 exit(2); / 程序结束 清单 4-3 father.c mai n() static char stri ng= “ Pare nt is using pipe write. int len; len = sizeof(stri ng); write(1, stri ng, le n); “ pare nt, pare nt, pare nt printf( /*将string中的内容写入管道中 n ;) */ exit(0); 清单4-4 main () child.c char output3
39、0; read (0, output, 30); /*从管道中读数据并存入output中*/ printf(“ s n ”,output); return(O); 实验报告 组别 姓名 高宇 同组实验者 实验项目名称 实验五线程练习 实验日期 第15周周四 3,4节 教师评语 实验成绩指导教师 一、实验目的 1. 掌握linux环境下线程的创建 2. 掌握linux环境下线程编程的几种常见模型 3. 掌握信号量和互斥量的使用、线程同步 二、实验准备 使用线程编程的几种常见模型: 1. 管理者 / 工作者(Ma nager/worker): 一个单线程,作为管理器将工作分配给其它线程 (工作者)
40、,典型的,管 理器处理所有输入和分配工作给其它任务。至少两种形式的 manager/worker 模型比较常用:静态 worker池和动态 worker池。 2. 管道(Pipeline): 任务可以被划分为一系列子操作,每一个被串行处理,且是被不同的线程 并发处理。汽车装配线可以很好的描述这个模型。比如IDM等下载软件 的文件分块同时下载。 3. Peer: Peer和manager/worker模型相似,但是主线程在创建了其它线程后,自 己也参与工作。 三、实验内容 1. 创建和结束线程函数: int pthread_create(pthread_t *tidp ,const pthrea
41、d_attr_t * attr ,(void *)(*start_rtn)( void *),void *arg); void pthread_exit( void * retval); int pthread_attr_i nit(pthread_attr_t * attr); int pthread_attr_destroy(pthread_attr_t * attr); 单线程 该程序5秒后输出,sleep期间不再响应其它消息或执行其他操作。为了更好地处理这 种耗时的操作,我们需要使用多线程编程。 root(n)ubuntu: /home/spring/text 0tnclude fft
42、nclude #lnclude void* print(void *) static int g=0; prtntf( %dn, g卄); pthread_exit(O); int main() pthreadt ttd; while(1) pthreadareatefId,NULL,print,NULL); pthreaddetachCtid); return 9; 执行结果: rootubuntu:/home/spring/text# gcc -fopenmp haha2.c -o haha2 rootubuntu:/home/spring/text# */haha2 4 1 got a
43、nunber 10 i got a number 10 1 got a number 10 t got a number 10 ii 1 got a number 10 t got a number 10 i got a number 10 got nunber 10 i got a number 10 i 1 got a number 10 i got a number 10 a 1 got a number 10 t got a number 10 1 got a number 10 I got a number 10 i got a number 10 1 got a nunber 10
44、 i got a number 10 1 got a number 10 t got a nunber 10 jn. 2. 用信号量进行同步 1) 信号量创建 #inelude int sem_init(sem_t *sem, int pshared, unsigned int value); 参数说明: sem :信号量对象。 pshared :控制信号量的类型,0表示这个信- 号量是当 前进程的局部信号量,否则,这 个信号量就可以在多个进程之间共享。 value :信号量的初始值。 2) 信号量控制 #include int sem_wait(sem_t *sem); int sem_po
45、st(sem_t *sem); sem_post的作用是以原子操作的方式给信号量的值加1。 em_wait的作用是以原子操作的方式给信号量的值减 1,但它会等到信号量非0时才会 开始减法操作。如果对值为0的信号量调用sem_wait,这个函数就会等待,直到有线 程增加了该信号量的值使其不再为 0。 3) 信号量销毁 #include int sem_destory(sem_t *sem); 这个函数的作用是,用完信号量后对它进行清理,清理该信号量所拥有的资源。如果 你试图清理的信号量正被一些线程等待,就会收到一个错误。 与大多数Linux函数一样,这些函数在成功时都返回0。 下面编码实现输入字
46、符串,统计每行的字符个数,以end”结束输入: 程序代码: rootubuntu* /home/sprfng/text include #tnclude #tnclude #tn匚Iude tfinclude #deftnE SIZE 1024 void *threadfunction(void *arg); char bufferSIZE; sem_t sen; roatubuntu: /home/spring/teKt tnt matn() tnt res; pthread_t a_thread; void *thredd_result; res = if (res != O) ( per
47、ror( ,TSem intt faitednH); exit(EXIT_FAILURE); _ res = pthread_cre5te( if (res != 0) ( perror( Thread cre-ate fsi.lednN; exit(EXIT_FAlLURE); prtntfCInput sone textEntmr end* to finishedn”); while (scanfC%sM. buffer) ( sem_post( printf (nWaiting for thread to finish. *n,r); res = pthread_join(a_threa
48、d, if (res != 0) perror(,rThread join failed*); extt(EXIT FAILURE): printf (Thread jotnXn*1); sern_destroy(; extt(EXITSUCCESS); voidhread_function(void *arg) _ sem_wait( while (strncnp(end, buffer, 3) != 6) ( printf(You input %d charactersn, strlenfbuffer); sem_watt(asen); _ pthread_exit(NULL); 执行语句
49、:gcc -o thread2 thread2.c -pthread 执行结果: ootubuntu:/hom亡/sping/t亡xt# */thread2 Input some text.Enter 1 end to finished 1234 You input 4 characters 1234567 You input 7 characters 32w You input 3 characters end Watting for thread to finish. Thread join A jn. I/ I* A i t- IH 通过使用信号量,阻塞了统计字符个数的线程。 四、实验总
50、结: 另一种用在多线程程序中同步访问的方法是使用互斥量。它允许程序员锁住某个对 象,使得每次只能有一个线程访问它。为了控制对关键代码的访问,必须在进入这段 代码之前锁住一个互斥量,然后在完成操作之后解锁它。 用于互斥量的基本函数和用于信号量的函数非常相似: #i nclude int pthread_mutex_i nit(pthread_mutex_t *mutex, const pthread_mutexattr_t, *mutexattr); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_ un loc
51、k(pthread_mutex_t *mutex); int pthread_mutex_destory(pthread_mutex_t *mutex); 可以使用互斥信号量重写上面的程序 实验报告 组别 姓名 高宇 同组实验者 实验项目名称 实验六网络编程 实验日期 第16周周四 3,4节 教师评语 实验成绩指导教师 一、实验目的 1. 理解端口和Socket的概念 2. 理解并且掌握面向连接的TCP编程和面向非连接的UDP编程 3. I/O多路利用的控制 二、实验要求 编写一个基于TCP编程的网络应用程序 三、实验原理 1. 套接字,区分不同的程序进程间的网络通信和连接,主要使用三个参数。
52、 通信的目的IP地址、使用的传输层协议(TCP或者UDP)、使用的端口号 2. 基于TCP协议的编程常用的函数 基于TCP协议的编程常用的函数 函数名功能 Socket 用于建立一个 socket连接 Bi nd 将socket与本机上的一个端口绑定,随后可在该端口监听服务请求 Connect 面向连接客服程序使用 connect函数来配置socket,并与远程服务 器建立一个TCP连接 Lin te n Listen函数使socket处于被动监听模式,并为该socket建立一个输 入数据队列,将到达的服务请求保存在此队列中 ,直到程序处理他 们 Accept Accept函数让服务器接收客户
53、连接请求 Close 停止在socket上的任何数据操作 Se nd 数据发送函数 Recv 数据接收函数 3. 通信过程: 1) 服务器调用socket()、bind()、listen()完成初始化后,调用accept()阻塞等待, 处于监听端口的状态;客户端调用socket()初始化后,调用connect()发出同步 信号SYN,并阻塞等待服务器应答,服务器应答一个同步-应答信号 SYN-ACK,客户端收到后从connect()返回,同时应答一个ACK,服务器收到后 从accept()返回。 2) 建立连接后,TCP协议提供全双工的通信服务,但是一般的客户端/服务器程序 的流程是由客户端主
54、动发起请求,服务器被动处理请求,一问一答的方式 3) 如果客户端没有更多的请求了 ,就调用close()关闭连接,就像写端关闭的管道 一样,服务器的read()返回0,这样服务器就知道客户端关闭了连接,也调用 close()关闭连接 四、实验内容 分别编写服务器端、客户端程序,服务器通过socket连接后,在服务器上显示 客户端的IP地址或域名,从客户端读字符,然后将每个字符转换为大写并回送给客 户端。客户端发送字符串 连接上了”,客户端把接收到字符串显示在屏幕上。 分析: 首先调用socket函数创建一个socket,接着调用bind函数将其与本机地址 以及一个本地端口号绑定,然后调用函数listen在相应的socket端口上监 听,当accept接收到一个连接服务请求时,将生成一个新的socket套
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年山西警官职业学院高职单招职业适应性测试历年(2019-2024年)真题考点试卷含答案解析
- 2025年山西艺术职业学院高职单招职业适应性测试历年(2019-2024年)真题考点试卷含答案解析
- 2025年山西水利职业技术学院高职单招(数学)历年真题考点含答案解析
- 2025年山东中医药高等专科学校高职单招职业技能测试近5年常考版参考题库含答案解析
- body-language课文教学课件
- 保险行业时间管理
- DNS服务基础知识课件
- 2345课件安全性分析
- 天津市河东区2025届高三下学期一模试题 地理 含解析
- 制作课程表指南
- 2025年中考化学实验操作考试试题库(全套完整版)
- AI在护理查房中的应用
- 西师版小学六年级数学教学大纲与计划
- 2024雅安雨城区中小学教师招聘考试试题及答案
- 20以内三个数加减混合运算竞赛练习训练题大全附答案
- 2025年郑州电力职业技术学院单招职业技能测试题库汇编
- 临床肾内科健康宣教
- GB/T 45166-2024无损检测红外热成像检测总则
- 知识付费居间合同样本
- 《犯罪心理学》教学大纲
- 幼儿园市级课一等奖-大班语言健康绘本《我的情绪小怪兽》有声绘本课件
评论
0/150
提交评论