《实验十一网络编程.docx》由会员分享,可在线阅读,更多相关《实验十一网络编程.docx(23页珍藏版)》请在优知文库上搜索。
1、嵌入式操作系统应用开发课程实验报告级:*号:*指导老师:*旅京*京*京成 绩,实验十一网络编程一、目的与任务目的:r解掌握网络编程的特点与功能,学会借助网络系统的功能函数进行 编程。任务:利用c语言指令编写程序调用网络系统函数,完成相应网络访问功 能。二、内容、要求与安排方式1、实验内容与要求:1)利用地址处理函数访问网络地址以及编程实现域名与IP估息解析.2)实现UDpnt与组播通信功能.3)练习版始套接口基本应用。2,实照安排方式;采用1人1级,上机在LinUx系统下进行编程实验,三、程序清单(I )TCP服务器和TCP客户端通信1.1 服务器代码:/include #includc #i
2、nclude /include #includc #include int main()int SoCkfd,clienUd;以OCk_fd-监听套接字描述符:ClienLS-连接套接字描述符struct Sockaddjin my_addr: 本机地址struct sock add ri n remote_addr; 客户端地址创建套接字if (sockfd = SOCket(AF_1NET, SOCK_STREAM. 0) =-l)Ipeor( socket);exit(l);设置地址端口可重用int val=l;SCIS(KkOPl( sockf(J, SOL-SOCKET. SO-RE
3、USEADDR. (char*)&val. sizeof(val);设置本地地址信息my_addr.sin JatniIy=AFJNET;协议族my_addr.sin_port=htons(3333);端 my_addr.sin_addr.s_addr=inet_addr( 127.0.0.1):/1P 地址bzero(&(n)y_addr.sin_zero),8);填充 0绑定地址到套接字描述符上if(hind(sockfd. (struct sockaddr *)&my_addr, sizeo(struct sockaddr)= -1) Perror(bind,; cxit(l);)在地址
4、端口上监听if (liscn(sockfd. 10) = - I)Ipcor(listen);cxit(l);等待客户端连接,如果有客户端连接,则产生新的连接套接字int sin-size = sizeof(struct SOCkaddjin);if(client_fd = accet(socklj, (struct sockaddr *)&reinote_addr,&sin_size)=-DIPClTOr(accept);cxit(l);输出客户端IP地址rintf(received a connection from %sn. inet_ntoa(remote_addr.sin_addr)
5、: 向客户端发送欢迎信息if (scnd(clicnt.fd. Hello, you arc connected!n. 26.0) = - I)PCrroreSCIKrXClOSe(CIienLfd);exit(2);关闭连接套接字cloe(client-M);关闭监听套接字Close(SockId);return 0;)1.2 客户端代码:#includc #include #includc #includc #include #includc int nain(int argc, char *argv) int sockfd. rccvbytcs:char bu10();struct ho
6、s(cnt *host;struct sockaddr_in SCrV_addr:创建套接字if (sockM = socket(h,!NET. SOCK_STREAM. 0) = -l)pcor( socket);exit(l):设置服务器地址结构体serv_addr.sin_fainily=AF_INET;scrv_addr.sin_p)rt=htons(3333);serv_addr.sin_addr.s_addr=inet_addr(l 27.0.0. ,);bzero(&(ser v-addr.s i n-zero),8);向服务器发起连接if (connect(sockfd, (s
7、truct sockadlr *=)&serv_addr,sizeof(struct sockaddr) = - I) (pcor( connect);exit(l);接收服务错端信息并显示if (rccvbytcs=rccv(sockfd. buf. I(X). 0) =-1)eror(recv);cxil( I);burecvbytes = O; 设置字符串结尾rintf(Received: %s,buf);关闭套接字close(sockfd);return 0;(2) UDP服务器和UDP客户端通信2.1 UDP服务器代码;#include /include #includc Sincl
8、ude /include #include int nain()(int sockfd:套接字描述符struct sockaddr_in server;struct SockaddrJn client;int sin_sizc;int num;char msg I00;char SbUiI 1OO=Welcome to my server.;if (sockfd = SoCket(AFNET. SOCK_DGRAM. 0) =-1)PClTor(socket);cxit( 1);bzero(&scncr.sizcof(scncr):server.sin_fam i Iy=A F_1 N ET:
9、scrvcr.sin-port=htons(3333);server. sin_add r. s_addr = inct-addr( 192.168.245.128);if(hind(sockkl, (struct sockaddr *)&server, SiZeOf(SlrUCI sockaddr) = - I)error(bind);exit(l);sin_sizc=sizcof(StRicl SoCkaddr_in);while (1)(nm=rccvfro(sockfl.msg. I(X),O,(struct sockaddr *)&client. &sin_size):if (num
10、 O)error(recvfrom);cxit(l);msgnum = ;printf(You got a message (%s) from %sn,msg,inct_ntoa(clicnt.sin_addr);strcpy(sbuf.msg);scndto(sockfd.sbuf.sirlcn(sb0.0,(struct sockaddr *)&clicnt,sin_sizc);if (!strcnp(msg.quit) break:CIoSe(SOCkfd);H close Iistcnfdreturn O;)2.2UDP客户端代码#include includc #include #i
11、nclude #includc #include VneIineuin.h#includc int main(int argc. char *argv)Iini fd, numbytes;char buf00:char sendbu 20J=hello;struct hostent *hc;struct sockaddr_in SerVer,reply:if (argc !=2)Iprintf(Usagc: %s n.argv(0); cxit(l);if (he=gcthostbynamc(argv( I )=NULL)Iprintf(gcthosibyname() CnWn);cxit(l
12、):if (fkl=sockct(AF-INET, SOCK_DGRAM, Oj)=-1 )Printf(socket)exit(l);bzero(serer.sizeof(server);scrvcr.sin-family = AF_1NET;server. sin_port = htons(3333);server.sin-addr = *(struct in-addr *)he-h-aldr);Sendto(RL scndbuf. strlcn(sen(lbuf),O.(strct Sockaddr *)&scrvcr, sizeof(struct sockaddr);while (I)
13、inilcn;Ien= sizcof(struct sockaddr_in);if (nunbytes=recvfrom(M,bu, l(X),O,(struct sockaddr *)&rcply,&lcn) = -1)(error(recvfrom);cxit(l):/check if server is right if (Ien != sizeof(struct sockaddr) IInlenICmP(ConN void *)feserver, (const void *)rcply,lcn) != O) (printf(Rcccivc message from other scrvcr.n);continue:/printf message from SCrVCrbufnumbylcs=O;printf( Server Message: %sn,buf);rintf(input:);fgcts(scndbuf.sizeof(sendbuf).s