输入命令格式:
下载:./SCP_Server 192.168.0.2:/home/pub/file1 ./
上传:./SCP_Client ./file2 192.168.0.2:/home/pub
/* SCP_Server.c */
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#define N 1024
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr, cliaddr;
char buf[N], buffer[N]; int lfd, connfd;
char str[INET_ADDRSTRLEN];
int n; socklen_t cliaddr_len;
lfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(5555);
bind(lfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(lfd, 20);
printf("Accepting connections ...\n");
while(1)
{
cliaddr_len = sizeof(cliaddr);
connfd = accept(lfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
n = read(connfd,buf, N);
///client request get file from server//
if(buf[0] == 'g')
{
n = read(connfd, buf, N);
printf("Client request get file from server.\n");
printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
printf("The file path is:%s\n", buf);
int fd;
if((fd = open(buf, O_RDONLY)) < 0)
{
perror("open failed");
write(connfd, "no such file!", 14);
close(connfd);
continue;
}
n = lseek(fd, 0, SEEK_END);
sprintf(buf, "%d", n);
write(connfd, buf, strlen(buf)+1);
lseek(fd, 0, SEEK_SET);
read(connfd, buffer, 1);
while((n = read(fd, buf, N)))
{
write(connfd, buf, n);
}
close(fd);
printf("Sending success.\n\n");
}
///client request put file to server
else if(buf[0] == 'p')
{
buf[0] = '\0';
n = read(connfd, buf, N);
printf("Client request put file to server.\n");
printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
if(strcmp(buf, "no such file!") == 0)
{
printf("wrong filen.\n");
close(connfd);
continue;
}
printf("The file path is:%s\n", buf);
int fd;
if((fd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0)
{
perror("open failed");
exit(2);
}
n = read(connfd, buf, N);
write(connfd, "", 1);
int cnt = atoi(buf);
lseek(fd, cnt - 1, SEEK_SET);
write(fd, "", 1);
lseek(fd, 0, SEEK_SET);
while((n = read(connfd, buf, N)))
{
write(fd, buf, n);
}
close(fd);
printf("Getting success.\n\n");
}
error
else printf("buf error!\n");
close(connfd);
}
close(lfd);
return 0;
}
/********* SCP_Client *********/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
#define MAX 256
int main(int argc, char *argv[])
{
if(argc != 3)
{
printf("error command.\n");
return 0;
}
struct sockaddr_in servaddr;
int sockfd; char buf[N], filename[MAX], buffer[N];
char *ph, *pc, *ip, *p1, *p2;
int n, i;
memset(buf, 0, N);
memset(buffer, 0, N);
memset(filename, 0, MAX);
p1 = argv[1], p2 = argv[2];
while(1)
{
while(*p1 != ':' && *p1 != '\0') p1++;/* get */
if(*p1 == ':')
{
buf[0] = 'g';
ip = ph = argv[1];
pc = argv[2];
break;
}
while(*p2 != ':' && *p2 != '\0') p2++;/* put */
if(*p2 == ':')
{
buf[0] = 'p';
ip = ph = argv[2];
pc = argv[1];
break;
}
printf("Wrong command.\n");
return 1;
}
while(*ph != '\0')
{
if(*ph++ == ':')
{
*(ph - 1) = '\0';
break;
}
}
/get file from server
if(buf[0] == 'g')
{
printf("get file from server.\n");
for(i = strlen(ph) - 1 ; ; i--)
{
if(*(ph + i) == '/')
break;
}
if(*(pc + strlen(pc) - 1) == '/')
*(pc + strlen(pc) - 1) = '\0';
sprintf(filename, "%s%s", pc, (ph + i));
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
if((inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr)) <= 0)
{
perror("wrong ip");
exit(3);
}
servaddr.sin_port = htons(5555);
if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("can't connect");
exit(3);
}
if(write(sockfd, buf, 1) < 0)
{
perror("write g error");
exit(1);
}
if(write(sockfd, ph, strlen(ph) + 1) < 0)
{
perror("write filename error");
exit(1);
}
n = read(sockfd, buf, N);
if(strcmp(buf, "no such file!") == 0)
{
printf("No such file, please check the filename.\n");
exit(1);
}
write(sockfd, "", 1);
int cnt = atoi(buf);
int fd;
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644 );
lseek(fd, cnt - 1, SEEK_SET);
write(fd, "", 1);
lseek(fd, 0, SEEK_SET);
while((n = read(sockfd, buf, N)))
{
write(fd, buf, n);
}
close(fd);
printf("Getting success.\n");
}
put file to server
else if(buf[0] == 'p')
{
printf("put file to server.\n");
for(i = strlen(pc) - 1 ; ; i--)
{
if(*(pc + i) == '/')
break;
}
if(*(ph + strlen(ph) - 1) == '/')
*(ph + strlen(ph) - 1) = '\0';
sprintf(filename, "%s%s", ph, (pc + i));
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
if((inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr)) <= 0)
{
perror("wrong ip");
exit(3);
}
servaddr.sin_port = htons(5555);
if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("can't connect");
exit(3);
}
if(write(sockfd, buf, 1) < 0)
{
perror("write p error");
exit(1);
}
int fd;
if((fd = open(pc, O_RDONLY)) < 0)
{
perror("open file failed");
write(sockfd, "no such file!", 14);
exit(1);
}
if(write(sockfd, filename, strlen(filename) + 1) < 0)
{
perror("write filename, error");
exit(1);
}
n = lseek(fd, 0, SEEK_END);
sprintf(buf, "%d", n);
write(sockfd, buf, strlen(buf)+1);
lseek(fd, 0, SEEK_SET);
read(sockfd, buffer, 1);
while((n = read(fd, buf, N)))
{
write(sockfd, buf, n);
}
close(fd);
printf("Sending success.\n");
}
/// error
else printf("It's wrong.\n");
close(sockfd);
return 0;
}
// myscp_server.c
#include <stdio.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#define N 1024
int main(int argc, char *argv[])
{
struct sockaddr_in servaddr, cliaddr;
char buf[N]; int lfd, connfd;
char str[INET_ADDRSTRLEN];
int n; socklen_t cliaddr_len;
lfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(5555);
bind(lfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
listen(lfd, 20);
printf("Accepting connections ...\n");
while(1)
{
cliaddr_len = sizeof(cliaddr);
connfd = accept(lfd, (struct sockaddr *)&cliaddr, &cliaddr_len);
n = read(connfd,buf, N);
///client request get file from server//
if(buf[0] == 'g')
{
n = read(connfd, buf, N);
printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
ntohs(cliaddr.sin_port));
printf("The file path is:%s\n\n", buf);
int fd;
fd = open(buf, O_RDONLY);
while((n = read(fd, buf, N)))
{
write(connfd, buf, n);
}
close(fd);
}
///client request put file to server
else if(buf[0] == 'p')
{
n = read(connfd, buf, N);
printf("received from %s at PORT %d\n", inet_ntop( AF_INET, &cliaddr.sin_addr, str, sizeof(str)),
ntohs(cliaddr.sin_port));
printf("The file path is:%s\n\n", buf);
int fd;
fd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0644);
while((n = read(connfd, buf, N)))
{
write(fd, buf, n);
}
close(fd);
}
error
else printf("buf error!\n");
close(connfd);
}
return 0;
}
/ myscp.c
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
#define MAX 256
int main(int argc, char *argv[])
{
if(argc != 3)
{
printf("error command.\n");
return 0;
}
struct sockaddr_in servaddr;
int sockfd; char buf[N], filename[MAX];
char *ph, *pc, *ip, *p1, *p2;
int n, i;
p1 = argv[1], p2 = argv[2];
while(1)
{
while(*p1 != ':' && *p1 != '\0') p1++;/* get */
if(*p1 == ':')
{
buf[0] = 'g';
ip = ph = argv[1];
pc = argv[2];
break;
}
while(*p2 != ':' && *p2 != '\0') p2++;/* put */
if(*p2 == ':')
{
buf[0] = 'p';
ip = ph = argv[2];
pc = argv[1];
break;
}
printf("Wrong command.\n");
return 1;
}
while(*ph != '\0')
{
if(*ph++ == ':')
{
*(ph - 1) = '\0';
break;
}
}
/get file from server
if(buf[0] == 'g')
{
printf("get file from server.\n");
for(i = strlen(ph) - 1 ; ; i--)
{
if(*(ph + i) == '/')
break;
}
if(*(pc + strlen(pc) - 1) == '/')
*(pc + strlen(pc) - 1) = '\0';
sprintf(filename, "%s%s", pc, (ph + i));
printf("ip = %s\nph = %s\npc = %s\nfilename = %s\n", ip, ph, pc, filename);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr);
servaddr.sin_port = htons(5555);
if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
{
perror("wrong ip");
exit(2);
}
if(write(sockfd, buf, 1) < 0)
{
perror("write g error");
exit(1);
}
if(write(sockfd, ph, strlen(ph) + 1) < 0)
{
perror("write filename error");
exit(1);
}
int fd;
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644 );
while((n = read(sockfd, buf, N)))
{
write(fd, buf, n);
}
close(fd);
}
put file to server
else if(buf[0] == 'p')
{
printf("put file to server.\n");
for(i = strlen(pc) - 1 ; ; i--)
{
if(*(pc + i) == '/')
break;
}
if(*(ph + strlen(ph) - 1) == '/')
*(ph + strlen(ph) - 1) = '\0';
sprintf(filename, "%s%s", ph, (pc + i));
printf("ip = %s\nph = %s\npc = %s\nfilename = %s\n", ip, ph, pc, filename);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, ip, &servaddr.sin_addr.s_addr);
servaddr.sin_port = htons(5555);
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if(write(sockfd, buf, 1) < 0)
{
perror("write p error");
exit(1);
}
if(write(sockfd, filename, strlen(filename) + 1) < 0)
{
perror("write filename, error");
exit(1);
}
int fd;
if((fd = open(pc, O_RDONLY)) < 0)
{
perror("open filename failed");
exit(1);
}
while((n = read(fd, buf, N)))
{
write(sockfd, buf, n);
}
close(fd);
}
/// error
else printf("It's wrong.\n");
close(sockfd);
return 0;
}