📄 server.c
字号:
/***
*****Filename:server.c
*****Author:Will
*****E-Mail:hihicd@hotmail.com
*****Date:2008-05-26
*****Version:Beta 0.0
*****Remark:HNU
***/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#define MY_PORT 9000
int main(int argc ,char **argv)
{
int size,listen_fd,accept_fd;
struct sockaddr_in client_addr;
int n;
FILE *fp;
struct sigaction sa;
memset(&sa,'\0',sizeof(sigaction));
sa.sa_handler=SIG_IGN;
sa.sa_flags=SA_NOCLDSTOP;
sigaction(SIGCHLD,&sa,NULL );
if((listen_fd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("Socket Error:%s\n\a",strerror(errno));
exit(1);
}
bzero(&client_addr,sizeof(struct sockaddr_in));
client_addr.sin_family=AF_INET;
client_addr.sin_port=htons(MY_PORT);
client_addr.sin_addr.s_addr=htonl(INADDR_ANY);
n=1;
setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(int));
if(bind(listen_fd,(struct sockaddr *)&client_addr,sizeof(client_addr))<0)
{
printf("Bind Error:%s\n\a",strerror(errno));
exit(1);
}
listen(listen_fd,5);
printf("Listening to the connect request of client......\n");
while(1)
{
accept_fd=accept(listen_fd,NULL,NULL);
if((accept_fd<0)&&(errno==EINTR))
continue;
else if(accept_fd<0)
{
printf("Accept Error:%s\n\a",strerror(errno));
continue;
}
if((n=fork())==0)
{
char *tmp = NULL;
char *dou=NULL;
char buffer[200*1024] = {0};
char req[3]={0};
char s_buffer[200*1024] = {0}; //recieve file
close(listen_fd);
//recieve request
n=read(accept_fd,req,3);
printf("Begining to recieve request......\n");
dou = strchr (req,'\0');//find '\0' in request
if( dou!=NULL && strcmp("up",req)==0 )
{
n=read(accept_fd,buffer,1024);
printf("filename : %s request : %s\n",buffer,req);
printf("Begining to recieve the file......\n");
tmp = strchr (buffer,'\0');//find '\0' in buffer
if (tmp != NULL) // '\0' exist in buffer
{
fp = fopen (buffer, "w+"); // open or create file
if (fp == NULL)
{
printf("Create File Error! \nfilename : %s request : %s\n",buffer,req);
exit (1);
}
if (strlen(buffer) < (n - 1))// something more than filename in buffer
{
tmp++;//ָjump next to the '\0'
fwrite (tmp, sizeof(char), n - strlen(buffer) - 1, fp);// write content to the file
}
while ((n = read(accept_fd, buffer, 1024)) >= 0) // read once a char
{
if (n > 0)
{
fwrite (buffer, sizeof(char), n, fp);//write to the file
}
else
{
fclose (fp);
break;
}
}
if (n < 0) // erro
{
fclose (fp);
printf("Read data from network error!\n");
exit (1);
}
printf("File transfer successfully!......\n");
}
close(accept_fd);
exit(0);
}
else if(dou!=NULL && strcmp("dn",req)==0)
{
n=read(accept_fd,buffer,1024);
printf("filename : %s request : %s\n",buffer,req);
//try to open the file
if( (fp = fopen(buffer, "r" )) == NULL )
{
fprintf( stderr, "The file was not opened\n" );
exit (1);
}
//sending once a char
while((size = fread(s_buffer,sizeof(char),sizeof(s_buffer), fp))>0)
{
printf(".");
if (send (accept_fd, s_buffer, size, 0) < 0)
{
fprintf (stderr,"Sending data to the server failed.\n");
}
}
printf("Sending data...\n");
close(accept_fd);
exit(0);
}
else
{
printf("Accept request Error!\n");
exit (1);
}
}
else if(n<0)
printf("Fork Error:%s\n\a",strerror(errno));
close(accept_fd);
}
printf("\nReceive finished,press any key to return\n");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -