Description:
Simple file transfer is achieved through the TCP protocol. Requirements:
1. The server is a daemon and the port number is 10000.
2. Before transferring files, the client should first enter the user name and password for authentication. The username and password are stored in the/etc/login.txt file.
The file content is (make a file by yourself in this format ):
CAT/etc/login.txt
Username: cuit_train
Passwd: good_job
3. After the authentication is passed, the client has several options: 1. Upload the file, and the absolute path of the sacnf file is required. 2. Exit
/************** Server. c ***************/
# Include <stdio. h>
# Include <sys/Wait. H>
# Include <sys/types. h>
# Include <fcntl. h>
# Include <unistd. h>
# Include <netinet/in. h>
# Include <sys/time. h>
# Include <sys/Wait. H>
# Include <sys/socket. h>
# Include <ARPA/inet. h>
# Include "name. H"
# Deprecision maxfile 65535
# Define maxdatasalize 1000
# Define backlog 10/* max connect */
# Define port 10000
# Define path "/etc/login.txt"
Char Buf [maxdatasize];
Void name (char * P );
Void daemo ();
Void server ();
Int child_process (INT Connect );
Int check_passwd (char * client_passwd );
Int getpasswd (char * p_file_name, char * server_passwd );
Int main (INT argc, char ** argv)
{
Pid_t PID;
PID = fork ();
// For daemon
If (PID <0)
{
Perror ("fork error ");
Return 1;
}
Else if (pid> 0)
Return 0;
Else // daemo
{
Daemo ();
}
// Server ();
}
Void daemo ()
{
Int I;
Setsid ();/* frist */
Chdir ("/");/* sencond */
Umask (0);/* thrid */
For (I = 0; I <maxfile; I ++)
Close (I );
Server ();/* Start the server program */
}
/* The server */
Void server ()
{
Int listenfd, connectfd;
Struct sockaddr_in server, client;
Socklen_t addrlen;
Pid_t PID;
If (listenfd = socket (af_inet, sock_stream, 0) =-1)
{
Perror ("error socket ");
Exit (1 );
}
/* IP address reuse */
Int opt = 1;
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, (struct sockaddr *) & server, sizeof (server) =-1)
{
Perror ("error bind ");
Exit (1 );
}
If (Listen (listenfd, backlog) =-1)
{
Perror ("error listen ");
Exit (1 );
}
Addrlen = sizeof (client );
While (1)
{
If (connectfd = accept (listenfd, (struct sockaddr *) & client, & addrlen) =-1)
{
Perror ("error accetp ");
Continue;
}
Printf ("You got a connection from Client IP is: % s, port is: % d/N ",
Inet_ntoa (client. sin_addr), ntohs (client. sin_port ));
/* Fork */
PID = fork ();
If (PID <0)
{
Perror ("fork error ");
Continue;
}
Else if (pid> 0)
{
Close (connectfd );
Continue;
}
Else
{
Close (listenfd );
/* Call child_process function */
Child_process (connectfd );
}
}
}
/* When a customer's connection is received, all sub-processes process everything */
/* Child process function */
Int child_process (INT connectfd)
{
File * FP;
Char t_file_name [100];
Char client_passwd [100];
Ssize_t ssize;
Ssize_t rsize;
Int check;
Int I;
/* Check passwd */
While (1)
{
Bzero (client_passwd, sizeof (client_passwd ));
Rsize = Recv (connectfd, client_passwd, sizeof (client_passwd), 0 );
If (strncmp ("exit", client_passwd, 4) = 0)
{
Printf ("client exit, wait the next client call/N ");
Close (connectfd );
Exit (0 );
}
/* Return 0 client passwd is right */
Check = check_passwd (client_passwd );
If (check = 0)
{
Send (connectfd, "right", 6, 0 );
Printf ("client name and passwd is right/N ");
Break;
}
Else
Send (connectfd, "error", 6, 0 );
}
/* Process File Transfer */
While (1)
{
Bzero (t_file_name, sizeof (t_file_name ));
Rsize = Recv (connectfd, t_file_name, sizeof (t_file_name), 0 );
/* Determine whether the customer launches the product */
If (strncmp ("exit", t_file_name, 4) = 0)
{
Printf ("client exit, wait the next client call/N ");
Close (connectfd );
Exit (0 );
}
/* Find transmission file name, example client filename is "/root/a. log ",
The name function transmission FIME name is "A. log "*/
Name (t_file_name );
Fprintf (stdout, "receive file name is % s/n", t_file_name );
If (FP = fopen (t_file_name, "W") = NULL)
{
Perror ("cannot open file ");
Return 1;
}
While (1)
{
Bzero (BUF, sizeof (BUF ));
Rsize = Recv (connectfd, Buf, maxdatasize, 0 );
If (strncmp (BUF, "end", 3) = 0)
{
Printf ("OK! Receive over ");
Break;
}
Fwrite (BUF, 1, rsize, FP );
I ++;
Printf ("fwrite I = % d, rsoze = % d/N", I, rsize );
}
Fclose (FP );
}
Close (connectfd );
}
Int getpasswd (char * p_file_name, char * server_passwd)
{
Int filefd;
Filefd = open (p_file_name, o_rdonly );
If (filefd =-1)
{
Perror ("error open ");
Exit (1 );
}
If (read (filefd, server_passwd, 18)> 0)
{
// Printf ("server_passwd: % s/n", server_passwd );
Return 0;
}
Else
Return-1;
}
/* Return 0 client_passwd is right */
Int check_passwd (char * client_passwd)
{
Char server_passwd [100];
Bzero (server_passwd, sizeof (server_passwd ));
Printf ("The client_passwd is: % s/n", client_passwd );
Getpasswd (path, server_passwd );
Printf ("server_passwd: % s/n", server_passwd );
If (strncmp (server_passwd, client_passwd, strlen (server_passwd) = 0)
Return 0;
Else
Return-1;
}
/* Find transmission file name, example client filename is "/root/a. log ",
The name function transmission FIME name is "A. log "*/
Void name (char * P)
{
Int Len, I;
Len = strlen (P );
For (I = len-1; I> = 0; I --)
{
If (P [I] = '/')
Break;
}
If (I> = 0)
{
Strcpy (p, p + I + 1 );
}
}
/// // Client, C ////////////////////////////////
# Include <stdio. h>
# Include <unistd. h>
# Include <strings. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <netinet/in. h>
# Include <netdb. h>
// # Include "name. H"
# Define port 10000
# Define maxdatasize (1024*1024)
Int check_passwd (INT sockfd );
Int tra_file (INT sockfd );
Int main (INT argc, char * argv [])
{
Int sockfd;
Struct hostent * He;
Struct sockaddr_in server;
Char chose [20];
If (argc! = 2)
{
Printf ("usage % S <IP address>/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)
{
Printf ("socket () Error/N ");
Exit (1 );
}
Bzero (& server, sizeof (server ));
Server. sin_family = af_inet;
Server. sin_port = htons (port );
Server. sin_addr = * (struct in_addr *) He-> h_addr );
If (connect (sockfd, (struct sockaddr *) & server, sizeof (server) =-1)
{
Printf ("connetc () Error/N ");
Exit (1 );
}
/* Verify the password and user name */
Check_passwd (sockfd );
/* File tramsmission */
While (1)
{
Printf ("chose you Want to do/n chose '1' to tramsmission file, chose 'e' to exit/N ");
Scanf ("% s", chose );
If (chose [0] = 'E ')
{
Send (sockfd, "exit", 5, 0 );
Close (sockfd );
Exit (0 );
}
Else/* file tramsmission */
{
Tra_file (sockfd );
}
}
Close (sockfd );
Return 0;
}
/* Exit this function only when the verification is correct */
Int check_passwd (INT sockfd)
{
Char chose [20];
Char passwd [30];
Char client_passwd [100];
Char Buf [maxdatasize];
While (1)
{
Printf ("######## chose you Want to do ############/N ");
Printf ("***** chose '1' to check name and passwd, chose 'e' to exit ***/N ");
Scanf ("% s", chose );
If (chose [0] = 'E ')
{
Send (sockfd, "exit", 5, 0 );
Close (sockfd );
Exit (0 );
}
Else
{
Bzero (client_passwd, sizeof (client_passwd ));
Bzero (passwd, sizeof (passwd ));
Bzero (BUF, sizeof (BUF ));
Printf ("Please input you name/N ");
Scanf ("% s", client_passwd );
Printf ("Please input you passwd/N ");
Scanf ("% s", passwd );
Strcat (client_passwd, passwd );
/* Send name and passwd */
Send (sockfd, client_passwd, strlen (client_passwd), 0 );
Recv (sockfd, Buf, sizeof (BUF), 0 );
If (strncmp (BUF, "right", 5) = 0)
{
Printf ("OK! You pass the check/N ");
Break;
}
Else
Printf ("Name and passwd is error/N ");
}
}
}
Int tra_file (INT sockfd)
{
Char Buf [maxdatasize];
Int ssize, rsize;
Int I = 0;
File * FP;
Char t_file_name [100];
While (1)
{
Printf ("********** OK! You name and passwd is right *******/N ");
Printf ("Please input you want to transmission file/N ");
Scanf ("% s", t_file_name );
Fprintf (stdout, "Send File Name is: % s/n", t_file_name );
Send (sockfd, t_file_name, 100, 0 );
If (FP = fopen (t_file_name, "rb") = NULL)
{
Perror ("cannot open file ");
Return 0;
}
/* Send File */
While (! Feof (FP ))
{
Bzero (BUF, sizeof (BUF ));
Rsize = fread (BUF, 1, maxdatasize, FP );
Ssize = (send (sockfd, Buf, rsize, msg_dontroute ));
I ++;
Printf ("I = % d, rsize = % d, ssize = % d/N", I, rsize, ssize );
}
Fclose (FP );
Printf ("OK! You send over/N ");
Sleep (1 );
Send (sockfd, "end", 4, 0 );
Break;
}
Return 0;
}