📄 simpleserver.c
字号:
#include "types.h"
#define SERVERPORT 6000
#define BACKLOG 10 //define pending queue length
int ServerSocket; //Server Socket
struct sockaddr_in ServerAddress;
int insocket; //in Socket
struct sockaddr_in inaddress;
void fatal( char * );
void server();
int path_get( char * fname, struct myfs_meta * pmeta );
void fatal( char *text )
{
printf( "Error in %s\n", text );
exit( 1 );
}
ssize_t readn(int fd, void * vptr, size_t n)
{
ssize_t nleft;
ssize_t nread;
char * ptr;
ptr = vptr;
nleft = n;
while(nleft > 0){
if((nread = read(fd,ptr,nleft)) < 0){
if(errno == EINTR)
nread = 0;
else
return -1;
}
else
if(nread == 0) break;
nleft -= nread;
ptr += nread;
}
return (n-nleft);
}
ssize_t writen(int fd, const void * vptr, size_t n)
{
ssize_t nleft;
ssize_t nwriten;
const char * ptr;
ptr = vptr;
nleft = n;
while(nleft > 0){
if( (nwriten = write(fd, ptr,nleft)) <= 0){
if(errno == EINTR)
nwriten = 0;
else
return -1;
}
nleft -= nwriten;
ptr += nwriten;
}
return n;
}
void server()
{
int addrlen, length, counter;
short optval;
char *SndBuffer, *RcvBuffer;
if( (ServerSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
fatal( "Create Socket.\n" );
ServerAddress.sin_family = AF_INET;
ServerAddress.sin_port = htons( SERVERPORT );
ServerAddress.sin_addr.s_addr =htonl(INADDR_ANY);
bzero( &( ServerAddress.sin_zero), 8 );
addrlen = sizeof( ServerAddress );
if( bind( ServerSocket, (struct sockaddr*)&ServerAddress, addrlen ) == -1 )
fatal( "Server Binding.\n" );
if( listen( ServerSocket, BACKLOG ) == -1 )
fatal( "Server listening.\n" );
printf(" listening the connection\n");
while( 1 ){ //loop waiting
addrlen = sizeof( ServerSocket );
if( (insocket = accept( ServerSocket, &inaddress, &addrlen )) == -1 ){
printf( "Accept Error!\n" );
continue;
}
printf( "Get a connection from the other side!\n" );
if( !fork() ){ //This is the child process in charge of the connection
RcvBuffer = (char*) malloc( sizeof(struct outmessage) );
SndBuffer = (char*) malloc( sizeof(struct inmessage) );
if( readn( insocket, RcvBuffer, sizeof(struct outmessage)) == -1 ){
printf( "Child process receive error!\n" );
close( insocket );
exit( 0 );
}
printf( "the directory is :%s\n", ((struct outmessage*)RcvBuffer)->dirname);
((struct inmessage*)SndBuffer)->type = ((struct outmessage*)RcvBuffer)->type;
if ( path_get( ((struct outmessage*)RcvBuffer)->dirname,
(struct myfs_meta*)&(((struct inmessage*)SndBuffer)->meta) ) <0 ){
printf( "path_get failed!\n" );
}
if( writen( insocket, SndBuffer, sizeof(struct outmessage)) == -1 ){
printf( "Child process send error!\n" );
close( insocket );
exit( 0 );
}
free (RcvBuffer);
free (SndBuffer);
close( insocket ); //parent do not need this
exit(0);
}//child process
} //while
}
int path_get( char * fname, struct myfs_meta * pmeta )
{
struct stat filestat;
int fd, len, is_dir = 0;
char nbuf[MAXPATHLEN];
char tmpbuf[4096];
/* // strip off any trailing /'s on the file name
for (len = strlen(nbuf); len > 0 && nbuf[len] == '/'; nbuf[len--] = '\0');
if (len < 0) return(-1);
if (stat(nbuf, &filestat) < 0 || !S_ISDIR(filestat.st_mode)) {
printf( "The file %s is not a directory.\n", nbuf );
}
is_dir = 1;
if ((fd = open(nbuf, O_RDONLY, 0)) < 0) return -1;
lseek(fd, 0, SEEK_SET);
if (read(fd, tmpbuf, 4096) < 0) return -1;
// read the set values out of the file
pmeta->handle = strtol(strtok(tmpbuf, "\n"), NULL, 10);
pmeta->uid = strtol(strtok(NULL, "\n"), NULL, 10);
pmeta->uid = strtol(strtok(NULL, "\n"), NULL, 10);
pmeta->mode = strtol(strtok(NULL, "\n"), NULL, 8);
pmeta->valid = strtol(strtok(NULL, "\n"), NULL, 10);
close(fd);
*/
//my modifications some what artificial!
pmeta->handle = 0x5a305;
pmeta->uid = 0;
pmeta->uid = 0;
pmeta->mode = 0x41ff;
pmeta->valid = 0x77;
return 0;
}
main( void )
{
server();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -