#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include<sys/wait.h>
#include <string.h>
#define DEFAULT_PORT 66
#define DEFAULT_IP "10.0.0.2"
int recv_file(int new_fd)
{
int cLen;
unsigned int img_size, name_size, total_size, in_size;
unsigned char * buff_in = NULL;
char name_buf[100];
img_size = 0;
cLen = recv(new_fd, &img_size, sizeof(img_size),0);
if((cLen < 0)||(cLen == 0)) {
printf("recv img_size failure!\n");
return -1;
}
name_size = 0;
cLen = recv(new_fd, &name_size, sizeof(name_size),0);
if((cLen < 0)||(cLen == 0)) {
printf("recv name_size failure!\n");
return -1;
}
total_size = img_size+name_size;
buff_in = (unsigned char *)malloc(total_size);
if(NULL == buff_in){
printf("malloc buff_in failure!\n");
return -1;
}
in_size = 0;
while(in_size < total_size){
cLen = recv(new_fd, &buff_in[in_size],total_size - in_size,0);
if((cLen < 0)||(cLen == 0)) {
printf("recv buff_in failure!\n");
return -1;
}
in_size+=cLen;
}
memset(name_buf, 0, 100);
memcpy(name_buf, buff_in, name_size);
FILE* out = fopen( name_buf, "w+" );
if ( out == NULL ) {
printf("open file %s failed\n", name_buf);
return -1;
}
fwrite(&buff_in[name_size], img_size, 1, out);
fclose(out);
free(buff_in);
printf("get file %s\n", name_buf);
return 0;
}
int send_file(int new_fd, char *input)
{
unsigned char * buff_out = NULL;
unsigned int img_size = 0;
unsigned int name_size = 0;
unsigned int total_size = 0;
FILE* out = fopen(input, "rw+" );
if ( out == NULL ) {
printf("open file %s failed\n", input);
return -1;
}
fseek(out, 0, SEEK_END);
img_size = ftell(out);
fseek(out, 0, SEEK_SET);
name_size = strlen(input);
total_size = img_size + name_size;
buff_out = (unsigned char *)malloc(total_size);
if(NULL == buff_out){
printf("malloc buff_out failure!\n");
goto out2;
}
if (send(new_fd, &img_size, sizeof(img_size), 0) < 0) {
printf("send img_size failure!\n");
goto out1;
}
if (send(new_fd, &name_size, sizeof(name_size), 0) < 0) {
printf("send name_size failure!\n");
goto out1;
}
memcpy(buff_out,input, strlen(input));
fread(&buff_out[strlen(input)], img_size ,1 ,out);
if (send(new_fd, buff_out, total_size, 0) < 0) {
printf("send buff_out failure!\n");
goto out1;
}
free(buff_out);
fclose(out);
return 0;
out1:
free(buff_out);
out2:
fclose(out);
return -1;
}
int run(char *input_file_name) {
int cPort = DEFAULT_PORT;
int cClient = 0;
int cLen = 0;
struct sockaddr_in cli;
cli.sin_family = AF_INET;
cli.sin_port = htons(cPort);
cli.sin_addr.s_addr = inet_addr(DEFAULT_IP);
cClient = socket(AF_INET, SOCK_STREAM, 0);
if(cClient < 0){
printf("socket() failure!\n");
return -1;
}
if(connect(cClient, (struct sockaddr*)&cli, sizeof(cli)) < 0)
{
printf("connect() failure!\n");
return -1;
}
if(send_file(cClient, input_file_name)){
close(cClient);
return -1;
}
if(recv_file(cClient)){
close(cClient);
return -1;
}
close(cClient);
return 0;
}
int main(int argc, char **argv)
{
if(argc < 2)
printf("please input read file\n");
char *input_name = argv[1];
printf("input file name:%s \n", input_name);
run(input_name);
return 0;
}