client.c
[code=c
]/*
* client.c
* 1.0
* fsy
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define port 3333
char ipaddr[15];
int sockfd;
struct sockaddr_in servaddr;
static int links()
{
if ((sockfd=socket(AF_INET,SOCK_STREAM,0))== -1)
{
perror("SOCKET");
exit(0);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(ipaddr);
servaddr.sin_port = htons(port);
if ((connect(sockfd,(struct sockaddr*)(&servaddr),sizeof(servaddr))) == -1)
{
perror("CONNECT");
exit(0);
}
return 1;
}
static void upload_file(char * file_name)
{
int fd;
char cmd = 'U';
int size = strlen(file_name);
char buf[1024];
int count;
struct stat file_stat;
if ((fd = open(file_name,O_RDONLY))==-1)
{
perror("OPEN");
printf("This file not found!\n Please input file name again!\n ");
return ; /* this need debug */
}
printf("%d",fd);
/* this need send upload command */
write(sockfd,&cmd,1);
/* this need send file name */
write(sockfd,(void *)(&size),4);
write(sockfd,(void *)(file_name),size);
/* this is send file size */
if((stat(file_name,&file_stat)) == -1)
{
printf("not");
return ; /* this need debug */
}
write(sockfd,(void *)(file_stat.st_size),4);
/*this need send file comment */
while ((count = read(fd,(void *)(buf),1024))>0)
{ printf("ok!");
write(sockfd,&buf,count);
}
close(fd);
}
static void download_file(char *file_name)
{
int fd;
char cmd = 'D';
int size = strlen(file_name);
char buf[1024];
int file_size=0;
int tmp_size=0;
int count=0;
/* this need send upload command */
write(sockfd,(void *)&cmd,1);
/* this need send file name */
write(sockfd,(void *)(&size),4);
write(sockfd,(void *)(file_name),size);
/* this need creat a file to save the download file */
if ((fd = open(file_name,O_RDONLY|O_CREAT,0777))==-1)
{
perror("OPEN");
printf("This file not found!\n Please input file name again!\n ");
return ; /* this need debug */
}
/* this is resave file size */
read(sockfd,&file_size,4);
/*this need save file comment */
while ((count = read(sockfd,(void *)(buf),1024))>0)
{
write(fd,&buf,count);
tmp_size +=count;
printf("%d",tmp_size);
printf("%d",file_size);
if (tmp_size==file_size)
{
break;
printf("%d",tmp_size);
}
}
close(fd);
}
static void quit()
{
char cmd = 'Q';
write(sockfd,(void *)&cmd,1);
system("clear");
exit(0);
}
static void menu()
{
char command;
int c;
char file_u[30];
char file_d[30];
char tmp;
for(;;)
{
printf("\n----------------------------------1.Upload Files ----------------------------------\n");
printf("\n----------------------------------2.Download File----------------------------------\n");
printf("\n----------------------------------3.Quit systerm ----------------------------------\n");
printf("Please input the Client command:\n");
command = getchar();
switch(command)
{
case '1':
{
printf("Please input the file name of u will upload:\n");
while((c = getchar())!= '\n'&& c != EOF ); /* 没有理解之前以为这里是多与的现在理解了知道这个地方是为了 吸 收输入的command之后输入的换行符的 */
fgets(file_u,30,stdin);
file_u[strlen(file_u)-1] = '\0';
upload_file(file_u);
}
break;
case '2' :
{
printf("Please input the file name of u will download:\n");
while((c = getchar())!= '\n'&& c != EOF ); /* 没有理解之前以为这里是多与的现在理解了知道这个地方是为了 吸收输入的command之后输入的换行符的 */
fgets(file_d,30,stdin); /* if use one name what happen! */
file_u[strlen(file_d)-1] = '\0';
download_file(file_d);
}
break;
case '3' :
quit();
break;
default :
printf("Please input right command!\n");
break;
}
}
}
int main(int argc,char *argv[])
{
if(argc!=2)
{
printf("format error: you must enter ipaddr like this: 192.168.1.101!\n");
exit(0);
}
strcpy(ipaddr,argv[1]);
links();
menu();
close(sockfd);
return 0;
}
[/code]
server.c
/*
* server.c
* 1.0
* fsy
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#define port 3333
int sockfd;
int new_sockfd;
int file_name_size;
char file_name[100];
int fd;
int file_size;
int tmp_size;
char buf[1024];
struct stat file_stat;
int count;
struct sockaddr_in servaddr,clieaddr;
socklen_t sin_size;
static void handle(char cmd)
{
switch (cmd)
{
case 'U' :
{
read(new_sockfd,&file_name_size,4);
read(new_sockfd,(void *)&file_name,file_name_size);
file_name[file_name_size]='\0';
if ((fd =(open(file_name,O_RDWR|O_CREAT,0777)))==-1)
perror("open error:\n");
read(new_sockfd,&file_size,4);
/*this need save file comment */
while ((count = read(new_sockfd,(void *)(buf),1024))>0)
{
write(fd,&buf,count);
tmp_size +=count;
if (tmp_size==file_size)
break;
}
close(fd);
}
break;
case 'D':
{
read(new_sockfd,&file_name_size,4);
read(new_sockfd,(void *)&file_name,file_name_size);
file_name[file_name_size-1]='\0';
if ((fd = open(file_name,O_RDONLY))==-1)
{
perror("OPEN");
printf("This file not found!\n Please input file name again!\n ");
return ; /* this need debug */
}
/* this is send file size */
if((stat(file_name,&file_stat)) == -1)
return ; /* this need debug */
write(new_sockfd,(void *)(file_stat.st_size),sizeof(file_stat.st_size));
/*this need send file comment */
while ((count = read(fd,(void *)(buf),1024))>0)
{
write(new_sockfd,&buf,count);
}
close(fd);
}
break;
}
}
int main (void)
{
char cmd;
char i;
if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket");
exit(-1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
if(bind(sockfd,(struct sockaddr*)(&servaddr),sizeof(servaddr))== -1)
{
perror("bind");
exit(1);
}
if (i=(listen(sockfd,5))==-1)
{
perror("listen");
exit(1);
}
else
printf("listen is %d!\n",i);
for(;;)
{
sin_size=sizeof(struct sockaddr);
printf("%d\n",sin_size);
if(new_sockfd=(accept(sockfd,(struct sockaddr*)(&clieaddr),&sin_size))== -1)
{
perror("accept");
exit(1);
}
else
printf("new_sockfd is %d!\n",new_sockfd);
for(;;)
{
read(new_sockfd,&cmd,1);
printf("%d",new_sockfd);
if(cmd=='Q')
{
close(new_sockfd);
break;
}
else
{
handle(cmd);
}
close(new_sockfd);
}
}
close(sockfd);
return 0;
}
]
我在一台电脑上面测试的这两个程序。
客户端发送完命令之后,服务器接受不到命令,一直在accept那里,返回的新sockfd是0.
操作环境是红帽7