//FTP IS NOTHING BUT DEALING OF FILES ON INTERNET...THROUGH SOCKET...SOCKET IS LIKE A PIPE WHICH WE HAD STUDIED BEFORE IN SINGLE SYSTEM..
//NOW SOCKET WHOSE 1 END WILL ATTACH TO ANOTHER SOCKET ON INTERNET & ANOTHER END WITH SYSTEM OF CLIENT OR SERVER...
//BASICALLY THERE ARE TWO SOCKETS WHICH ARE COMMUNICATING EACH OTHER ...2 DIFFRENT SOCKETS OF 2 DIFFRENT SYSTEMS ,,,ARE DEALING WITH EACH OTHER...VIA NET...
//SERVER SOCKET IS HELPING TO CREATE || WE CAN SAY CREATING A EXACT COPY OF CLIENT SOCKET...ACCEPT CALL IS RETURNING US fd of CLIENT SOCKET ...THROUGH WHICH CLIENT SOCKET ..WHICH WILL ALSO RETURN FD OF CLIENT SOCKET ...SO BOTH CLIENT & SERVER ARE COMMUNICATING TO EACH OTHER BASICALLY WITH THE HELP OF CLIENT FD..
//IN SERVER WE ARE WRITTING IN CLIENT FD....& IN CLIENT WE ARE READING FROM SAME CLIENT FD....//
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<linux/in.h>
#include<fcntl.h>//FOR O_RDONLY MODE ...OR OPEN CALL OF SYSTEM
struct sockaddr_in ser_add,client_add;//declare the VARIABLES OF sockaddr_in structure defined in sys/socket.h
int main()
{
char *usr,*page,*buff;//take 3 POINTERS & MALLOC THEM ..WITH WOULD BE USED FURTHER
usr=malloc(30);
printf("size of usr=%d\n",sizeof(usr));
page=malloc(30);
buff=malloc(243);
int wfd,rfd,sfd,ret,fd;
socklen_t len;
ser_add.sin_family=AF_INET;//PUT ADDRESS FAMILY ..PORT NO..TO ENTER INTO OTHER SYSTEM.,..& IP ADDRESS TO SEARCH SYSTEM INTO NETWORK...INTO THIS VARIABLE...BCZ THESE MANY THINGS ARE DEMANDED AS ELEMENT OF struct sockaddr_in structure in sys/socket.h//////////
ser_add.sin_port=htons(556);
ser_add.sin_addr.s_addr=inet_addr("127.0.0.1");
len=sizeof(struct sockaddr_in);
sfd=socket(AF_INET,SOCK_STREAM,0);//socket system call is used to create a socket & it will return fd of server
printf("sfd=%d\n",sfd);
ret=bind(sfd,(struct sockaddr*)&ser_add,len);//SERVER SOCKET IS ALWAYS BINDED ...WITHOUT BINDING ...IT WILL NOT ALLOW THE CLIENT TO CONNECT.....struct sockaddr ..is padded with struct sockaddr_in...//
if(ret==0)
printf("binded\n");
else
printf("not binded\n");
ret=listen(sfd,5);//listen call will decide ..how many max. no. of clients could wait for socket ...//
write(fd,"ENTER THE COMAND",30);//NOW SERVER WILL WRITE ITS APPLICATION
write(fd,"1)ls 2)gets 3)puts",30);
read(fd,buff,4);//COMMAND FROM CLIENT IS READ,...
if(strncmp(buff,"ls",2)==0)//..IF IT IS ls..// { rfd=open("putsi",O_RDONLY);//then SERVER WILL OPEN ITS FILE IN WHICH ITS ALL DATA OF LS IS FILLED USINg ls>putsi read(rfd,buff,30);//READ THAT DATA write(fd,buff,30);//WRITE INTO CLIENT FD..// } if(strncmp(buff,"getc",4)==0)//if COMMAND IS getc....MEANS CLIENT WILL UPLOAD SOME FILE...// { wfd=open("desti",O_WRONLY|O_CREAT);//OPEN SOME FILE TO SAVE THE DATA WRITTEN BY CLIENT..// printf("wfd=%d\n",wfd); read(fd,buff,243);//READ THAT DATA INTO BUFF WRITTEN BY CLIENT..// write(wfd,buff,243);//WRITE OR SAVE THAT DATA WHICH IS WRITTEN BY CLIENT INTO SOME FILE ...IN SYSTEM OF SERVER..// } return 0; OUT: return -1;
ret=connect(fd,(struct sockaddr*)&client_add,len);//CONNECT THE CLIET BY GIVING ITS FD AS 1ST ARG...
if(ret==0)//IF RET ...THEN CONNECTED
printf("connected\n");
else
printf("not connected\n");
printf("ENTER THE USERNAME\n");//PRINT IT
scanf("%s",usr);//USER NAME IS ENTERED INTO USR BUFFER OF CLIENT
write(fd,usr,strlen(usr));//USERNAE IS SEND TO SERVER THROGH FD OF CLIENT SOCKET...//
read(fd,buff,19);//THOSE WORDS EITHER "ENTER THE PASSWORD" OR "USERNAME NOT FOUND" WILL BE READ INTO BUFF
printf("%s\n",buff);//WORDS "ENTER THE PASSWORD" IS PRINTED WHEN USERNAME IS MATCHED || "USERNAME NOT FOUND" IS PRINTED WEITHER ...USERNAME IS NOT MATCHED TO SERVER'DATABASE USERNAME ...//
scanf("%s",usr);//IF PASSWORD IS MATCHED ...THEN ENTER THE EXACT PASSWORD WHICH U HAD..& WHICH IS IN SERVER DATABASE TO USE THE SERVICE OF SERVER...........//
write(fd,usr,8);//WRITE THAT PASSWORD TO CLIENT FD & SEND IT TO SERVER
read(fd,usr,20);//READ WORDS SEND BY SERVER WEITHER PASSWORD OF CLIENT IS ACCESSED BY SERVER OR NOT MATCHED TO THE PASSWORD OF SERVER....//
printf("%s\n",usr);//PRINT THOSE WORDS
read(fd,usr,30);//APPLICATION PROVIDED BY SERVER IS READ...SERVER WILL ASK U TO ENTER 1 OF THE COMMAND & PROVIDE OUTPUT TO U ACCORDING TO THAT COMMAND...//
printf("%s\n",usr);//print ENTER THE COMMAND
read(fd,usr,30);//MENU OF FACILITY PROVIDED BY SERVER IS GET INTO CLIENT
printf("%s\n",usr);//print the menu of commands
//write(fd,"ls",2);//IF U PRINTED ls command...means U HAD DEMANDES LS DATA
//read(fd,buff,30);//READ DATA CORESSPONDING TO LS COMMAND WHICH SERVER HAD SEND TO U,..//
//printf("%s",buff);//printf the file putsi//PRINT THAT DATA...OR U MAY ALSO SAVE THIS DATA INTO A FILE ...//
write(fd,"getc",4);//IN PLACE LS ...U MAY ALSO ENTER getc COMMAND...MEANS U HAVE TO PUT A FILE TO SERVER..U HAVE TO UPLOAD A FILE TO SERVER....//
rfd=open("set",O_RDONLY);//OPEN THE FILE WHICH U WANT TO UPLOAD ON TO THE SERVER..//
read(rfd,buff,243);//READ THAT FILE INTO A BUFF
write(fd,buff,243);//WRITE DATA OF THAT FILE INTO CLIENT FD...
WHERE THERE IS A WILL THERE IS A WAY....HEY I HAVE DONE REALLY SOMETHING DIFFRENT ...EITHER COMPRESSION & DECOMPRESSION HAD NO WORK ON INTERNET..BUT I WILL DO IT USING FTP POINT TO ANOTHER'S SYSTEM POINTER ..OR ANOTHER 'S SYSTEM MASTERARRAY..
WHERE THERE IS A WILL THERE IS A WAY....HEY I HAVE DONE REALLY SOMETHING DIFFRENT ...EITHER COMPRESSION & DECOMPRESSION HAD NO WORK ON INTERNET..BUT I WILL DO IT USING FTP POINT TO ANOTHER'S SYSTEM POINTER ..OR ANOTHER 'S SYSTEM MASTERARRAY..