TCP SOCKET - FT

Page 1 2 3 4 5 6 7 8 9


FILE TRANSFER APPLICATION USING TCP SOCKET

SOURCE CODE

SERVER

#include “stdio.h “
#include “unistd.h “
#include “sys/types.h “
#include “sys/socket.h “
#include “sys/stat.h “
#include “fcntl.h “
#include “netinet/in.h “

main()
{
char buf[512]={""},buff[512]={""};
int id,b,a,r,i,l,s,f1,x;
socklen_t n;
struct stat a1;
struct sockaddr_in server,client;
id=socket(AF_INET,SOCK_STREAM,0);
if(id < sin_family="AF_INET;" sin_port="htons(8483);" s_addr="INADDR_ANY;" b="bind(id,(struct" l="listen(id,1);" n="sizeof(client);" a="accept(id,(struct" r="read(a,buff,sizeof(buff));"> 0)
{
write(1,"file does not exist",32);
_exit(0);
}
stat(buff,&a1);
if(!S_ISREG(a1.st_mode))
{
write(1,"\nfile is not a regular file",28);
_exit(0);
}
if((f1=open(buff,0))==-1)
{
write(1,"cannot open the file",20);
_exit(0);
}
while((n=read(f1,buf,512)) > 0)
{
if(write(a,buf,n)==-1)
{
write(1,"write error",12);
_exit(0);
}
}
write(1,buf,sizeof(buf));
printf("PORT NUMBER: %u",client.sin_port);
printf("IP ADDRESS: %s",inet_ntoa(client.sin_addr));
write(a,"DATA RECEIVED",14);
write(a,"\n",2);
close(id);
}










CLIENT:
#include “unistd.h “
#include “stdio.h “
#include “sys/socket.h “
#include “sys/types.h “
#include “sys/stat.h “
#include “fcntl.h “
#include “netinet/in.h “
main()
{
char buf[512]={""},buff[512]={""};
int id,b,c,s,a,r,n,l;
struct stat a1;
struct sockaddr_in server,client;
id=socket(AF_INET,SOCK_STREAM,0);
if(id < sin_family="AF_INET;" sin_port="htons(8483);" s_addr="INADDR_ANY;" sin_family="AF_INET;" sin_port="htons(8483);" s_addr="INADDR_ANY;" c="connect(id,(struct" s="send(id,&buff,sizeof(buff),0);">

Output


SERVER:


[NPLab@localhost ~]$ cc fts.c
[NPLab@localhost ~]$ ./a.out
apple
orange
mango
grapes
pineapple
[NPLab@localhost ~]$


CLIENT:


[NPLab@localhost ~]$ cc ftc.c
[NPLab@localhost ~]$ ./a.out
Enter The File To Transfer:fruits.txt
apple
orange
mango
grapes
pineapple
[NPLab@localhost ~]$