1.客户端
//cilent code
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SIZE 128
void client_list(int socket_fd);
void client_get(int socket_fd,char *bufsend);
void client_put(int socket_fd,char *bufsend);
int main(int argc, const char *argv[])
{
if(argc<3){
printf("Usrmsg:%s <IP> <Port>
",argv[0]);
return -1;
}
//创建链接和通讯套接字
int socket_fd;
socket_fd = socket(AF_INET,SOCK_STREAM, 0);
if(socket_fd == -1){
perror("socket");
return -1;
}
printf("socket ok
");
//填充结构体sockadd_in,传参
int connect_fd;
struct sockaddr_in serveraddr;
bzero(&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
//connect链接
connect_fd = connect(socket_fd, (struct sockaddr *)&serveraddr,sizeof(serveraddr));
if(connect_fd == -1){
perror("connect");
return -1;
}
printf("connect ok
");
//发送接受buf缓存
char bufsend[SIZE];
char bufrecv[SIZE] = {0};
//循环发送并接收发送成功校验
while(1){
printf("***************list**************
");
printf("***********put filename**********
");
printf("***********get filename**********
");
printf("***************quit**************
");
printf("choose msg >>");
fgets(bufsend,SIZE,stdin);
bufsend[strlen(bufsend)-1] = ' ';
switch(bufsend[0]){
case 'l':
client_list(socket_fd);
break;
case 'p':
printf("send put
");
client_put(socket_fd,bufsend+4);
break;
case 'g':
printf("send get
");
client_get(socket_fd,bufsend+4);
break;
case 'q':
printf("quit
");
close(socket_fd);
return 0;
default:
printf("error choose
");
break;
}
}
close(socket_fd);
return 0;
}
void client_list(int socket_fd)
{
int recvbyte,i=1;
char buf[SIZE] = {0};
sprintf(buf,"L");
send(socket_fd, buf, sizeof(buf),0); //发送L给服务器
while(1){
memset(buf,0,SIZE);
recvbyte = recv(socket_fd,buf,sizeof(buf),0);
if(strncmp(buf,"qqq!",4) == 0)
break;
printf("%d.%s
",i++,buf);
}
printf("client list is ok
");
}
void client_get(int socket_fd,char *bufsend)
{
char buf[SIZE];
sprintf(buf,"G %s",bufsend);
send(socket_fd, (void *)buf, sizeof(buf),0);
int fdd,n;
if((fdd = open(buf+2,O_WRONLY | O_CREAT | O_TRUNC,0666))== -1 ){
perror("opendes");
return;
}
int recvbyte;
while(1){
memset(buf,0,SIZE);
recvbyte = recv(socket_fd, buf,sizeof(buf),0);
if(strncmp(buf,"qqq!",4)==0)
break;
write(fdd,buf,recvbyte);
}
printf("client get ok
");
close(fdd);
}
void client_put(int socket_fd,char *bufsend)
{
char buf[SIZE];
sprintf(buf,"P %s",bufsend);
send(socket_fd, (void *)buf, sizeof(buf),0);
int fds,n;
if((fds = open(bufsend,O_RDONLY))== -1 ){
perror("opensrc");
return;
}
while((n = read(fds,buf,64))>0){
send(socket_fd, buf, n,0);
usleep(10000);
}
strcpy(buf,"qqq!");
send(socket_fd, buf,sizeof(buf),0);
printf("client put ok
");
close(fds);
}
2.服务器代码
//server code
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SIZE 128
void server_get(int accept_fd,char *bufrecv);
void server_put(int accept_fd,char *bufrecv);
void server_list(int accept_fd);
int main(int argc, const char *argv[])
{
if(argc<2){
printf("Usrmsg:%s <Port>
",argv[0]);
return -1;
}
//1.创建套接字文件,用于链接
int socket_fd;
socket_fd = socket(AF_INET,SOCK_STREAM,0);
if(socket_fd == -1){
perror("socket");
return -1;
}
printf("socket Created ok
");
//填充结构体sockaddr_in,用于传参
int bind_fd;
struct sockaddr_in serveraddr; //定义填充的结构体
bzero((void *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET; //IPV4协议
serveraddr.sin_port = htons(atoi(argv[1])); //本地端口号转化位网络字节码
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);//本地IP转化位网络字节码,任意IP
//2.绑定socket文件描述符、端口、IP
bind_fd = bind(socket_fd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
if(bind_fd == -1){
perror("bind");
return -1;
}
printf("bind ok
");
//3.监听,将套接字文件属性该主动为被动模式
int listen_fd;
listen_fd = listen(socket_fd,10);
if(listen_fd == -1){
perror("listen");
return -1;
}
printf("listen ok
");
int accept_fd;
while(1){
//4.阻塞函数,等待客户链接请求,不关心链接的是那个客户端,填NULL
accept_fd = accept(socket_fd, NULL, NULL);
if(accept_fd == -1){
perror("accept");
return -1;
}
printf("accept ok
");
//循环接收信息
char bufrecv[SIZE] = {0};
int recvbyte;
while(1){
memset(bufrecv,0,SIZE);
printf("recv wait:
");
recvbyte = recv(accept_fd, (void *)bufrecv, SIZE, 0);
if(recvbyte>0){
switch(bufrecv[0]){
case 'L':
server_list(accept_fd);
break;
case 'P':
printf("recv put
");
server_put(accept_fd,bufrecv+2);
break;
case 'G':
printf("recv get
");
server_get(accept_fd,bufrecv+2);
break;
}
}
else if(recvbyte<0){
printf("recv fail,waiting for next connect.
");
break;
}
else{
printf("connect break, waiting for next connect.
");
close(accept_fd);
break;
}
}
}
close(accept_fd);
close(socket_fd);
return 0;
}
void server_list(int accept_fd)
{
DIR *dirp;
struct dirent *dent;
struct stat temp;
dirp = opendir(".");
char buf[SIZE] = {0};
while((dent = readdir(dirp)) != NULL)
{
if(strncmp(dent->d_name,".",1) == 0)
continue;
stat(dent->d_name,&temp);
if(S_ISDIR(temp.st_mode) !=0)
continue;
memset(buf,0,SIZE);
strcpy(buf,dent->d_name);
send(accept_fd, buf, sizeof(buf),0);
usleep(10000); //防止栈包
}
memset(buf,0,SIZE);
strcpy(buf,"qqq!");
send(accept_fd, buf, sizeof(buf),0);
printf("server list is ok
");
closedir(dirp);
return;
}
void server_get(int accept_fd,char *bufrecv)
{
int fds,n;
char buf[SIZE];
if((fds = open(bufrecv,O_RDONLY))== -1 ){
perror("opensrc");
return;
}
while((n = read(fds,buf,SIZE))>0){
send(accept_fd, buf, n,0);
usleep(10000);
}
strcpy(buf,"qqq!");
send(accept_fd, buf, sizeof(buf),0);
printf("server get ok
");
close(fds);
}
void server_put(int accept_fd,char *bufrecv)
{
int fdd,n;
if((fdd = open(bufrecv,O_WRONLY | O_CREAT | O_TRUNC,0666))== -1 ){
perror("opendes");
return;
}
char buf[SIZE] = {0};
int recvbyte;
while(1){
memset(buf,0,SIZE);
recvbyte = recv(accept_fd, (void *)buf,sizeof(buf) ,0);
if(strncmp(buf,"qqq!",4)==0)
break;
write(fdd,buf,recvbyte);
}
printf("server put ok
");
close(fdd);
}