0% found this document useful (0 votes)
21 views10 pages

TCP & UDP File Transfer

Uploaded by

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

TCP & UDP File Transfer

Uploaded by

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

23.

Design TCP client and server application to transfer


file

Server Code
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <errno.h>

#define SERV_PORT 9877

#define MAXLINE 4096

void err_sys(const char *x) {


perror(x);

exit(1);

int main() {

int listenfd, connfd;

struct sockaddr_in servaddr, cliaddr;

socklen_t clilen;

char buf[MAXLINE];

ssize_t n;
listenfd = socket(AF_INET, SOCK_STREAM, 0);

if (listenfd < 0) err_sys("socket error");

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(SERV_PORT);

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

err_sys("bind error");

if (listen(listenfd, 10) < 0) err_sys("listen error");

clilen = sizeof(cliaddr);

connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);

if (connfd < 0) err_sys("accept error");

FILE *fp = fopen("received_file", "wb");

if (fp == NULL) err_sys("fopen error");

while ((n = read(connfd, buf, MAXLINE)) > 0) {

if (fwrite(buf, sizeof(char), n, fp) != n) err_sys("fwrite error");

if (n < 0) err_sys("read error");

fclose(fp);

close(connfd);
close(listenfd);

return 0;

Client Code
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <errno.h>

#define SERV_PORT 9877

#define MAXLINE 4096

void err_sys(const char *x) {

perror(x);

exit(1);

int main(int argc, char **argv) {

int sockfd;

struct sockaddr_in servaddr;

char buf[MAXLINE];
ssize_t n;

if (argc != 3) {

fprintf(stderr, "usage: %s <IPaddress> <filename>\n", argv[0]);

exit(1);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0) err_sys("socket error");

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(SERV_PORT);

if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_sys("inet_pton error");

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

err_sys("connect error");

FILE *fp = fopen(argv[2], "rb");

if (fp == NULL) err_sys("fopen error");

while ((n = fread(buf, sizeof(char), MAXLINE, fp)) > 0) {

if (write(sockfd, buf, n) != n) err_sys("write error");

if (ferror(fp)) err_sys("fread error");

fclose(fp);

close(sockfd);
return 0;

Output:
Server Terminal :

Client terminal :

Once again server terminal:


27. Design UDP Client server to transfer a file

Server Code
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>
#include <arpa/inet.h>

#include <netinet/in.h>

#define SERV_PORT 9877

#define MAXLINE 4096

void err_sys(const char *x) {

perror(x);

exit(1);

int main() {

int sockfd;

struct sockaddr_in servaddr, cliaddr;

char buf[MAXLINE];

socklen_t clilen;

ssize_t n;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if (sockfd < 0) err_sys("socket error");

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(SERV_PORT);

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

err_sys("bind error");

FILE *fp = fopen("received_file", "wb");

if (fp == NULL) err_sys("fopen error");

clilen = sizeof(cliaddr);

printf("Server is running and waiting for data...\n");

while ((n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *) &cliaddr, &clilen)) >
0) {
printf("Received %zd bytes\n", n);

if (fwrite(buf, sizeof(char), n, fp) != n) {

err_sys("fwrite error");

fflush(fp); // Ensure data is written to the file immediately

if (n < 0) err_sys("recvfrom error");

fclose(fp);
close(sockfd);

return 0;

Client Code
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#define SERV_PORT 9877

#define MAXLINE 4096

void err_sys(const char *x) {

perror(x);

exit(1);
}

int main(int argc, char **argv) {

int sockfd;

struct sockaddr_in servaddr;

char buf[MAXLINE];

ssize_t n;

if (argc != 3) {

fprintf(stderr, "usage: %s <IPaddress> <filename>\n", argv[0]);


exit(1);

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if (sockfd < 0) err_sys("socket error");

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(SERV_PORT);

if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) err_sys("inet_pton error");

FILE *fp = fopen(argv[2], "rb");

if (fp == NULL) err_sys("fopen error");

printf("Client is sending file: %s to server: %s\n", argv[2], argv[1]);

while ((n = fread(buf, sizeof(char), MAXLINE, fp)) > 0) {

printf("Read %zd bytes from file\n", n);


if (sendto(sockfd, buf, n, 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) != n) {

err_sys("sendto error");

printf("Sent %zd bytes to server\n", n);

if (ferror(fp)) err_sys("fread error");

fclose(fp);

close(sockfd);
return 0;

Output:
Server terminal :

Client terminal :

Open server terminal again :

How to execute ???

Look at above results and run.

You might also like