网络应用的基本模型是客户机/服务器模型,这是一个不对称的编程模型,通信的双方扮演不同的角色:客户机和服务器。本题中的程序,客户机接收用户在键盘上输入的文字内容,服务器将客户机发送来的文字内容直接返回给客户机。 此程序中,用户自定义函数有: int read_all( int fd, void*buf, int nbyte ); 函数read all从参数fd指定的套接字描述符中读取nbytes字节数据至缓冲区buf中,成功返回实际读的字节数(可能小于nbyte),失败返回-1。 int write_all( int fd, void*buf, int nbyte ); 函数write_all向参数fd指定的套接字描述符中写入缓冲区buf前nbyte字节的数据,成功返回实际写的字节数(始终等于nbyte),失败返回-1。 write_requ函数为客户机发送请求的函数;read_requ函数为服务器获取请求的函数 服务器主程序部分: #define SERVER_PORT 8080 //服务器监听端口号为8080 #define BACKLOG 5 //连接请求队列长度 int main( int argc, char*argv[]) { int listenfd, connfd //监听套接字、连接套接字描述符 struct sockaddr_in servaddr; //服务器监听地址 listenfd=(1); //创建用于监听的套接字 if (listenfd<0) { fPrintf( stderr,"创建套接字错误!"); exit(1); } //套接字创建失败时打印错误信息 bzero(&servaddr.sizeof(servadd));//将地址结构置空 servaddr.sin_family=AF_INET;//设置地址结构遵循TCP/IP协议 servaddr.sin_addrs_addr=htonl.(2);//设置监听的IP地址为任意合法地址,并将该地址转换为网络字节顺序 servaddr.sin_port=(3);//设置监听的端口,并转化为网络字节顺序 if ( bind(4)<0 ) { fprintf( stderr,"绑定套接字与地址!"); exit(1); } //将监听地址与用于监听的套接字绑定,绑定失败时打印错误信息 if ( listen( listedfd, BACKLOG)<0) { fprintf( stderr,"转换套接字为监听套接字!"); exit(1); } //将用于监听的套接字由普通套接字转化为监听套接字 for(;){ connfd=(5); //从监听套接字的连接队列中接收已经完成的连接,并创建新的连接套接字 if(connfd<0){ fprintf(stderr,"接收连接失败!"); exit(1); } //接收失败打印错误信息 serv_respon(connfd); //运行服务器的处理函数 (6);//关闭连接套接字 } close(listenfd);//关闭监听套接字 } 服务器通信部分: #include<stdio.h> ……//引用头文件部分略> void serv_respon( int sockfd) { int nbytes; char buf[1024]; for(;;) { nbytes=read_requ(sockfd, buf, 1024); //读出客户机发出的请求,并分析其中的协议结构,获知请求的内容部分的长度,并将内容复制到缓冲区buf中, if ( nbytes=0) return;//如客户机结束发送就退出 else if ( bytes<0 ) { fprintf( siderr,"读错误情息:%s\n", sterror( errno )); return; }//读请求错误打印错误信息 if ( write_all ( sockfd, buf, nbytes)<0) //将请求中的内容部分反向发送回客户机 fprintf( siderr,"写错误信息:%s\n", strerror( errno ) ); } } int read_requ( int sockfd, char*buf int size ) { char inbuf[256]; int n; int i; i=read_line( sockfd, inbuf, 256 ); //从套接字接收缓冲区中读出一行数据,该数据为客户请求的首部 if(1<O)return(1); else if ( i=0 ) return(0); if ( strncmp( inbuf,"",6 )=0) sscanf( (7),"%d", &n );//从缓冲区buf中读出长度信息 else{ sprintf( buf," ",14 ); return(14); }//取出首部Length域中的数值,该数值为内容部分的长度 return( read_all( sockfd, buf, n ) );//从接收缓冲区中读出请求的内容部分 } int get_char(int fd, char*ch) { static int offset=0; static int size=0; static char buff[1024]; //声明静态变量,在get_char多次被调用期间,该变量的内存不释放 for ( ;size<=0 ||(8);) { size=read(fd,buf,1024);//一次从套接字缓冲区中读出一个数据块 if ( size<0 ) { if ( errno=EINTR ) { size=0; confine; //EINTR表示本次读操作没有成功,但可以继续使用该套接字读出数i }else return(-1); } offset=0;//读出数据后,将偏址置为0 } *ch=buf[(9)];//将当前的字符取出,并将偏址移向下一字符 return(1); } int read_line(int fd, char*buf, int maxlen) { int i,n; char ch; for ( i=0; i<maxlen;) { n = get_char( fd, &ch );//取出一个字符 if ( n==1 ){ buff[i++]=ch;//将字符加入字符串中 if ( (10) break; }else if ( n< ) return(-1); else break; } buf[i]=’\0’; return(i); } //函数read_line的作用是读出请求的首部,其处理的方法是每次调用get_char函数,取出一个字符,检查该字符是否是回车符‘\n’,如果是回车符,就返回请求的首部。 //get_char的处理方式较为特殊,并不是每次调用read函数读一个字符,而是一次从缓冲区中读一块内容,再一次一个字符提交给函数read_line,如果提交完了就再读一块,这样就可以提高读缓冲区的效率。另外,由于客户机是分两次调用writ_all函数将请求的首部和内容发送给服务器,因此get_char不会取出请求内容部分的字符。
查看答案
Passage One For more than thirty years after astronauts first set foot on the Moon, scientists have been unable to unravel the mystery of where the Earth’s only satellite came from. But now there is direct evidence that the Moon was born after a giant collision between the young Earth and another planet. Previous studies of rocks from the Earth and the Moon have been unable to distinguish between the two, suggesting that they formed from the same material. But this still left room for a number of theories explaining how—for example, that the Moon and Earth formed from the same material at the same time. It was even suggested that the early Earth spun so fast it formed a bulge that eventually broke off to form the Moon. Franck Poitrasson, and his colleagues at the Swiss Federal Institute of Technology have compared Moon rocks with rocks from Earth and discovered a surprising difference. They analysed the weight of the elements present in the rock using a highly accurate form of mass spectroscopy(光谱研究) that involves vaporising a sample by passing it through an argon (氩) flame. Although they appeared very similar in most respects, the Moon rocks had a higher ratio of iron-57 to iron-54 isotopes(同位素)than the Earth rocks. "The only way we could explain this difference is that the Moon and the Earth were partly vaporised during their formation," says Poitrasson. Only the popular "giant planetary impact" theory could generate the temperatures of more than 1700℃ needed to vaporise iron. In this scenario, a Mars-sized planet known as Theia crashed into Earth 50 million years after the birth of the Solar System. This catastrophic collision would have released 100 million times more energy than the impact believed to have wiped out the dinosaurs—enough to melt and vaporise a large portion of the Earth and completely destroy Theia. The debris from the collision would have been thrown into orbit around the Earth and eventually coalesced to form the Moon. When iron is vaporised, the lighter isotopes burn off first. And since the ejected debris that became the Moon would have been more thoroughly vaporised, it would have lost a greater proportion of its lighter iron isotopes than Earth did. This would explain the different ratios that Poitrasson has found. Which of the following statement is NOT TRUE according to the passage
A. The Moon was born after a giant collision between the young Earth and another planet.
B. The Moon rocks had a higher ratio of iron 57 to iron 54 isotopes than the Earth rocks.
C. The Moon and the Earth have almost the same portion of iron.
D. This catastrophic collision would have released much more energy than the impact believed to have wiped out the dinosaurs.
Passage Three Scraps of food could soon be helping power your home, thanks to an ultra-cheap bacteria-driven battery. Its developers hope that instead of feeding the dog or making garden compost(混合肥料) ,organic household waste could top up your home’s electricity. Although such "microbial fuel cells" (MFCs)have been developed in the past, they have always proved extremely inefficient and expensive. Now Chris Melhuish and technologists at the University of the West of England(UWE)in Bristol have come up with a simplified MFC that costs as little as £10 to make. Right now, their fuel cell runs only on sugar cubes, since these produce almost no waste when broken down, but they aim to move on to carrot power. "It has to be able to use raw materials, rather than giving it a refined fuel," says Melhuish. Inside the Walkman-sized battery, a colony of E. coil bacteria produce enzymes that break down carbohydrates, releasing hydrogen atoms. The cell also contains chemicals that drive a series of redox, or reduction and oxidation reactions, stripping electrons from the hydrogen atoms and delivering them steadily to the fuel cell’s anode(正极). This creates a voltage that can be used to power a circuit. To prove the MFC works, the researchers are using it to power a small light-sensitive robot. And when a number of the cells are connected in series, they could power domestic appliances, running a 40-watt bulb for eight hours on about 50 grams of sugar. Earlier MFCs were inefficient because they relied on energy-hungry filters and pumps. By experimenting with different anode materials, the UWE team have figured out how to make their system work: they dump the bacteria and redox chemicals directly into the cell. In its current form, the UWE team says its organic battery can produce eight times as much power as any previous MFC. But Melhuish wants to improve this, both by scaling it up and finding a better mix of redox chemicals. Earlier MFCs were inefficient because they ______.
A. produced very little electricity
B. required different anode materials
C. consumed much energy on filters and pumps
D. could only run domestic appliances
Why did man have to track his target at a close range when using a short bow
A. Because it was too heavy.
Because it did not bend easily.
C. Because it did not shoot far.
D. Because its string was short.
Directions: This section is designed to test your ability to understand spoken English. You will hear a selection of recorded materials and you must answer the questions that accompany them. There are two parts in this section, Part A and Part B. Remember, while you are doing the test, you should first put down your answers in your test booklet. At the end of the listening comprehension section, you will have 3 minutes to transfer your answers from your test booklet onto your ANSWER SHEET 1. If you have any questions, you may raise your hand. Now as you will not be allowed to speak once the test is started. Now look at Part A in your test booklet.Part A You will hear 10 short dialogues. For each dialogue, there is one question and four possible answers. Choose the correct answer--A, B, C or D, and mark it in your test booklet. You will have 15 seconds to answer the question and you will hear each dialogue ONLY ONCE. Now look at question 1 What does the woman imply
A. She hasn’t seen George since the baby was born.
B. George never smokes.
C. She wants to visit George and the baby.
D. George no longer smokes.