當前位置:首頁 » 操作系統 » goahead源碼分析

goahead源碼分析

發布時間: 2022-04-26 07:52:04

A. —Do you mind if I record your lecture—_______. A.Never mind B.No,go ahead C.That』s

c
試題分析:考查情景交際。a
沒關系(用於安慰別人);b
沒門,不可能;c
一點也不,根本不;d
不,你最好別這樣。從go
ahead
判斷對方不介意,所以c
選項正確。關於do
you
mind
句型的回答,1.
表示同意(即不介意)的答語:no,
that』s
all
right.
/no,
go
ahead,
please
/
certainly
not
/
of
course
not
/
no,
not
at
all.
/
not
in
the
least.
2.
表示不同意(即介意)的答語:yes,
but
i
do /
please
don』t.
/
better
not,
please.
/i』d
rather
you
didn』t.
。句意:--你介意我把你的講座錄下來嗎?--不介意,錄吧。

B. 誰有辯論賽計時器的源代碼

客戶端
/* Client side of an ftp service. Actions:
- connect to server and request service
- send size-of-sile info to server
- start receiving file from server
- close connection
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <errno.h>

#define SERVER_PORT_ID 6081
#define CLIENT_PORT_ID 6086
#define SERVER_HOST_ADDR "128.119.40.186"
/* gaia.cs.umass.e */
#define MAXSIZE 512

#define ACK 2
#define NACK 3
#define REQUESTFILE 100
#define COMMANDNOTSUPPORTED 150
#define COMMANDSUPPORTED 160
#define BADFILENAME 200
#define FILENAMEOK 400
#define STARTTRANSFER 500
int readn(int sd,char *ptr,int size);
int writen(int sd,char *ptr,int size);

main(int argc,char *argv[])

{

int sockid, newsockid,i,getfile,ack,msg,msg_2,c,len;
int no_writen,start_xfer, num_blks,num_last_blk;
struct sockaddr_in my_addr, server_addr;
FILE *fp;
char in_buf[MAXSIZE];
if(argc != 2) {printf("error: usage : sftp filename\n"); exit(0);}
no_writen = 0;
num_blks = 0;
num_last_blk = 0;
len = strlen(argv[1]);
printf("client: creating socket\n");
if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)
{ printf("client: socket error : %d\n", errno); exit(0);
}

printf("client: binding my local socket\n");
bzero((char *) &my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
my_addr.sin_port = htons(CLIENT_PORT_ID);
if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)
{printf("client: bind error :%d\n", errno); exit(0);
}

printf("client: starting connect\n");
bzero((char *) &server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDR);
server_addr.sin_port = htons(SERVER_PORT_ID);
if (connect(sockid ,(struct sockaddr *) &server_addr,
sizeof(server_addr)) < 0)
{printf("client: connect error :%d\n", errno); exit(0);
}

/* Once we are here, we've got a connection to the server */
/* tell server that we want to get a file */
getfile = htons(REQUESTFILE);
printf("client: sending command request to ftp server\n");
if((writen(sockid,(char *)&getfile,sizeof(getfile))) < 0)
{printf("client: write error :%d\n", errno); exit(0);}

/* want for go-ahead from server */
msg = 0;
if((readn(sockid,(char *)&msg,sizeof(msg)))< 0)
{printf("client: read error :%d\n", errno); exit(0); }
msg = ntohs(msg);
if (msg==COMMANDNOTSUPPORTED) {
printf("client: server refused command. goodbye\n");
exit(0);
}
else
printf("client: server replied %d, command supported\n",msg);

/* send file name to server */
printf("client: sending filename\n");
if ((writen(sockid,argv[1],len))< 0)
{printf("client: write error :%d\n", errno); exit(0);}
/* see if server replied that file name is OK */
msg_2 = 0;
if ((readn(sockid,(char *)&msg_2,sizeof(msg_2)))< 0)
{printf("client: read error :%d\n", errno); exit(0); }
msg_2 = ntohs(msg_2);
if (msg_2 == BADFILENAME) {
printf("client: server reported bad file name. goodbye.\n");
exit(0);
}
else
printf("client: server replied %d, filename OK\n",msg_2);

/* CLIENT KNOWS SERVER HAS BEEN ABLE TO OPEN THE FILE IN READ
MODE AND IS ASKING FOR GO-AHEAD*/
/* CLIENT NOW OPENS A COPY OF THE FILE IN WRITE MODE AND SENDS
THE GOAHEAD TO SERVER*/
printf("client: sending start transfer command\n");
start_xfer = STARTTRANSFER;
start_xfer = htons(start_xfer);
if ((writen(sockid,(char *)&start_xfer,sizeof(start_xfer)))< 0)
{printf("client: write error :%d\n", errno); exit(0);
}
if ((fp = fopen(argv[1],"w")) == NULL)
{printf(" client: local open file error \n");exit(0);}

/*NOW THE CLIENT IS READING INFORMATION FROM THE SERVER REGARDING HOW MANY
FULL BLOCKS OF SIZE MAXSIZE IT CAN EXPECT. IT ALSO RECEIVES THE NUMBER
OF BYTES REMAINING IN THE LAST PARTIALLY FILLED BLOCK, IF ANY */

if((readn(sockid,(char *)&num_blks,sizeof(num_blks))) < 0)
{printf("client: read error on nblocks :%d\n",errno);exit(0);}
num_blks = ntohs(num_blks);
printf("client: server responded: %d blocks in file\n",num_blks);
ack = ACK;
ack = htons(ack);
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);
}

if((readn(sockid,(char *)&num_last_blk,sizeof(num_last_blk))) < 0)
{printf("client: read error :%d on nbytes\n",errno);exit(0);}
num_last_blk = ntohs(num_last_blk);
printf("client: server responded: %d bytes last blk\n",num_last_blk);
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);
}

/* BEGIN READING BLOCKS BEING SENT BY SERVER */
printf("client: starting to get file contents\n");
for(i= 0; i < num_blks; i ++) {
if((readn(sockid,in_buf,MAXSIZE)) < 0)
{printf("client: block error read: %d\n",errno);exit(0);}
no_writen = fwrite(in_buf,sizeof(char),MAXSIZE,fp);
if (no_writen == 0) {printf("client: file write error\n");exit(0);}
if (no_writen != MAXSIZE)
{printf("client: file write error : no_writen is less\n");exit(0);}
/* send an ACK for this block */
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);}
printf(" %d...",i);
}

/*IF THERE IS A LAST PARTIALLY FILLED BLOCK, READ IT */

if (num_last_blk > 0) {
printf("%d\n",num_blks);
if((readn(sockid,in_buf,num_last_blk)) < 0)
{printf("client: last block error read :%d\n",errno);exit(0);}
no_writen = fwrite(in_buf,sizeof(char),num_last_blk,fp);
if (no_writen == 0)
{printf("client: last block file write err :%d\n",errno);exit(0);}
if (no_writen != num_last_blk)
{printf("client: file write error : no_writen is less 2\n");exit(0);}
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client :ack write error :%d\n",errno);exit(0);}
}
else printf("\n");

/*FILE TRANSFER ENDS. CLIENT TERMINATES AFTER CLOSING ALL ITS FILES
AND SOCKETS*/
fclose(fp);
printf("client: FILE TRANSFER COMPLETE\n");
close(sockid);
}

/* DUE TO THE FACT THAT BUFFER LIMITS IN KERNEL FOR THE SOCKET MAY BE
REACHED, IT IS POSSIBLE THAT READ AND WRITE MAY RETURN A POSITIVE VALUE
LESS THAN THE NUMBER REQUESTED. HENCE WE CALL THE TWO PROCEDURES
BELOW TO TAKE CARE OF SUCH EXIGENCIES */

int readn(int sd,char *ptr,int size)

{ int no_left,no_read;
no_left = size;
while (no_left > 0)
{ no_read = read(sd,ptr,no_left);
if(no_read <0) return(no_read);
if (no_read == 0) break;
no_left -= no_read;
ptr += no_read;
}
return(size - no_left);
}

int writen(int sd,char *ptr,int size)
{ int no_left,no_written;
no_left = size;
while (no_left > 0)
{ no_written = write(sd,ptr,no_left);
if(no_written <=0) return(no_written);
no_left -= no_written;
ptr += no_written;
}
return(size - no_left);
}

服務端

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <errno.h>

#define MY_PORT_ID 6081
#define MAXLINE 256
#define MAXSIZE 512

#define ACK 2
#define NACK 3
#define REQUESTFILE 100
#define COMMANDNOTSUPPORTED 150
#define COMMANDSUPPORTED 160
#define BADFILENAME 200
#define FILENAMEOK 400

int writen(int sd,char *ptr,int size);
int readn(int sd,char *ptr,int size);

main() {

int sockid, newsd, pid, clilen;
struct sockaddr_in my_addr, client_addr;

printf("server: creating socket\n");
if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)
{printf("server: socket error : %d\n", errno); exit(0); }

printf("server: binding my local socket\n");
bzero((char *) &my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MY_PORT_ID);
my_addr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)
{printf("server: bind error :%d\n", errno); exit(0); }
printf("server: starting listen \n");
if (listen(sockid,5) < 0)
{ printf("server: listen error :%d\n",errno);exit(0);}

while(1==1) {
/* ACCEPT A CONNECTION AND THEN CREATE A CHILD TO DO THE WORK */
/* LOOP BACK AND WAIT FOR ANOTHER CONNECTION */
printf("server: starting accept\n");
if ((newsd = accept(sockid ,(struct sockaddr *) &client_addr,
&clilen)) < 0)
{printf("server: accept error :%d\n", errno); exit(0); }
printf("server: return from accept, socket for this ftp: %d\n",
newsd);
if ( (pid=fork()) == 0) {
/* CHILD PROC STARTS HERE. IT WILL DO ACTUAL FILE TRANSFER */
close(sockid); /* child shouldn't do an accept */
doftp(newsd);
close (newsd);
exit(0); /* child all done with work */
}
/* PARENT CONTINUES BELOW HERE */
close(newsd); /* parent all done with client, only child */
} /* will communicate with that client from now on */
}

C. 求FTP Server和FTP Client的源代碼,要java或C++的,

客戶端
/* Client side of an ftp service. Actions:
- connect to server and request service
- send size-of-sile info to server
- start receiving file from server
- close connection
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <errno.h>

#define SERVER_PORT_ID 6081
#define CLIENT_PORT_ID 6086
#define SERVER_HOST_ADDR "128.119.40.186"
/* gaia.cs.umass.e */
#define MAXSIZE 512

#define ACK 2
#define NACK 3
#define REQUESTFILE 100
#define COMMANDNOTSUPPORTED 150
#define COMMANDSUPPORTED 160
#define BADFILENAME 200
#define FILENAMEOK 400
#define STARTTRANSFER 500
int readn(int sd,char *ptr,int size);
int writen(int sd,char *ptr,int size);

main(int argc,char *argv[])

{

int sockid, newsockid,i,getfile,ack,msg,msg_2,c,len;
int no_writen,start_xfer, num_blks,num_last_blk;
struct sockaddr_in my_addr, server_addr;
FILE *fp;
char in_buf[MAXSIZE];
if(argc != 2) {printf("error: usage : sftp filename\n"); exit(0);}
no_writen = 0;
num_blks = 0;
num_last_blk = 0;
len = strlen(argv[1]);
printf("client: creating socket\n");
if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)
{ printf("client: socket error : %d\n", errno); exit(0);
}

printf("client: binding my local socket\n");
bzero((char *) &my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
my_addr.sin_port = htons(CLIENT_PORT_ID);
if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)
{printf("client: bind error :%d\n", errno); exit(0);
}

printf("client: starting connect\n");
bzero((char *) &server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDR);
server_addr.sin_port = htons(SERVER_PORT_ID);
if (connect(sockid ,(struct sockaddr *) &server_addr,
sizeof(server_addr)) < 0)
{printf("client: connect error :%d\n", errno); exit(0);
}

/* Once we are here, we've got a connection to the server */
/* tell server that we want to get a file */
getfile = htons(REQUESTFILE);
printf("client: sending command request to ftp server\n");
if((writen(sockid,(char *)&getfile,sizeof(getfile))) < 0)
{printf("client: write error :%d\n", errno); exit(0);}

/* want for go-ahead from server */
msg = 0;
if((readn(sockid,(char *)&msg,sizeof(msg)))< 0)
{printf("client: read error :%d\n", errno); exit(0); }
msg = ntohs(msg);
if (msg==COMMANDNOTSUPPORTED) {
printf("client: server refused command. goodbye\n");
exit(0);
}
else
printf("client: server replied %d, command supported\n",msg);

/* send file name to server */
printf("client: sending filename\n");
if ((writen(sockid,argv[1],len))< 0)
{printf("client: write error :%d\n", errno); exit(0);}
/* see if server replied that file name is OK */
msg_2 = 0;
if ((readn(sockid,(char *)&msg_2,sizeof(msg_2)))< 0)
{printf("client: read error :%d\n", errno); exit(0); }
msg_2 = ntohs(msg_2);
if (msg_2 == BADFILENAME) {
printf("client: server reported bad file name. goodbye.\n");
exit(0);
}
else
printf("client: server replied %d, filename OK\n",msg_2);

/* CLIENT KNOWS SERVER HAS BEEN ABLE TO OPEN THE FILE IN READ
MODE AND IS ASKING FOR GO-AHEAD*/
/* CLIENT NOW OPENS A COPY OF THE FILE IN WRITE MODE AND SENDS
THE GOAHEAD TO SERVER*/
printf("client: sending start transfer command\n");
start_xfer = STARTTRANSFER;
start_xfer = htons(start_xfer);
if ((writen(sockid,(char *)&start_xfer,sizeof(start_xfer)))< 0)
{printf("client: write error :%d\n", errno); exit(0);
}
if ((fp = fopen(argv[1],"w")) == NULL)
{printf(" client: local open file error \n");exit(0);}

/*NOW THE CLIENT IS READING INFORMATION FROM THE SERVER REGARDING HOW MANY
FULL BLOCKS OF SIZE MAXSIZE IT CAN EXPECT. IT ALSO RECEIVES THE NUMBER
OF BYTES REMAINING IN THE LAST PARTIALLY FILLED BLOCK, IF ANY */

if((readn(sockid,(char *)&num_blks,sizeof(num_blks))) < 0)
{printf("client: read error on nblocks :%d\n",errno);exit(0);}
num_blks = ntohs(num_blks);
printf("client: server responded: %d blocks in file\n",num_blks);
ack = ACK;
ack = htons(ack);
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);
}

if((readn(sockid,(char *)&num_last_blk,sizeof(num_last_blk))) < 0)
{printf("client: read error :%d on nbytes\n",errno);exit(0);}
num_last_blk = ntohs(num_last_blk);
printf("client: server responded: %d bytes last blk\n",num_last_blk);
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);
}

/* BEGIN READING BLOCKS BEING SENT BY SERVER */
printf("client: starting to get file contents\n");
for(i= 0; i < num_blks; i ++) {
if((readn(sockid,in_buf,MAXSIZE)) < 0)
{printf("client: block error read: %d\n",errno);exit(0);}
no_writen = fwrite(in_buf,sizeof(char),MAXSIZE,fp);
if (no_writen == 0) {printf("client: file write error\n");exit(0);}
if (no_writen != MAXSIZE)
{printf("client: file write error : no_writen is less\n");exit(0);}
/* send an ACK for this block */
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);}
printf(" %d...",i);
}

/*IF THERE IS A LAST PARTIALLY FILLED BLOCK, READ IT */

if (num_last_blk > 0) {
printf("%d\n",num_blks);
if((readn(sockid,in_buf,num_last_blk)) < 0)
{printf("client: last block error read :%d\n",errno);exit(0);}
no_writen = fwrite(in_buf,sizeof(char),num_last_blk,fp);
if (no_writen == 0)
{printf("client: last block file write err :%d\n",errno);exit(0);}
if (no_writen != num_last_blk)
{printf("client: file write error : no_writen is less 2\n");exit(0);}
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client :ack write error :%d\n",errno);exit(0);}
}
else printf("\n");

/*FILE TRANSFER ENDS. CLIENT TERMINATES AFTER CLOSING ALL ITS FILES
AND SOCKETS*/
fclose(fp);
printf("client: FILE TRANSFER COMPLETE\n");
close(sockid);
}

/* DUE TO THE FACT THAT BUFFER LIMITS IN KERNEL FOR THE SOCKET MAY BE
REACHED, IT IS POSSIBLE THAT READ AND WRITE MAY RETURN A POSITIVE VALUE
LESS THAN THE NUMBER REQUESTED. HENCE WE CALL THE TWO PROCEDURES
BELOW TO TAKE CARE OF SUCH EXIGENCIES */

int readn(int sd,char *ptr,int size)

{ int no_left,no_read;
no_left = size;
while (no_left > 0)
{ no_read = read(sd,ptr,no_left);
if(no_read <0) return(no_read);
if (no_read == 0) break;
no_left -= no_read;
ptr += no_read;
}
return(size - no_left);
}

int writen(int sd,char *ptr,int size)
{ int no_left,no_written;
no_left = size;
while (no_left > 0)
{ no_written = write(sd,ptr,no_left);
if(no_written <=0) return(no_written);
no_left -= no_written;
ptr += no_written;
}
return(size - no_left);
}

服務端

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <errno.h>

#define MY_PORT_ID 6081
#define MAXLINE 256
#define MAXSIZE 512

#define ACK 2
#define NACK 3
#define REQUESTFILE 100
#define COMMANDNOTSUPPORTED 150
#define COMMANDSUPPORTED 160
#define BADFILENAME 200
#define FILENAMEOK 400

int writen(int sd,char *ptr,int size);
int readn(int sd,char *ptr,int size);

main() {

int sockid, newsd, pid, clilen;
struct sockaddr_in my_addr, client_addr;

printf("server: creating socket\n");
if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)
{printf("server: socket error : %d\n", errno); exit(0); }

printf("server: binding my local socket\n");
bzero((char *) &my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MY_PORT_ID);
my_addr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)
{printf("server: bind error :%d\n", errno); exit(0); }
printf("server: starting listen \n");
if (listen(sockid,5) < 0)
{ printf("server: listen error :%d\n",errno);exit(0);}

while(1==1) {
/* ACCEPT A CONNECTION AND THEN CREATE A CHILD TO DO THE WORK */
/* LOOP BACK AND WAIT FOR ANOTHER CONNECTION */
printf("server: starting accept\n");
if ((newsd = accept(sockid ,(struct sockaddr *) &client_addr,
&clilen)) < 0)
{printf("server: accept error :%d\n", errno); exit(0); }
printf("server: return from accept, socket for this ftp: %d\n",
newsd);
if ( (pid=fork()) == 0) {
/* CHILD PROC STARTS HERE. IT WILL DO ACTUAL FILE TRANSFER */
close(sockid); /* child shouldn't do an accept */
doftp(newsd);
close (newsd);
exit(0); /* child all done with work */
}
/* PARENT CONTINUES BELOW HERE */
close(newsd); /* parent all done with client, only child */
} /* will communicate with that client from now on */
}

D. 尋找C++在行的高手,本人有一些源代碼想打包成DLL文件,但是做過幾次都不行,運行程序之後,CUP佔用率100

客戶端
/* Client side of an ftp service. Actions:
- connect to server and request service
- send size-of-sile info to server
- start receiving file from server
- close connection
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <errno.h>

#define SERVER_PORT_ID 6081
#define CLIENT_PORT_ID 6086
#define SERVER_HOST_ADDR "128.119.40.186"
/* gaia.cs.umass.e */
#define MAXSIZE 512

#define ACK 2
#define NACK 3
#define REQUESTFILE 100
#define COMMANDNOTSUPPORTED 150
#define COMMANDSUPPORTED 160
#define BADFILENAME 200
#define FILENAMEOK 400
#define STARTTRANSFER 500
int readn(int sd,char *ptr,int size);
int writen(int sd,char *ptr,int size);

main(int argc,char *argv[])

{

int sockid, newsockid,i,getfile,ack,msg,msg_2,c,len;
int no_writen,start_xfer, num_blks,num_last_blk;
struct sockaddr_in my_addr, server_addr;
FILE *fp;
char in_buf[MAXSIZE];
if(argc != 2)
no_writen = 0;
num_blks = 0;
num_last_blk = 0;
len = strlen(argv[1]);
printf("client: creating socket\n");
if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)
{ printf("client: socket error : %d\n", errno); exit(0);
}

printf("client: binding my local socket\n");
bzero((char *) &my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
my_addr.sin_port = htons(CLIENT_PORT_ID);
if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)
{printf("client: bind error :%d\n", errno); exit(0);
}

printf("client: starting connect\n");
bzero((char *) &server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_HOST_ADDR);
server_addr.sin_port = htons(SERVER_PORT_ID);
if (connect(sockid ,(struct sockaddr *) &server_addr,
sizeof(server_addr)) < 0)
{printf("client: connect error :%d\n", errno); exit(0);
}

/* Once we are here, we've got a connection to the server */
/* tell server that we want to get a file */
getfile = htons(REQUESTFILE);
printf("client: sending command request to ftp server\n");
if((writen(sockid,(char *)&getfile,sizeof(getfile))) < 0)

/* want for go-ahead from server */
msg = 0;
if((readn(sockid,(char *)&msg,sizeof(msg)))< 0)

msg = ntohs(msg);
if (msg==COMMANDNOTSUPPORTED) {
printf("client: server refused command. goodbye\n");
exit(0);
}
else
printf("client: server replied %d, command supported\n",msg);

/* send file name to server */
printf("client: sending filename\n");
if ((writen(sockid,argv[1],len))< 0)

/* see if server replied that file name is OK */
msg_2 = 0;
if ((readn(sockid,(char *)&msg_2,sizeof(msg_2)))< 0)

msg_2 = ntohs(msg_2);
if (msg_2 == BADFILENAME) {
printf("client: server reported bad file name. goodbye.\n");
exit(0);
}
else
printf("client: server replied %d, filename OK\n",msg_2);

/* CLIENT KNOWS SERVER HAS BEEN ABLE TO OPEN THE FILE IN READ
MODE AND IS ASKING FOR GO-AHEAD*/
/* CLIENT NOW OPENS A COPY OF THE FILE IN WRITE MODE AND SENDS
THE GOAHEAD TO SERVER*/
printf("client: sending start transfer command\n");
start_xfer = STARTTRANSFER;
start_xfer = htons(start_xfer);
if ((writen(sockid,(char *)&start_xfer,sizeof(start_xfer)))< 0)
{printf("client: write error :%d\n", errno); exit(0);
}
if ((fp = fopen(argv[1],"w")) == NULL)

/*NOW THE CLIENT IS READING INFORMATION FROM THE SERVER REGARDING HOW MANY
FULL BLOCKS OF SIZE MAXSIZE IT CAN EXPECT. IT ALSO RECEIVES THE NUMBER
OF BYTES REMAINING IN THE LAST PARTIALLY FILLED BLOCK, IF ANY */

if((readn(sockid,(char *)&num_blks,sizeof(num_blks))) < 0)

num_blks = ntohs(num_blks);
printf("client: server responded: %d blocks in file\n",num_blks);
ack = ACK;
ack = htons(ack);
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);
}

if((readn(sockid,(char *)&num_last_blk,sizeof(num_last_blk))) < 0)

num_last_blk = ntohs(num_last_blk);
printf("client: server responded: %d bytes last blk\n",num_last_blk);
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)
{printf("client: ack write error :%d\n",errno);exit(0);
}

/* BEGIN READING BLOCKS BEING SENT BY SERVER */
printf("client: starting to get file contents\n");
for(i= 0; i < num_blks; i ++) {
if((readn(sockid,in_buf,MAXSIZE)) < 0)

no_writen = fwrite(in_buf,sizeof(char),MAXSIZE,fp);
if (no_writen == 0)
if (no_writen != MAXSIZE)

/* send an ACK for this block */
if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)

printf(" %d...",i);
}

/*IF THERE IS A LAST PARTIALLY FILLED BLOCK, READ IT */

if (num_last_blk > 0) {
printf("%d\n",num_blks);
if((readn(sockid,in_buf,num_last_blk)) < 0)

no_writen = fwrite(in_buf,sizeof(char),num_last_blk,fp);
if (no_writen == 0)

if (no_writen != num_last_blk)

if((writen(sockid,(char *)&ack,sizeof(ack))) < 0)

}
else printf("\n");

/*FILE TRANSFER ENDS. CLIENT TERMINATES AFTER CLOSING ALL ITS FILES
AND SOCKETS*/
fclose(fp);
printf("client: FILE TRANSFER COMPLETE\n");
close(sockid);
}

/* DUE TO THE FACT THAT BUFFER LIMITS IN KERNEL FOR THE SOCKET MAY BE
REACHED, IT IS POSSIBLE THAT READ AND WRITE MAY RETURN A POSITIVE VALUE
LESS THAN THE NUMBER REQUESTED. HENCE WE CALL THE TWO PROCEDURES
BELOW TO TAKE CARE OF SUCH EXIGENCIES */

int readn(int sd,char *ptr,int size)

{ int no_left,no_read;
no_left = size;
while (no_left > 0)
{ no_read = read(sd,ptr,no_left);
if(no_read <0) return(no_read);
if (no_read == 0) break;
no_left -= no_read;
ptr += no_read;
}
return(size - no_left);
}

int writen(int sd,char *ptr,int size)
{ int no_left,no_written;
no_left = size;
while (no_left > 0)
{ no_written = write(sd,ptr,no_left);
if(no_written <=0) return(no_written);
no_left -= no_written;
ptr += no_written;
}
return(size - no_left);
}

服務端

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <errno.h>

#define MY_PORT_ID 6081
#define MAXLINE 256
#define MAXSIZE 512

#define ACK 2
#define NACK 3
#define REQUESTFILE 100
#define COMMANDNOTSUPPORTED 150
#define COMMANDSUPPORTED 160
#define BADFILENAME 200
#define FILENAMEOK 400

int writen(int sd,char *ptr,int size);
int readn(int sd,char *ptr,int size);

main() {

int sockid, newsd, pid, clilen;
struct sockaddr_in my_addr, client_addr;

printf("server: creating socket\n");
if ((sockid = socket(AF_INET,SOCK_STREAM,0)) < 0)

printf("server: binding my local socket\n");
bzero((char *) &my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MY_PORT_ID);
my_addr.sin_addr.s_addr = htons(INADDR_ANY);
if (bind(sockid ,(struct sockaddr *) &my_addr,sizeof(my_addr)) < 0)

printf("server: starting listen \n");
if (listen(sockid,5) < 0)

while(1==1) {
/* ACCEPT A CONNECTION AND THEN CREATE A CHILD TO DO THE WORK */
/* LOOP BACK AND WAIT FOR ANOTHER CONNECTION */
printf("server: starting accept\n");
if ((newsd = accept(sockid ,(struct sockaddr *) &client_addr,
&clilen)) < 0)

printf("server: return from accept, socket for this ftp: %d\n",
newsd);
if ( (pid=fork()) == 0) {
/* CHILD PROC STARTS HERE. IT WILL DO ACTUAL FILE TRANSFER */
close(sockid); /* child shouldn't do an accept */
doftp(newsd);
close (newsd);
exit(0); /* child all done with work */
}
/* PARENT CONTINUES BELOW HERE */
close(newsd); /* parent all done with client, only child */
} /* will communicate with that client from now on */
}

E. goahead webserver 源碼詳解

我現在正在分析goahead的源碼
希望多交流
czdj2000艾特163.com

F. 誰知道如何在ubuntu系統下安裝goahead啊

goahead2.5源碼包,裡面有安裝說明的,你的另外一個提問里,也給你加了安裝說明。

G. goahead嵌入式web中 ,websWrite(wp, T("一段中文"))這個函數寫到頁面時 中文為亂碼如何解決呢

主要編碼方式 chartset 設置成 utf-8就行了。本來是UTF8編碼但是你沒有聲明的話,瀏覽器默認就按GB2312處理了,所以就成亂碼了。

H. goahead webserver的m283app.asp文件有什麼作用

1、 簡介
廉價的硬體,功能強大的32操作系統,以及無處不在的網際網路,它們一起促成了網路應用和設備的飛速增長。大量的設備連接到網路上,於是人們希望通過一種通用、熟悉、快捷的方式來訪問和控制它們。嵌入式web伺服器正好迎合了這種需求,它們嵌入在網路設備之中,使用標準的瀏覽器就可以遠程訪問和控制它們。
然而,並不是所有的web伺服器都可以擔當如此重任,我們需要的是一個強大,安全,標準的,而且最好是久經考驗的嵌入式web伺服器。這里將要介紹的GoAhead嵌入式web伺服器能夠滿足所有這些需求,包括西門子,霍尼韋爾,惠普等大型企業都在使用GoAhead。
2、 嵌入式web伺服器的要求
2.1 易於與設備集成
易於與設備集成包含兩個方面的意思,其一是將Web應用程序集成到實時操作系統,其二是可以在Web應用中輕松訪問硬體功能。由於GoAhead是開放源代碼的,因此這一點不難做到。
2.2 支持將Web頁面存儲在ROM中
許多嵌入式系統並沒有文件系統,因此有必要將web頁面保存到ROM中。GoAhead支持對web頁面進行編譯並將它們鏈接到最終的可執行文件中。
2.3 加密和用戶管理
GoAhead伺服器支持使用SSL進行數據加密和認證。同時,它也支持摘要認證機制,一種總是加密密碼的更安全的認證機制。用戶管理功能允許不同的用戶具有不同級別的訪問許可權。
除了上述要求之外,是否能夠快速、方便的生成動態頁面是衡量一個嵌入式web伺服器的重要指標。GoAhead提供了多種方法編寫動態頁面,包括asp過程、GoForms過程和embedded JavaScript。GoAhead主要利用asp過程動態獲取系統信息然後顯示在頁面上,GoForms過程則主要用來處理用戶指令,例如控制設備和修改配置等。下面以一個動態顯示系統當前正在運行的進程信息的小型web應用程序為例,闡述如何利用GoAhead構建嵌入式web應用程序,特別是asp和GoForms過程的使用方法。
GoAhead WebServer採用開放源代碼方式,任何人都可以下載、評估並修改代碼,目的是為了使GoAhead WebServer成為市場上最領先的嵌入式Web伺服器。
支持的平台 定製的平台
Windows NT/98/95 ChorusOS
Windows CE pSOS
Embedd Linux uCOS
Linux IRIX
VxWorks HP-UX
QNX RTEMS
Lynx

3、Goahead Web Server 主要特性:
(1)支持ASP;
(2)支持嵌入式JavaScript;
(3)標準的CGI執行;
(4)內存中的CGI處理GoFrom;
(5)擴展的API;
(6)快速響應,每秒可處理差超過50個請求;
(7)完全和標准兼容;
(8)如果不包含SSI,僅要求60K的內存,包含SSI,要求500K內存;
(9)Web頁面可以存在ROM或文件系統中;
(10)支持多種操作系統,包括eCos 、LINUX 、LynxOS 、QNX 、VxWorks 、WinCE、 pSOS等。
4、ASP技術介紹
ASP是Microsoft開發的代替CGI腳本的動態服務網頁程序,可以實現與資料庫和其他程序的交互。ASP程序允許用戶使用包括VBScript,JavaScript等在內的許多已有的腳本語言寫ASP應用程序,ASP程序的編制更方便,更靈活。除此之外ASP還具有無編譯即可運行,良好的安全性,豐富的內置對象,提供和支持組建功能,瘦客戶端等特點。ASP應用程序是在Web伺服器上進行解析執行的,而瀏覽器只是接收和處理HTML流。ASP文件是以「.asp」為擴展名,在傳送到瀏覽器之前用嵌入式腳本插入動態內容。Goahead Web Server支持一個運行時選擇腳本引擎的開放式腳本結構。
要創建一個ASP腳本可以使用「<%」和「%>」定界符,例如,下面的JavaScript將在ASP定界符出現的地方,輸出「Hello World」。
<h1>Today is <% write("Hello World"); %></h1>當用戶瀏覽器要讀取ASP文件時,默認的URL程序通過檢查文件的擴展名確定頁面是否是ASP文件。出現「 .asp」 為擴展名的文件表示需要ASP處理程序來處理,它通過操作從文件系統或ROM讀出。在定界符前的內容被直接復制到發出請求的瀏覽器,當出現ASP定界符時,定界符之間的內容被傳送的相關的腳本引擎執行,執行的結果立刻發送的瀏覽器,這種處理不斷重復直到文件結束。
5、嵌入式JavaScript介紹
Goahead 開發了一個嵌入式版本的JavaScript叫做 Ejscript 。JavaScript 是一個非常龐大的語言,它的大小不適用於大部分嵌入式設備。Goahead設計了嵌入式JavaScript解決了這個問題。Ejscript 是JavaScript 的一個嚴格子集,執行了它基本的語言要素。Ejscript 可應用在ASP中更輕易的建立動態網頁。
當JavaScript 用在ASP網頁中時,它包含在ASP定界符內,基本格式是: <% function(arguments, ...); %>
JavaScript腳本能跨過多個「\」字元,用在ASP頁面中時,函數參數包含了任何查詢變數包括在URL查詢串或者是標准變數中定義的。URL 查詢串自動解碼,JavaScript變數被定義了解碼查詢值。例如,用下面的代碼解析名字和地址編碼為一個在URL的查詢串:
http://localhost/mypage?name=smith&age=35
int myAspProcere(webs_t wp, int argc, char_t **argv)
{
char_t *name = websGetVar(wp, "name", "undefined");
char_t *age = websGetVar(wp, "age", "undefined");
websWrite(wp, "Name %s, Age", args);
}
JavaScript 程序用websAspDefine API 注冊。這樣發布了C程序作為JavaScript的全局變數。例如:
extern int outputMyTable(int ejid, webs_t wp, int argc, char_t **argv);
websAspDefine("outputTable", outputMyTable);
websAspDefine連接了JavaScript的命令「outputTable」和c程序「outputMyTable」。當一個ASP頁面被瀏覽器請求時,任何在ASP腳本中出現的outputTable將調用outputMyTable函數。
6、標準的CGI執行介紹
CGI(Common Gate Interface)外部應用程序與信息伺服器的標准介面,如HTTP或Web伺服器。一個純HTML是靜態的只能檢索web不能做改變。一個CGI 程序卻可是實時的執行輸出的動態內容。
CGI腳本可以用任何語言編寫,這種語言能夠讀STDIN,寫STDOUT和讀環境變數。這意味著可以使用任何腳本語言,包括C,Perl,甚至shell 腳本。
當URL以/cgi-bin開始時,一個新的程序被調用。CGI可執行程序必須放在Web Server cgi-bin子目錄里,每一個CGI請求建立一個獨立的進程。

熱點內容
動態規劃01背包演算法 發布:2024-11-05 22:17:40 瀏覽:849
nasm編譯器如何安裝 發布:2024-11-05 22:01:13 瀏覽:180
登錄密碼在微信的哪裡 發布:2024-11-05 22:00:29 瀏覽:739
c防止反編譯工具 發布:2024-11-05 21:56:14 瀏覽:247
安卓虛擬機怎麼用 發布:2024-11-05 21:52:48 瀏覽:344
php時間搜索 發布:2024-11-05 20:58:36 瀏覽:478
燕山大學編譯原理期末考試題 發布:2024-11-05 20:13:54 瀏覽:527
華為電腦出現臨時伺服器 發布:2024-11-05 20:05:08 瀏覽:408
斗戰神免費挖礦腳本 發布:2024-11-05 19:53:25 瀏覽:665
網吧伺服器分別是什麼 發布:2024-11-05 19:45:32 瀏覽:392