User:Vaibhav.it/sandbox

 SOCKET PROGRAMING File transfer Program //SERVER.C
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 5) include
 * 6) include
 * 7) include
 * 8) include
 * 9) include
 * 10) include
 * 11) include

int main {   int ser_len, ser1_len; int ser_ds, ser1_ds; struct sockaddr_in ser_addr; struct sockaddr_in ser1_addr;

//creating a socket ser_ds=socket(AF_INET,SOCK_STREAM,0); //var in sock addr ser_addr.sin_family=AF_INET; ser_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); ser_addr.sin_port=4563; ser_len=sizeof(ser_addr); //binding a socket bind(ser_ds,(struct sockaddr*)&ser_addr,ser_len); //listen int l1; l1=listen(ser_ds,5); //connecting to client ser1_len=sizeof(ser1_addr); ser1_ds=accept(ser_ds,(struct sockaddr *)&ser1_addr,&ser1_len); //operations FILE *fd; char buffer[50]; memset(buffer,'\0',sizeof(buffer)); fd=fopen("server.txt","r"); printf("Server Opened:"); fread(buffer,sizeof(buffer),sizeof(char),fd); write(ser1_ds,buffer,strlen(buffer)); fclose(fd); }

//CLIENT.C
 * 1) include
 * 2) include
 * 3) include
 * 4) include
 * 5) include
 * 6) include<sys/socket.h>
 * 7) include<arpa/inet.h>
 * 8) include<netinet/in.h>
 * 9) include<fcntl.h>
 * 10) include<sys/stat.h>

int main {	int cli_len; int cli_fd; struct sockaddr_in cli_addr; //socket creating cli_fd=socket(AF_INET,SOCK_STREAM,0); //variables in socaddr

cli_addr.sin_family=AF_INET; cli_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); cli_addr.sin_port=4563; cli_len=sizeof(cli_addr); //connect int result; result=connect(cli_fd,(struct sockaddr *)&cli_addr,cli_len); if(result == -1){ perror("Failed"); exit(1); } //coding.. FILE *fd; char buff[50]; memset(buff,'\0',strlen(buff)); read(cli_fd,buff,50); fd=fopen("clientfile.txt","w"); printf("file opened"); fwrite(buff,sizeof(buff),sizeof(char),fd); fclose(fd); }