You are on page 1of 7

Name: Divyasri K Date: 23/08/2022

Roll no: 205001035 Academic year: 2020-2024

SSN College of Engineering, Kalavakkam


Department of Computer Science and Engineering
V Semester - CSE 'A'
UCS1511 NETWORK LAB
Exercise 2b: File Transfer using TCP

AIM

To transfer a file from server to client using TCP socket programming

ALGORITHM

CLIENT

1. Create a socket using socket() system call.

2. Connect it to the server.

3. Prompt the user to enter the file name.

4. Transfer the file name to the server

5. Receive the contents of the file and save in a new location

6. Close the socket

SERVER

1. Create a socket using socket() system call.

2. Bind the created socket with the port.

3. Listen for the connections.

4. When the server receives file name from the client, read the contents and send the
contents

to client.
Name: Divyasri K Date: 23/08/2022
Roll no: 205001035 Academic year: 2020-2024

CODE>>

CLIENT:

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

#include<stdio.h>

#include<fcntl.h>

#include<errno.h>

#include<string.h>

#include<unistd.h>

#include <arpa/inet.h>

int main()

int len;

int sockfd,n;

struct sockaddr_in servaddr,cliaddr;

char str[1000]; char buff[1024];

sockfd=socket(AF_INET,SOCK_STREAM,0);
Name: Divyasri K Date: 23/08/2022
Roll no: 205001035 Academic year: 2020-2024

if(sockfd == -1)

perror("cannot create socket");

bzero(&servaddr,sizeof(servaddr));

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");

servaddr.sin_port=htons(8081);

connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));

printf("\nCLIENT SIDE: \n");

printf("Enter filename: ");

scanf("%[^\n]s",buff);

n=write(sockfd,buff,sizeof(buff));

n=read(sockfd,buff,sizeof(buff));

printf("\nContents of file from Server: \n\n%s\n",buff);

printf("\nSaving the file as copy in Desktop\n");

int fdw = open("../../../copy.txt", O_CREAT | O_RDWR,0642);

if(fdw != -1)

printf("\nFILE CREATED\n");

else

printf("\nUNSUCCESSFUL\n");
Name: Divyasri K Date: 23/08/2022
Roll no: 205001035 Academic year: 2020-2024

int sz2 = write(fdw, buff, strlen(buff));

if(sz2 < 0)

printf("\nWRITE FAILED\n");

close(fdw);

close(sockfd);

return 0;

SERVER:

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

#include<fcntl.h>

#include<errno.h>

#include<string.h>

#include<unistd.h>

int main()

int len;
Name: Divyasri K Date: 23/08/2022
Roll no: 205001035 Academic year: 2020-2024
int sockfd, newfd,n;

struct sockaddr_in servaddr,cliaddr; char buff[1024];

char str[1000];

sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd == -1)

perror("cannot create socket");

bzero(&servaddr,sizeof(servaddr));

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=INADDR_ANY;

servaddr.sin_port=htons(8081);

if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

perror("Bind error");

listen(sockfd,2);

len=sizeof(cliaddr);

newfd=accept(sockfd,(struct sockaddr*)&cliaddr,&len); //FIle Descriptr

printf("\nSERVER SIDE\n");

n=read(newfd,buff,sizeof(buff));

printf("\nFile name from Client:\t%s\n",buff);

int fd = open(buff, O_RDONLY);

if(fd == -1)

printf("\nFILE NOT FOUND\n");


Name: Divyasri K Date: 23/08/2022
Roll no: 205001035 Academic year: 2020-2024

int sz = read(fd, buff, 128);

printf("\nsending the contents to client...\n");

n=write(newfd,buff,sizeof(buff));

//printf("\nMessage Sent:\t%s\n",buff);

close(sockfd);

close(newfd);

return 0;

OUTPUT>>
Name: Divyasri K Date: 23/08/2022
Roll no: 205001035 Academic year: 2020-2024

Result:
The above code is compiled and run successfully

Learning outcome:
Learnt the process of setting up connecting and connecting one client to the server and
sending messages both ways

You might also like