实验十一网络编程.docx
《实验十一网络编程.docx》由会员分享,可在线阅读,更多相关《实验十一网络编程.docx(25页珍藏版)》请在冰豆网上搜索。
实验十一网络编程
《嵌入式操作系统应用开发》
课程实验报告
班级:
****************
学号:
*************
姓名:
**************
指导老师:
***************
成绩:
实验十一网络编程
一、目的与任务
目的:
了解掌握网络编程的特点与功能,学会借助网络系统的功能函数进行编程。
任务:
利用C语言指令编写程序调用网络系统函数,完成相应网络访问功能。
二、内容、要求与安排方式
1、实验内容与要求:
1)利用地址处理函数访问网络地址以及编程实现域名与IP信息解析。
2)实现UDP广播与组播通信功能。
3)练习原始套接口基本应用。
2、实验安排方式:
采用1人1组,上机在Linux系统下进行编程实验。
三、程序清单
(1)TCP服务器和TCP客户端通信
1.1服务器代码;
#include
#include
#include
#include
#include
#include
intmain()
{
intsockfd,client_fd;//sock_fd-监听套接字描述符;client_fd-连接套接字描述符
structsockaddr_inmy_addr;//本机地址
structsockaddr_inremote_addr;//客户端地址
//创建套接字
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit
(1);
}
//设置地址端口可重用
intval=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char*)&val,sizeof(val));
//设置本地地址信息
my_addr.sin_family=AF_INET;//协议族
my_addr.sin_port=htons(3333);//端口
my_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//IP地址
bzero(&(my_addr.sin_zero),8);//填充0
//绑定地址到套接字描述符上
if(bind(sockfd,(structsockaddr*)&my_addr,sizeof(structsockaddr))==-1)
{
perror("bind");
exit
(1);
}
//在地址端口上监听
if(listen(sockfd,10)==-1)
{
perror("listen");
exit
(1);
}
//等待客户端连接,如果有客户端连接,则产生新的连接套接字
intsin_size=sizeof(structsockaddr_in);
if((client_fd=accept(sockfd,(structsockaddr*)&remote_addr,&sin_size))==-1)
{
perror("accept");
exit
(1);
}
//输出客户端IP地址
printf("receivedaconnectionfrom%s\n",inet_ntoa(remote_addr.sin_addr));
//向客户端发送欢迎信息
if(send(client_fd,"Hello,youareconnected!
\n",26,0)==-1)
{
perror("send");
close(client_fd);
exit
(2);
}
//关闭连接套接字
close(client_fd);
//关闭监听套接字
close(sockfd);
return0;
}
1.2客户端代码;
#include
#include
#include
#include
#include
#include
intmain(intargc,char*argv[])
{
intsockfd,recvbytes;
charbuf[100];
structhostent*host;
structsockaddr_inserv_addr;
//创建套接字
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit
(1);
}
//设置服务器地址结构体
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(3333);
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
bzero(&(serv_addr.sin_zero),8);
//向服务器发起连接
if(connect(sockfd,(structsockaddr*)&serv_addr,sizeof(structsockaddr))==-1)
{
perror("connect");
exit
(1);
}
//接收服务器端信息并显示
if((recvbytes=recv(sockfd,buf,100,0))==-1)
{
perror("recv");
exit
(1);
}
buf[recvbytes]='\0';//设置字符串结尾
printf("Received:
%s",buf);
//关闭套接字
close(sockfd);
return0;
}
(2)UDP服务器和UDP客户端通信
2.1UDP服务器代码;
#include
#include
#include
#include
#include
#include
intmain()
{
intsockfd;//套接字描述符
structsockaddr_inserver;
structsockaddr_inclient;
intsin_size;
intnum;
charmsg[100];
charsbuf[100]="Welcometomyserver.";
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("socket");
exit
(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(3333);
server.sin_addr.s_addr=inet_addr("192.168.245.128");
if(bind(sockfd,(structsockaddr*)&server,sizeof(structsockaddr))==-1)
{
perror("bind");
exit
(1);
}
sin_size=sizeof(structsockaddr_in);
while
(1)
{
num=recvfrom(sockfd,msg,100,0,
(structsockaddr*)&client,&sin_size);
if(num<0)
{
perror("recvfrom");
exit
(1);
}
msg[num]='\0';
printf("Yougotamessage(%s)from%s\n",msg,inet_ntoa(client.sin_addr));
strcpy(sbuf,msg);
sendto(sockfd,sbuf,strlen(sbuf),0,(structsockaddr*)&client,sin_size);
if(!
strcmp(msg,"quit"))break;
}
close(sockfd);//closelistenfd
return0;
}2.2UDP客户端代码
#include
#include
#include
#include
#include
#include
#include
intmain(intargc,char*argv[])
{
intfd,numbytes;
charbuf[100];
charsendbuf[20]="hello";
structhostent*he;
structsockaddr_inserver,reply;
if(argc!
=2)
{
printf("Usage:
%s\n",argv[0]);
exit
(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
printf("gethostbyname()error\n");
exit
(1);
}
if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
printf("socket");
exit
(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(3333);
server.sin_addr=*((structin_addr*)he->h_addr);
sendto(fd,sendbuf,strlen(sendbuf),0,(structsockaddr*)&server,
sizeof(structsockaddr));
while
(1)
{
intlen;
len=sizeof(structsockaddr_in);
if((numbytes=recvfrom(fd,buf,100,0,
(structsockaddr*)&reply,&len))==-1)
{
perror("recvfrom");
exit
(1);
}
//checkifserverisright
if(len!
=sizeof(structsockaddr)||memcmp((constvoid*)&server,
(constvoid*)&reply,len)!
=0)
{
printf("Receivemessagefromotherserver.\n");
continue;
}
//printfmessagefromserver
buf[numbytes]='\0';
printf("ServerMessage:
%s\n",buf);
printf("input:
");
fgets(sendbuf,sizeof(sendbuf),stdin);
sendbuf[strlen(sendbuf)-1]='\0';
sendto(fd,sendbuf,strlen(sendbuf),0,(structsockaddr*)&server,
sizeof(structsockaddr));
if(strcmp(sendbuf,"quit")==0)
break;
}
close(fd);
return0;
}
3.原始套接口基本应用
套接口服务器代码
#include
#include
#include
#include
#include
#include
#include
#definePORT1234
#defineBACKLOG5
#defineMAXDATASIZE1000
voidprocess_cli(intconnfd,structsockaddr_inclient);
main()
{
intlistenfd,connfd;
pid_tpid;
structsockaddr_inserver;
structsockaddr_inclient;
intlen;
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit
(1);
}
intopt=SO_REUSEADDR;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(listenfd,(structsockaddr*)&server,sizeof(server))==-1)
{
perror("bind");
exit
(1);
}
if(listen(listenfd,BACKLOG)==-1)
{
perror("listen");
exit
(1);
}
len=sizeof(client);
while
(1)
{
if((connfd=accept(listenfd,(structsockaddr*)&client,&len))==-1)
{
perror("accept");
exit
(1);
}
if((pid=fork())>0)
{
close(connfd);
continue;
}
elseif(pid==0)
{
close(listenfd);
process_cli(connfd,client);
exit(0);
}
else
{
perror("fork");
exit(0);
}
}
close(listenfd);
}
voidexchange(char*src,intnum)
{
charc;
inti;
src[num-1]=0;
num=num-1;
printf("%d\n",num);
for(i=0;i{
c=src[i];
src[i]=src[num-1];
src[num-1]=c;
num=num-1;
if(num<=i)
break;
}
}
voidprocess_cli(intconnfd,structsockaddr_inclient)
{
intnum;
charexitflag=0;
charrecvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],cli_name[MAXDATASIZE];
printf("gotaconnectionfrom%s.\n",inet_ntoa(client.sin_addr));
num=recv(connfd,cli_name,MAXDATASIZE,0);
if(num==-1)
{
close(connfd);
perror("recv");
return;
}
cli_name[num-1]='\0';
printf("%sclient'snameis%s.\n",inet_ntoa(client.sin_addr),cli_name);
while(num=recv(connfd,recvbuf,MAXDATASIZE,0))
{
recvbuf[num]='\0';
printf("%s:
%s",cli_name,recvbuf);
if(strcmp(recvbuf,"bye")==0)
exitflag=1;
exchange(recvbuf,num);
send(connfd,recvbuf,num,0);
if(exitflag==1)
break;
}
close(connfd);
printf("client%sfrom%send.\n",cli_name,inet_ntoa(client.sin_addr));
}
套接口客户端代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#definePORT1234
#defineMAXDATASIZE100
voidprocess(FILE*fp,intsockfd);
char*getMessage(char*sendline,intlen,FILE*fp);
intmain(intargc,char*argv[])
{
intsockfd;
structhostent*he;
structsockaddr_inserver;
if(argc!
=2)
{
printf("Usage:
%s\n",argv[0]);
exit
(1);
}
if((he=gethostbyname(argv[1]))==NULL)
{
printf("gethostbyname()error\n");
exit
(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket");
exit
(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr=*((structin_addr*)he->h_addr);
if(connect(sockfd,(structsockaddr*)&server,sizeof(server))==-1)
{
perror("connect");
exit
(1);
}
process(stdin,sockfd);
close(sockfd);
}
voidwritefile(intfd,char*str)
{
time_tt1;
structtm*t2;
charwtext[100];
t1=time(NULL);
t2=localtime(&t1);
sprintf(wtext,"%d-%d-%d-%d:
%d:
%d",t2->tm_year+1900,t2->tm_mon+1,t2->tm_mday,t2->tm_hour,t2->tm_min,t2->tm_sec);
sprintf(wtext,"%s%s\n",wtext,str);
write(fd,wtext,strlen(wtext));
}
voidprocess(FILE*fp,intsockfd)
{
charsendline[MAXDATASIZE],recvline[MAXDATASIZE];
intnum;
intfd;
time_tt1;
structtm*t2;
charcname[30];
chartmp[100];
printf("Connectedtoserver.\n");
printf("Inputclient'sname:
");
if(fgets(cname,30,fp)==NULL)
{
printf("\nExit.\n");
return;
}
send(sockfd,cname,strlen(cname),0);
cname[strlen(cname)-1]=0;
fd=open(cname,O_WRONLY|O_CREAT|O_APPEND,0644);
if(fd==-1)
{
perror("open");
}
writefile(fd,"connectionsuccess");
while(getMessage(sendline,MAXDA