0% found this document useful (0 votes)
65 views3 pages

NWLAB2B

The document describes transferring a file from a server to client using TCP sockets in 3 steps: 1) The server creates a socket, binds it to a port, and listens for connections. 2) When the server receives a file name from the client, it reads the file contents and sends it to the client. 3) The client creates a socket, connects to the server, sends the file name, receives the file contents and saves it locally.

Uploaded by

Divyasri K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views3 pages

NWLAB2B

The document describes transferring a file from a server to client using TCP sockets in 3 steps: 1) The server creates a socket, binds it to a port, and listens for connections. 2) When the server receives a file name from the client, it reads the file contents and sends it to the client. 3) The client creates a socket, connects to the server, sends the file name, receives the file contents and saves it locally.

Uploaded by

Divyasri K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise 2b: File Transfer using TCP

Karun Anantharaman - 195001049


AIM:
To transfer a file from server to client using TCP socket programming
ALGORITHM:
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.
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
SOURCE CODE:
Server.c

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
int main(int argc, char **argv)
{
int len;
int sockfd, newfd, n;
struct sockaddr_in servaddr, cliaddr;
char buff[1024];
char str[1000];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
perror("Cannot create socket!\n");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(7228);
if(bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
perror("Bind error!\n");
listen(sockfd,2);
len = sizeof(cliaddr);
newfd = accept(sockfd, (struct sockaddr*)&cliaddr, &len);
n = read(newfd, buff, sizeof(buff));
printf("File name: %s\n", buff);
int fd = open(buff, O_RDONLY);
if(fd < 0) {
strcpy(buff, "Does not exist!\n");
}
else {
n = 0;
bzero(buff, sizeof(buff));
read(fd, buff, 1024);
close(fd);
}
n = write(newfd, buff, sizeof(buff));
int wfd = open("server.txt", O_WRONLY | O_CREAT);
n = write(wfd, buff, sizeof(buff));
close(wfd);
close(newfd);
close(sockfd);
return 0;
}

Client.c

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>
int main(int argc, char **argv)
{
int len;
int sockfd, n;
struct sockaddr_in servaddr, cliaddr;
char str[1000];
char buff[1024];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
perror("Cannot create socket!\n");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
servaddr.sin_port = htons(7228);
connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
printf("Enter the file name: ");
scanf("%s", buff);
n = write(sockfd, buff, sizeof(buff));
close(sockfd);
return 0;
}

OUTPUT:
Server.txt

You might also like