/*A NICE IMPLEMENTATION OF MULTIPLE DATA COMPRESSION USING SOCKET*/
//WE KNOW SOCKETS..//
//SOCKETS ARE USED TO COMMUNICATE BETWEEN THE TWO SYSTEMS ON THE NET.
//IN THIS PROJECT ...I HAD DEMANDED FOR A COMRESSED FILE FOR ORIGINAL FILE BY CLIENT.IN THE SITUATION ..I HAD LESSMEMORY IN DEVICE ...& NO PACKAGE TO TRANSFER THE FILE ..& DUE TO LESS MEMORY ...I GET NEED TO COMPRESS THE FILE ..SO I MADE THE PROJECT IN WHICH CLIENT IS ALWAYS DEMANDING FOR ..DIFFRENT-DIFFRENT COMPRESSIONS OF DIFFRENT FILES...& SERVER IS CONNECTED THROUGH THE SOCKET..& SERVER AGAINCONNECTED TO THE PERTICULAR PROCESS IN WHICH UNIVERSAL CODE OF COMPRESSION IS WRITTEN..ALL THE COMPRESSIONS OF DIFFRRENT FILE ARE DONE ACCORDING TO SIZE & NO. OF DISTINCT CHARACTERS OF THAT FILE ..//& COMPRESSED FILE IS AGAIN ..GIVEN BACK TO CLIENT BY PROCESS THROUGH SERVER//
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<linux/in.h>
#include<fcntl.h>
struct sockaddr_in client_add,client_add1;//VARIABLES OF STRUCTURE ARE DECLARED TO USE IT FURTHER..//
int main()
{
//char opr='+';
socklen_t len;
char *buff;
buff=malloc(1000);
char *buf;
buf=malloc(1000);
int i=0,wfd,ret=0,count=1,rfd,ffd,fd,num1=5,num2=3;
len=sizeof(struct sockaddr_in);
client_add.sin_family=AF_INET;
client_add.sin_port=htons(4444);//IP ADDRESS IS NECCESARY TO FIND OTHER SYSTEM ON NET..& PORT NO. TO ENTER INTO THTV SYSTEM....& OFFCOURSE FAMILY OF ADDRESS IS ALSO GIVEN INTO ELEMENTS OF STRUCTURE sockaddr_in DEFINED IN SYS/SOCKET.H
fd=socket(AF_INET,SOCK_STREAM,0);//SOCKET CALL IS MADE TO CREATE THE SOCKET
printf("fd=%d\n",fd);
ret=connect(fd,(struct sockaddr*)&client_add,len);//CONNECT CALL IS TO CONNECT THE SOCKET TO SERVER ...THERE WILL BE TWO DIFFRENT SOCKETS CREATED AT TWO SYSTEMS .../BOTH THE SOCKETS WILL COMMUNICATE EACH OTHER THROUGH NET..BUT HOW?
//ACTUALLY SERVER SOCKE THAT IS CREATED ON SERVER SIDE WILL CREATE COPY OF CLIENT SOCKET ...USING ACCEPT CALL CORESSPONDING TO CONNECT CALL...FD OF THAT COPY OF CLIENT SOCKET WILL BE TREATED SAME AS THAT COMING OF CLIENT SOCKET..MEANS IN CLIENT SOCKET SOME FILE TO BE COMPRESSED IS WRITTEN & READ ON THE SERVER SIDE FROM THE COPY OF CLIENT SOCKET...//
if(ret==0)
printf("connected\n");
else
printf("not connected\n");
ffd=socket(AF_INET,SOCK_STREAM,0);//ONE ANOTHER SOCKET IS CREATED ..TO GET THE RESULT OF ..MEANS TO GET THE COMPRESSED FILE ..//
printf("ffd=%d\n",ffd);
ret=connect(ffd,(struct sockaddr*)&client_add,len);//AT SAME TIME ..WE CAN CONNECT TWO OR MORE SOCKETS TO THE SAME SERVER...THROUGH SAME PROGRAM ..OR DIFFRENT APPLICATION...BUT THE ONE SERVER WILL ALWAYS REMAIN A SINGLE UNIQUE PORT TO COMMUNICATE TO ANY OF THE CLIENT...MEANS FOR A PERTICULAR SERVER ...NONE OF THE PORT NO. & IP ADDRESS | ADDRESSS FAMILY IS GOING TO BE CHANGED..//
rfd=open("source4",O_RDONLY);//OPEN THE FILE IN O_RDONLY MODE WHICH IS TO BE COMPRESSED//
while(count)
{
//printf("set set set c1\n");
//sleep(1)
count=read(rfd,(buff+i),1);//READ THE FILE INTO A POINTER BUFFER..TO SEND IT TO SERVER 1 BY 1 CHARACTER ..ACCORDING TO COUNT ...BCZ WE DONT NOW ALREADY...WHAT COULD BE SIZE OF FILE WHICH IS TO BE COMPRESSED
//ALTHOUGH IT IS NOT VERY GOOD ..TO READ THE FILE IN A POINTER ..BCZ FILE COULD IN SOME KB..BUT THIS TIME IT IS OK..//U MAY READ IT AS IT IS..& SEND TO SERVER CHARACTER BY CHARACTER..//
if(count==0)
break;
//printf("buff=%c\tcount=%d\n",*(buff+i),count);
i++;
}
count=write(fd,buff,strlen(buff));//NOW ACCORDING TO LENGTH OF BUFFER ...U MAY WRITE THE FILE TO SERVER TO GET ..THE OUPUT BACK..//
printf("buff=%s count=%d\n",buff,count);
//close(fd);
count=1;i=0;
while(count)
{
printf("give give give compressed\n");
count=read(ffd,(buf+i),1);//THROUGH OTHER SOCKET & INTO ANOTHER BUFFER ..RESULTING COMPRESSED FILE IS REad
//KEY POINT:DO NOT USE ^Z TO KILL THE PROCESS ..BCZ IS NOT ACTUALLY FINISHES USING ^Z..PORT REMAIN BUSY//ONLY USE ^C TO KILL THE PROCESS ..IT WILL TEND FREE UR PORT...//
int ret=0,i=0,count=1,ffd,ppfd,fd,pfd,sock_fd,true=1;
socklen_t len;
len=sizeof(struct sockaddr_in);
ser_add.sin_family=AF_INET;
ser_add.sin_port=htons(4444);//CLIENT WILL SEARCH THE SERVER ON NET ..//USING ITS PUBLIC IP.& ENTER INTO SYSTEM USING PORT NO..
ser_add.sin_addr.s_addr=inet_addr("127.0.0.1");
sock_fd=socket(AF_INET,SOCK_STREAM,0);//SERVER SOCKET IS CREATED USING SOCKET SYSTEM CALL//
if(sock_fd>0)
printf("sock_fd is obtained\n");
printf("sock_fd=%d\n",sock_fd);
ret=setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int));//USUALLY USED TO UNLINK THE PORT
if(ret==0)
printf("setsockopt is succesful..it will unlink\n");
else
printf("setsockopt is unsuccesful ..not be able to unlink\n");
ret=bind(sock_fd,(struct sockaddr*)&ser_add,len);//USED TOBIND THE SOCKET..//MEANS TO PROVIDE THE NAME TO SERVER SOCKET..//BIND CALL DO NOT MERE PROVIDE NAME TO SOCKET OF SERVER BUT ALSO PROVIDE ADDRESS TO THE SYSTEM..A SPECIAL ADDRESS WHERE CLIENT AFTER ENTERY TO THE PORT ..FINDS THE SERVER SOCKET...THROUGH THAT PERTICULAR PORT..//
//IT BINDS THE ADDRESS OF SOCKET TO ITS PORT NO..THAT IS WHY IT IS CALLED BIND SYSTEM CALL//
if(ret==0)
printf("bind is succesful...client system will be connected\n");
else
printf("bind is not succesful\n");
ret=listen(sock_fd,5);//USED TO MAKE THAT HOW MUCH MAX. NO. OF CLIENTS COULD WAIT?//
if(ret==0)
printf("listen succesful\n");
else
printf("listen is not succesful\n");
fd=accept(sock_fd,(struct sockaddr*)&client_addr,&len);//TO ACCEPT THE CONNECT CALL OF CLIENT..//HERE ARE 4 ACCEPT CALLS ...1ST CLIENT WILL BE OPERATED ...SO 2 FDS OF CLIENT 1 BY 1 ..WILL BE CREATED../
//THEN PROCESS WILL BE OPERATRED...THEN ONE 1 BY 1 ..2 FDS OF PROCESS SOCKET ARE ALSO CREATED..4TH CALL IS DOWN...WRITTEN..
printf("fd=_____________%d\n",fd);
ffd=accept(sock_fd,(struct sockaddr*)&client_addr1,&len);//ALL THE 4 SOCKETS OF 2 OF CLIENT & 2 OF PROCESS ARE CONNECTED AT THE SAME TIME ..//
count=read(fd,(buff+i),1);//FILE TO BE COMPRRESSED THAT WAS WRITTEN BY CLIENT INTO 1 OF THE SOCKET ..IS READ BY COPY CREATED OF THAT SOCKET ..I.E. 1ST OF ALL CREATED..//
write(pfd,buff,strlen(buff));//WRITE THAT FILE TO BE COMPRESSED INTO PROCESS INTO COPY OF 1 OF THE PROCESS SOCKET
printf("buff=%s\n",buff);
count=1,i=0;
ppfd=accept(sock_fd,(struct sockaddr*)&pro_add1,&len);//I WILL WAIT FOR THE ACCEPT OF 2ND CONNECT CALL OF PROCESS IN WHICH RESULT I.E. COMPRESSED FILE IS GOING TO COME AFTER THE COMPRESSION ..//
printf("ppfd=_____________%d\n",ppfd);
while(count ==1) { printf("GIVE ME GIVE ME COMPRESSED\n"); count=read(ppfd,(bufff+i),1);//BY COPY OF 2ND SOCKET OF PROCESS..COMPRESSED FILE IS READ,,// if(count==0 || *(bufff+i) == '\0') break; printf("bufffff for desti=%c_________________count=%d\n",*(bufff+i),count); i++; } close(ppfd); write(ffd,bufff,strlen(bufff));//WRITE THAT COMPRESSED FILE INTO COPY OF 2ND SOCKET OF CLIENT CREATED BY SERVER SOCKET..//
wfd=open("ssource",O_WRONLY | O_CREAT);//1ST OF ALL SAVED THAT FILE TO BE COMPRESSED HERE .IN THE SYSTEM OF SERVER,,TEMPORARY UNTILL ,./COMPRESSION IS NOT DONE ||PERMANENTLY IF SYSTEM HAVE MUCH MEMORY TO HANDLE LAKHS OF CLIENTS..//
printf("buf=%s\n",buf);
write(wfd,buf,strlen(buf));//WRITE INTO FILE TO BE SAVED..//
ret=create_masterarray(buff);//SEND THIS CHARACTER TO CREATE_MASTER ARRAY TO FIND THE DISTINCT NO. OF CHARACTERS INTO MASTERARRAY ..BCZ...WE WILL FIND DISTINCT NO. OF CHARACTERS IN THE FILE & THEN WE WILL GIVE UNIQUE ID TO THAT UNIQUE NO. OF THOSE CHARACTERS ...& THAT UNIQUE ID WILL BE CALLED INDEX NO..//SO CONTINUE..FORWARD//
}
printf("ndc__=%d\n",ret);
j=codelen(ret);//TO FIND CODELENGTH OF FILE ..USING NO. OF UNIQUE CHARACTERS...THIS FUNCTION IS CALLED//CODELENGTH MEANS IN HOW MANY BITS THOSE UNIQUE NO. OF CHARACTERS COULD BE REPRESENTED ..//
//FOR E.G.WE HAVE 16 || <16 UNIQUE NO. OF CHARACTERS IN FILE ..IT CAN BE REPESENTED IN 4 BITS 0000...
//IF WE HAVE 32 || >16 |<32 UNIQUE CHARACTERS...THEN CAN BE REPRESENTED IN 5 BITS ..SO ON..MOVE//
printf("codelen=%d\n",j);
switch(j)//ACCORDING TO CODELENGTH TECHNIQUE OR ALGO FOR COMPRESSION WILL BE DECIDED ..//LESS NO. OF UNIQUE CHARACTRERS MORE WILL BE COMPRESSION//
count=read(fd,&sor,1);//COMPRESSED FILE IS READ FROM FILE
printf("count=%d\n",count);
//write(bfd,&sor,1);
write(ppfd,&sor,1);//SEND THE COMPRESSED FILE THROUGH SOCKET..//HERE IS SOME PROBLEM....SEEE ANY CODE THAT IS COMPRESSED HERE ....THAT FILE IS OPENED & WRITTEN INTO THE SOCKET..& WE KNOW SOCKET CAN HAVE ANY MUCH SIZE OF FILE WRITTEN INTO IT..//BUT WITH THE SAME CODE IF I READ FILE UPTO 4 BIT CODE ..//IT WORKS NICE ...IT WRITE BACK INTO FILE ..FOR ANY MUCH SIZE OF FILE ...IT WILL WRITE BACK UPTO 4 BIT CODE...BUT WHEN TRY TO WRITE 5 || 6 ||7 BIT CODE FILE BACK...IT WILL NOT WRITE..& AFTER READING SMALL NO. OF CHARACTERS EITHER 2 ..EITHER 4 EITHER 7..IT ITSELF COME OUT OF LOOP..& NOT WRITING INTO THE SOCKET ..ALTHOGH CONDITION WAS NOT MADE FALSE //
//IF I REMOVE THIS COMMAND OF WRITING INTO SOCKET ..IT WILL READ ALL THE DATA PERFECTING ..& WRITE INTO ANY FILE WHOLETHE DATA OF COMPRESSED FILE WITHOUT ANY PROBLED..BUT WHEN I APPLY WRITE CALL TO WRITE INTO ANY OF THE SOCKET ..I WILL NOT ABLE TO WRITE ..INSTEAD OF THAT WITHOUT HAVING COUNT=0..WITHOUT MAKING CONDITION FALSE OF LOOP ..IT LET OUT OF LOOP// //ALTHOUGH LOOP IS NOT APPLIED ...I DIRECTLY TRIED TO WRITE DATA INTO SOCKET ...THEN IT IS ALSO NOT ABLE TO WRITE DATA INTO SOCKET ...WHEN I DO 5 || 6 || 7 BIT COMPRESSION// //WRITING FROM DESTI (COMPRESSED)FILE & WRITTING INTO SOCKET CODE IS SAME FOR ALL OF THE COMPRESSION ALGOS///1 2 3 4 5 6 7...THEN WHY? WHY IT IS NOT ABLE TO WRITE INTO SOCKET ..WHILE 5 6 7 BIT COMPRESSION IS DONE //FORGET THERE IS NO FLOW IN COMPRESSION..BCZ..I HAD ALREADY TRIED DECOMPRESSION OF ALL THE COMPRESSED CODES..SO COMPRESSION CODES ARE PERFECT FOR ALL THE COMPRESSION ALGORITHEMS... //??????????????????????????????????????????????????????????????????????PLEASE ANS ME . IF ANY ONE CAN?????ITS CHALLAGING QUES??????????????????????????????????????????????????????????????????????????????????????????????????????????????/??? if(count == 0) break; } return 0; }