#include<sys/socket.h> //for socket
#include<stdio.h>
#include<unistd.h> // for close
#include<stdlib.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<pthread.h> // for thread
#include<string.h>
#include<sys/stat.h> // for fstat
#include<sys/mman.h> //for mmap
#define PORT 5123
#define MAX_LEN 1500
#define EACH_DATA 1040
#define SERVER "127.0.0.1"
char sendbuff[MAX_LEN];
char recvbuff[10];
int main(int argc, char ** argv)
{
int sockfd;
struct sockaddr_in servaddr;
int filesize = 0;
int sendtime = 0;
struct stat statbuf;
FILE * fp = NULL;
char * src= NULL;
char * cur_src = NULL;
char serverip[40]={'\0'};
memcpy(serverip, SERVER, sizeof(SERVER));
if(argc != 2)
{
fprintf(stderr, "usage client <filename>.");
return 1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
inet_pton(AF_INET, serverip, &servaddr.sin_addr);
//printf("address =%s\n ",argv[1]);
if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))==-1)
{
printf("client connect faliure.");
return 1;
}
//first send filename
char filename[100]={'\0'};
int filenamelen = strlen(argv[1]);
memcpy(filename, argv[1], filenamelen);
char *p = (char *) &filenamelen;
//memcpy(sendbuff+1, p, 4);
memcpy(sendbuff, p, 4);
memcpy(sendbuff+4,filename,filenamelen);
printf("len =%d filename=%s\n",filenamelen, filename);
send(sockfd, sendbuff, filenamelen+4, 0);
//printf("here\n");
//start to send file
fp = fopen(filename, "rb+");
if(fstat(fileno(fp), &statbuf) < 0)
{
fprintf(stderr,"fstate failure.");
return 1;
}
filesize = statbuf.st_size;
sendtime = filesize / EACH_DATA;
if(filesize % EACH_DATA !=0)
sendtime +=1;
if((src = (char *)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED)
{
fprintf(stderr, "mmap failure");
fclose(fp);
return 1;
}
fclose(fp);
cur_src = src;
int t_len =0;
while(sendtime > 0)
{
if(sendtime > 1)
{
t_len = EACH_DATA;
memcpy(sendbuff, cur_src, t_len);
cur_src+=t_len;
}
else if(sendtime ==1)
{
t_len = filesize % EACH_DATA;
if(t_len ==0)
t_len = EACH_DATA;
memcpy(sendbuff, cur_src, t_len);
sendbuff[t_len] ='\r';
sendbuff[t_len +1] ='\n';
cur_src += t_len;
t_len += 2;
}
sendtime --;
send(sockfd, sendbuff, t_len, 0);
}
munmap(src, statbuf.st_size);
printf("send over\n");
}
#include<stdio.h>
#include<unistd.h> // for close
#include<stdlib.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<pthread.h> // for thread
#include<string.h>
#include<sys/stat.h> // for fstat
#include<sys/mman.h> //for mmap
#define PORT 5123
#define MAX_LEN 1500
#define EACH_DATA 1040
#define SERVER "127.0.0.1"
char sendbuff[MAX_LEN];
char recvbuff[10];
int main(int argc, char ** argv)
{
int sockfd;
struct sockaddr_in servaddr;
int filesize = 0;
int sendtime = 0;
struct stat statbuf;
FILE * fp = NULL;
char * src= NULL;
char * cur_src = NULL;
char serverip[40]={'\0'};
memcpy(serverip, SERVER, sizeof(SERVER));
if(argc != 2)
{
fprintf(stderr, "usage client <filename>.");
return 1;
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
inet_pton(AF_INET, serverip, &servaddr.sin_addr);
//printf("address =%s\n ",argv[1]);
if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))==-1)
{
printf("client connect faliure.");
return 1;
}
//first send filename
char filename[100]={'\0'};
int filenamelen = strlen(argv[1]);
memcpy(filename, argv[1], filenamelen);
char *p = (char *) &filenamelen;
//memcpy(sendbuff+1, p, 4);
memcpy(sendbuff, p, 4);
memcpy(sendbuff+4,filename,filenamelen);
printf("len =%d filename=%s\n",filenamelen, filename);
send(sockfd, sendbuff, filenamelen+4, 0);
//printf("here\n");
//start to send file
fp = fopen(filename, "rb+");
if(fstat(fileno(fp), &statbuf) < 0)
{
fprintf(stderr,"fstate failure.");
return 1;
}
filesize = statbuf.st_size;
sendtime = filesize / EACH_DATA;
if(filesize % EACH_DATA !=0)
sendtime +=1;
if((src = (char *)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fileno(fp), 0)) == MAP_FAILED)
{
fprintf(stderr, "mmap failure");
fclose(fp);
return 1;
}
fclose(fp);
cur_src = src;
int t_len =0;
while(sendtime > 0)
{
if(sendtime > 1)
{
t_len = EACH_DATA;
memcpy(sendbuff, cur_src, t_len);
cur_src+=t_len;
}
else if(sendtime ==1)
{
t_len = filesize % EACH_DATA;
if(t_len ==0)
t_len = EACH_DATA;
memcpy(sendbuff, cur_src, t_len);
sendbuff[t_len] ='\r';
sendbuff[t_len +1] ='\n';
cur_src += t_len;
t_len += 2;
}
sendtime --;
send(sockfd, sendbuff, t_len, 0);
}
munmap(src, statbuf.st_size);
printf("send over\n");
}