Sie sind auf Seite 1von 11

SERVER.

#include "tcpsocket.h"
#include "packet.h"
#include "time.h"
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include "enc_dec.h"

#define PORT 3001


#define KEY "accent12"

int fd_server;
int fd_client;
struct packet s_pkt,r_pkt;
int BREAK;
char path;
char *time_str;
FILE *fp;

void Send_file(char s_file[],int init_file);


void Send_bmp(char s_file[],int init_file);
int random_int(int,int);

void *recv_p(void* null)


{
while(1)
{
recv(fd_client,&r_pkt,sizeof(struct packet),0);
// printf("recv *************\n");

if(r_pkt.s_type == _CHELLO)
{
printf("\n hello pkt recv\n");
s_pkt.s_type = _SHDONE;

get_time_string(TIME_12,s_pkt.s_status.SS_Time);
printf("Time : %s",s_pkt.s_status.SS_Time);
s_pkt.s_id = random_int(50,300);
send(fd_client,&s_pkt,sizeof(struct packet),0);
}
if(r_pkt.s_type == _SENDF)
{
// printf("\n SEND pkt recv\n");
if(r_pkt.s_status.status == _REQ)
{
// printf("\n REQ pkt recv\n");
// path = 'A';
path = r_pkt.f_type;
printf("path : %c\n",path);
BREAK = 1;
}
if(r_pkt.s_status.status == _BREQ)
{
// printf("\n _BREQ pkt recv\n");
path = r_pkt.f_type;
BREAK = 3;
}
}
if(r_pkt.s_type == _END)
exit(0);
if(r_pkt.s_type == _BREAK)
{
// printf("\n BREAK pkt recv\n");
BREAK = 2;
}
}
}
void *send_p(void *null)
{
char ch[2] = "01";
char s_file[10];
int i = 0;

while(1)
{
// printf("BREAK %d ",BREAK);
if(BREAK==1)
{
i = 0;
if(path == 'A')
{
strcpy(s_file,"A/");
s_file[2]='\0';
strcat(s_file,ch);
s_file[4]='\0';
strcat(s_file,".txt");
Send_file(s_file,i);
}
else if(path == 'I')
{
strcpy(s_file,"I/");
s_file[2]='\0';
strcat(s_file,ch);
s_file[4]='\0';
strcat(s_file,".bmp");
printf("\n select file is %s\n",s_file);
Send_bmp(s_file,i);

}
}
if(BREAK == 3)
{
if(path == 'A')
{
strcpy(s_file,"A/");
s_file[2]='\0';
strcat(s_file,r_pkt.s_status.state);
i=((s_file[2]-48)*10 )+(s_file[3]-48);
// printf("\nAfter break pkt send : %s :: %s
%d\n",s_file,r_pkt.s_status.state,i);
Send_file(s_file,i);
// BREAK = 1;
}
else if(path == 'I')
{
strcpy(s_file,"I/");
s_file[2]='\0';
strcat(s_file,r_pkt.s_status.state);
i=((s_file[2]-48)*10 )+(s_file[3]-48);
Send_bmp(s_file,i);

}
}
}
}

void Send_bmp(char s_file[],int init_file)


{
char key[8];
char file[10]="enc.txt";
int i,size,no;
unsigned int val;
char data;
unsigned char data1;

while(init_file<10)
{
if(BREAK == 2)
break;

s_pkt.s_type = _BMP;
strcpy(s_pkt.fname,s_file);

printf("file name : %s\n",s_file);


if((fp = fopen(s_file,"r")) == NULL )
{
printf("\n file not found error : %s\n",s_file);
exit(1);
}

fscanf(fp,"%c",&data1);
fscanf(fp,"%c",&data);
fread(&val,4,1,fp);
s_pkt.s_file = val;
send(fd_client,&s_pkt,sizeof(struct packet),0);
rewind(fp);
i = 0;
while(1)
{
// strcpy(s_pkt.data,data);
no=fread(&data1,1,1,fp);
if(no==0)break;
send(fd_client,&data1,1,0);
}
fclose(fp);
// printf("\n data : \n%s \n",data);

// send(fd_c,data,strlen(data),0);

printf("\n");
system("clear");
printf("\n\n\n\n\n\n\n");
printf("\n\t\t\t\t Send File\t\t: %s \n ",s_file);
printf("\n\t\t\t\t Session ID\t\t: %d \n",s_pkt.s_id);
printf("\n\t\t\t\t Session Start Time\t: %s
\n",&r_pkt.s_status.SS_Time);
// printf("\t DATA :\n %s \n",s_pkt.data);
init_file++;
if(s_file[3]=='9')
{
s_file[2]+=1;
s_file[3]='0';
s_file[4]='\0';
strcat(s_file,".bmp");
}
else
s_file[3]+=1;
sleep(2);
}
if(init_file == 10)
{
s_pkt.s_type = _SFINISH;
// time_str = get_time_string(TIME_12);
get_time_string(TIME_12,s_pkt.s_status.SE_Time);
// strcpy(s_pkt.s_status.SE_Time,time_str);
send(fd_client,&s_pkt,sizeof(struct packet),0);
BREAK = 0;
// printf("Send finish\n");
}

void Send_file(char s_file[],int init_file)


{
char key[8];
char file[10]="enc.txt";
int i;
char data[BUFFER];
struct stat f_status;

while(init_file<10)
{
if(BREAK == 2)
break;

s_pkt.s_type = _DATA;
strcpy(s_pkt.fname,s_file);

strcpy(key,KEY);
enc_dec('e',key,s_file,file);

stat(file, &f_status);
s_pkt.s_file = f_status.st_size;

fp = fopen(file,"r");
fread(data,1,s_pkt.s_file,fp);
fclose(fp);
strcpy(s_pkt.data,data);
// printf("\n data : \n%s \n",data);

// send(fd_c,data,strlen(data),0);

send(fd_client,&s_pkt,sizeof(struct packet),0);
printf("\n");
system("clear");
printf("\n\n\n\n\n\n\n");
printf("\n\t\t\t\t Send File\t\t: %s \n ",s_file);
printf("\n\t\t\t\t Session ID\t\t: %d \n",s_pkt.s_id);
printf("\n\t\t\t\t Session Start Time\t: %s
\n",&r_pkt.s_status.SS_Time);
// printf("\t DATA :\n %s \n",s_pkt.data);
init_file++;
if(s_file[3]=='9')
{
s_file[2]+=1;
s_file[3]='0';
s_file[4]='\0';
strcat(s_file,".txt");
}
else
s_file[3]+=1;
sleep(2);
}
if(init_file == 10)
{
s_pkt.s_type = _SFINISH;
// time_str = get_time_string(TIME_12);
get_time_string(TIME_12,s_pkt.s_status.SE_Time);
// strcpy(s_pkt.s_status.SE_Time,time_str);
send(fd_client,&s_pkt,sizeof(struct packet),0);
BREAK = 0;
// printf("Send finish\n");
}

int main()
{
int i = 0;
int status;
pthread_t thread[2];
pthread_attr_t attr;

BREAK=0;
time_str = (char *)malloc(12);
//Socket Creation

fd_server = tcp_server_creat(PORT,1);

//Accept Client

fd_client = tcp_accept(fd_server,PORT);

/* Initialize and set thread detached attribute */


pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

if (pthread_create(&thread[0], &attr, recv_p, NULL))


{ printf("ERROR; return code from pthread_create() is 0\n");
exit(-1); }
if (pthread_create(&thread[1], &attr, send_p, NULL))
{ printf("ERROR; return code from pthread_create() is 1\n");
exit(-1); }

/* Free attribute and wait for the other threads */


pthread_attr_destroy(&attr);

for(i=0;i<2;i++)
{
if (pthread_join(thread[i], (void **)&status))
{ printf("ERROR return code from pthread_join() is
\n"); exit(-1); }
}

// while(1);
close(fd_server);
close(fd_client);
return 0;

int random_int(low,high)
{
int rand();
int r,i,j;
i = RAND_MAX / (high-low+1);
i *= (high-low+1);
while ((j = rand()) >=i){continue;}
r = (j % (high-low+1)) + low;
if ((r < low) || (r > high))
printf("Error: ra ndom integer %d out of range
[%d,%d]\n",
r,low,high);
return(r);
}
Client.C

#include "tcpsocket.h"
#include "packet.h"
#include "time.h"
#include <pthread.h>

#define PORT 3001


#define KEY "accent12"

struct packet s_pkt,r_pkt;

int fd_client;
char BREAK;
char *time_str;
char end;
char key[8];
char file_type;
#include "enc_dec.h"

void *break_p(void *null)


{
while(1)
{
if(BREAK == 'a')
{
scanf(" %c",&BREAK);
if(BREAK == 'q' || BREAK == 'Q')
{
s_pkt.s_type = _END;
send(fd_client,&s_pkt,sizeof(struct packet),0);
exit(1);
}
}
}
}

void *send_p(void *null)


{
FILE *fp;

int i,j;
char r_file[10],data1;
char file[10]= "enc.txt";
while(1)
{
printf("Press Enter to start Session\n");
getchar();
s_pkt.s_type = _CHELLO;
s_pkt.c_id = 2;
send(fd_client,&s_pkt,sizeof(struct packet),0);
printf("\n hello pkt send\n");
BREAK = 'a';
while(1)
{
if(BREAK !='b'&& BREAK !='B' && BREAK != 'c' && BREAK
!= 'C' )
{
recv(fd_client,&r_pkt,sizeof(struct packet),0);
// printf("\n BREAK : %c\n",BREAK);
if(r_pkt.s_type == _SHDONE)
{
printf("\nSession Start in :
%s\n",&r_pkt.s_status.SS_Time);

strcpy(s_pkt.s_status.SS_Time,r_pkt.s_status.SS_Time);
while(1)
{
printf("Select the file type : \n
(A) - Text file \n (I) Image file\n ");
file_type = getchar();

if(file_type == 'i' || file_type ==


'I')
{
file_type = 'I';
printf("\nImage file type\n");
break;
}
if(file_type == 'a' || file_type ==
'A')
{
file_type = 'A';
break;
}

s_pkt.s_type = _SENDF;
s_pkt.s_status.status = _REQ;
s_pkt.f_type = file_type;
send(fd_client,&s_pkt,sizeof(struct
packet),0);
}

if(r_pkt.s_type == _SFINISH)
{
printf("\nSession Start Time :
%s\nSession End Time :
%s\n",&r_pkt.s_status.SS_Time,&r_pkt.s_status.SE_Time);
printf("Session Finished\n");
printf("\nIf you want to ['q'-quit (or)
's' - start new session] : \n");
scanf(" %c",&BREAK);
if(BREAK == 's')
break;
}

if(r_pkt.s_type == _DATA)
{
strcpy(r_file,&r_pkt.fname[2]);
// printf("\nrec file name : %s ::
%s\n",r_file,r_pkt.fname);
// printf("\nDATA : \n%s\n",r_pkt.data);
fp = fopen(file,"w");
fwrite(r_pkt.data,r_pkt.s_file,1,fp);
fclose(fp);
strcpy(key,KEY);
enc_dec('d',key,file,r_file);

system("clear");
printf("\n\n\n\n\n\n\n");
printf("\n\t\t\t\t Recv File\t\t: %s \n
",r_file);
printf("\n\t\t\t\t Session ID\t\t: %d
\n",r_pkt.s_id);
printf("\n\t\t\t\t Session Start Time\t:
%s\nIf you want to ['q'-quit (or) 'b' - break session]
",&r_pkt.s_status.SS_Time);
// printf("\n\t DATA : \n%s\n",r_pkt.data);
// printf("\n\t\t If you want to ['q'-quit
(or) 'b' - break session] ");

}
if(r_pkt.s_type == _BMP)
{
strcpy(r_file,&r_pkt.fname[2]);
fp = fopen(r_file,"w");
// fwrite(r_pkt.data,r_pkt.s_file,1,fp);

for(j=0;j<r_pkt.s_file;j++)
{
recv(fd_client,&data1,1,0);
fwrite(&data1,1,1,fp);
}
fclose(fp);
system("clear");
printf("\n\n\n\n\n\n\n");
printf("\n\t\t\t\t Recv File\t\t: %s \n
",r_file);
printf("\n\t\t\t\t Session ID\t\t: %d
\n",r_pkt.s_id);
printf("\n\t\t\t\t Session Start Time\t:
%s",&r_pkt.s_status.SS_Time);
// printf("\n\t DATA : \n%s\n",r_pkt.data);
printf("\n\t\t If you want to ['q'-quit
(or) 'b' - break session] ");
}
}

else if(BREAK == 'c' || BREAK == 'C')


{
s_pkt.s_type = _SENDF;
s_pkt.s_status.status = _BREQ;
strcpy(s_pkt.s_status.state,r_file);
BREAK = 'a';
send(fd_client,&s_pkt,sizeof(struct packet),0);
}
else if(BREAK == 'b' || BREAK == 'B')
{
s_pkt.s_type = _BREAK;
printf("\n break pkt send\n");
send(fd_client,&s_pkt,sizeof(struct packet),0);
printf("\nIf you want to ['q'- quit] / ['c'- ]
: ");
scanf(" %c",&BREAK);
}
if(BREAK == 'q' || BREAK == 'Q')
{
s_pkt.s_type = _END;
send(fd_client,&s_pkt,sizeof(struct packet),0);
exit(1);
}
}
}
}

int main()
{
FILE *fp;
char s_file[10];
int i = 0,status;
pthread_t thread[2];
pthread_attr_t attr;

end = 'e';
BREAK = 's';
//Create Client socket

fd_client = tcp_client_creat(PORT,"192.168.10.206");

/* Initialize and set thread detached attribute */


pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

if (pthread_create(&thread[0], &attr, send_p, NULL))


{ printf("ERROR; return code from pthread_create() is 0\n");
exit(-1); }
if (pthread_create(&thread[1], &attr, break_p, NULL))
{ printf("ERROR; return code from pthread_create() is 1\n");
exit(-1); }

/* Free attribute and wait for the other threads */


pthread_attr_destroy(&attr);

if (pthread_join(thread[0], (void **)&status))


{ printf("ERROR return code from pthread_join() is \n");
exit(-1); }

if (pthread_join(thread[1], (void **)&status))


{ printf("ERROR return code from pthread_join() is \n");
exit(-1); }
/* for(i=0;i<2;i++)
{
if (pthread_join(thread[i], (void **)&status))
{ printf("ERROR return code from pthread_join() is
\n"); exit(-1); }
}
*/
// while(1);
close(fd_client);
return 0;

Das könnte Ihnen auch gefallen