基于TCP协议下的Linux网络编程的服务器端和客户端.docx

上传人:苏美尔 文档编号:10702807 上传时间:2021-05-31 格式:DOCX 页数:8 大小:19.75KB
返回 下载 相关 举报
基于TCP协议下的Linux网络编程的服务器端和客户端.docx_第1页
第1页 / 共8页
基于TCP协议下的Linux网络编程的服务器端和客户端.docx_第2页
第2页 / 共8页
基于TCP协议下的Linux网络编程的服务器端和客户端.docx_第3页
第3页 / 共8页
基于TCP协议下的Linux网络编程的服务器端和客户端.docx_第4页
第4页 / 共8页
基于TCP协议下的Linux网络编程的服务器端和客户端.docx_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《基于TCP协议下的Linux网络编程的服务器端和客户端.docx》由会员分享,可在线阅读,更多相关《基于TCP协议下的Linux网络编程的服务器端和客户端.docx(8页珍藏版)》请在三一文库上搜索。

1、基于T C P协议下的Li n ux网络编程的服务器端和客户端作者:日期:基于TCP协议下的L i n u x网络编程的服务器端和客户端 客户端:C li ent. c/*n se n d and rece i ve * * * * * * * * * * Narre : Linux Cli e nt* D a t e : 201 4-1110*Aut h o r : marks man*Aim : Lin * ? me s*/#includeu x C 1 ie nt ca sa g e. ?* * * *# i n elude# i n cl u d e # i nclude # i nc

2、 1 ude #includ e intma i n (int ar g c,ch ar *a r g v 口)7nt socke t fd,r e ce ive_so c ket ;etr u c t so c kaddr _ i n s _add, c add;?jns i gned s h ort portnum = 0x22B 8;? n t len ;?:har b uf 1 00;? *? * C reate the s ocket*/7f(so ck etfd=socket(A F INET,SOCS TREAM,0) )0)?print f (Soc k et create e

3、rror! n);exit( 1 );?/ *set t h e add r ess f o rmat?*/Tbzer o (&s add,si z e of( struct soc k addr _ i n);s_add.sin_fa mil y = AF_ I NET;1 ) ;/ / c han g es add. s in _ a ddr. s _a d dr = inet_add r (127 . 0. 0 .the st ring to 32-bi t i n tern e t b yt e . s_add.sin _ port = htons (portnum);?i f (co

4、n n ec t (so c ke t f d,( s t ru c t soc k add r * ) (& s _add), s izeof(struct sockad dr) v 0)7?brin t f(Co n nect fai 1 ure! n);? r e tur n -1;4else?p r int f (C o nnect S u ccess ! n );? *Usi ng the loop to send and rec e i ve the mess a ge. */? ?while(1)? e m s et( b uf, / 0,100);printf( I n put

5、 mes s age to send :);?fge t s( b u f , 100,std i n);?en = sen d (soc k e tfd,buf, str len(bu f )-1,0 );?i?( 1 en 0 )?pr i n t f(send erro r ! n);? exit (1);4?l s e?(printf ( Send Suc c ess! s e nd is :%s n ,buf);?memset (buf, / 0,100);?en=rec v (s o cket f d,buf , 100 ,0);? f (le n 0)?(? pri n tf(

6、re c v erro r !n);?Ut(1);?else if (0= 1 en)? p r intf( c lien t qui t ! n);?exit(1);? 1 se?% s n u ,b u f);? printf(re c ei v e mes s age i s ?)r i n tf( - -n);?c 1 o s e(so c k e t fd); ?服务器端:Ser ver. c/ * * * * *Name : Linu x Se rv e r*Date :2 01 4 - 1 1-1 0* Author : ma rksman*Aim : Linux S e r v

7、 e r can* me ssage.It is ato m* Ins t a n t Mess a g i n* * * * * * * * * * * * * * *?*send and receive ?*system ab o ut ? *g s y s tem.? *k * * * * */#includ e #include # i nclude # incl u d e vstdl i b. h #in c lud e # in c 1 u de #include int m a i n ( int argc , char *a rgv)?nts o cketf d _liste

8、n, s o cketf d_c on nect;etru c t soc k add r _i n serv er _ ad dr,cl i e n t _ad d r; unsigned short p o rtnum = 0x2 2 B 8;in t len ;为h a r buf 1 0 0 ;?*C r e a te the s o cket?* /? f (sock e tfd_l i s ten = s o cket(AF_INET, S OCK_S TREAt, 0)erv e _ _addr. sin_family=AF_INET;Re r ve r _addr.sin_ad

9、dr.s_ a ddr=h t onl( I NADDRANY ; ?Re r v er_addr . sin_po rt = htons(po r tnum);?f(bind(s o c ketfd_list e n , ( struct soc kad d r *) ( &serve r _ a ddr) ,sizeof(st ruct s ockaddr)0)?rint f ( b ind fa i 1 u r e!n);7et u rn 1;?pr i n t f (b i nd Success! n);? f (lis ten (socketfd_li s ten,5) v 0)?

10、p r i ntf ( Listen f a il u r e! n ); return -1;l e n= s izeof(struc t sockaddr );socketfd_conn e ct = a c c e pt( s ocke t fd _ l i st e n, (struct soc k a d dr *)(& cl i en t_a ddr), &l e n);?i f (socketfd_ c onnec t v 0)7? prin t f( a ccep t fai 1! n );r et u rn -1;?/*Using the 1 oop to send a n

11、d rece i ve the message .*/ ?w h i l e (1)?mems e t (buf,0 , 100);? en = r e cv(socketf d _connec t ,b uf , 1 00,0);?(len v 0)?rintf(recv err o r!n );?0xit( 1 );?e ls e if(0 = 1 en)?p r i n tf(the c 1 ie n t qu i n );? ?brea k ;?0l s e? p r i n tf ( r ec ei v e m essa g e is : % s n,bu f );)?/?/me m

12、s et ( buf,0,100);/printf(Inpu t message to sen d :);? /fge t s (buf,1 0 0,st d in);?le n =se n d (so c ke t f d _connect,bu f , s tr 1 e n( b uf) -1, 0 ); ? f ( 1 en0) ? p r intf(Send erro r ! n);?0xit (1);? els e ? r in t f(S e nd su c ce e d send : %s n , b uf);?)rintf( n);?c l o s e (s o cketfd_l i st e n);?:lo s e(soc k et f d_co n ne c t ) ; ?Return0 ;运行过程,然后再打开一个终端,在一个终端下编译两个文件,运行服务器或者客户端中的一个 运行另一个即可。具体操作:$ g cc o ser v e r Ser v er . c$gcc -o c 1 i e nt Clien t . c终端A:$./server终端B:$./cli ent

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1