0% found this document useful (0 votes)
16 views4 pages

CNS 10

This document contains code for a client and server program that transfer files over a network using UDP datagrams. The client program gets a filename from the user, determines the file size, and sends the file in 503 byte chunks to the server. The server program receives the filename and file chunks, writes them to a new file, and then closes the connection. Key functions include sending and receiving data with sendto() and recvfrom(), determining file size with fsize(), and writing the received file chunks to a new file.

Uploaded by

Kajal Shelar
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)
16 views4 pages

CNS 10

This document contains code for a client and server program that transfer files over a network using UDP datagrams. The client program gets a filename from the user, determines the file size, and sends the file in 503 byte chunks to the server. The server program receives the filename and file chunks, writes them to a new file, and then closes the connection. Key functions include sending and receiving data with sendto() and recvfrom(), determining file size with fsize(), and writing the received file chunks to a new file.

Uploaded by

Kajal Shelar
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/ 4

PRATICAL NO:10

Client.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>

#define SERVER "127.0.0.1"


#define BUFLEN 503
#define PORT 8885

void die(char *s)// used to quit


{
perror(s);
exit(1);
}
unsigned long fsize(char* file)
{
    FILE * f = fopen(file, "r");
    fseek(f, 0, SEEK_END);//used to take the function pointer to the
starting of the file or end of the file or to     any position specified by the
user.
    unsigned long len = (unsigned long)ftell(f);// used to find out the
position of file pointer in the file with  respect to starting of the file.
    fclose(f);
    return len;
}

int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];

if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)


{
die("socket");
}

memset((char *) &si_other, 0, sizeof(si_other));


si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);

if (inet_aton(SERVER , &si_other.sin_addr) == 0) //converts the


specified string, in the Internet standard  dot notation, to a network
address, and stores the address in  the structure provided.
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}

char fname[20];
printf("Enter Filename with extension: ");
scanf("%s",&fname);//add.txt
sendto(s, fname, 20 , 0 , (struct sockaddr *) &si_other, slen);
memset(message,0,503);
unsigned long siz = fsize(fname);//fsize(add.txt)=20
printf("%ld",(siz % 503));//20%503=20
char str[10];
sprintf(str, "%d", siz);
sendto(s, str, 20 , 0 , (struct sockaddr *) &si_other, slen);
FILE *f;
f=fopen(fname,"rb");
memset(message,0,503);
fread(message, 503,1,f);
int itr =1;
while(itr*503<siz)
{
//fread(message, 503,1,f);
if (sendto(s, message, 503 , 0 , (struct sockaddr *) &si_other, slen)==-
1)
{
die("sendto()");
}
memset(message,0,503);
fread(message, 503,1,f);
itr++;
}
fread(message, (siz % 503),1,f);
sendto(s, message, (siz % 503) , 0 , (struct sockaddr *) &si_other, slen);
memset(message,0,503);
fclose(f);
close(s);
return 0;
}

Server.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>

#define BUFLEN 503


#define PORT 8885

void die(char *s)


{
perror(s);
exit(1);
}

int main(void)
{
struct sockaddr_in si_me, si_other;

int s, i,j, slen = sizeof(si_other) , recv_len;


char buf[BUFLEN];

if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)


{
die("socket");
}

memset((char *) &si_me, 0, sizeof(si_me));

si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);

if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)


{
die("bind");
}
char fname[20];
FILE *fp;
recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen);
char fna[100];
memset(buf,0,503);
recv_len = recvfrom(s, buf, 20, 0, (struct sockaddr *) &si_other, &slen);
strcpy(fna,buf);
int len= strlen(fna);
printf("%d",len);//20
for(j=len-1;j>=0;j--)
{
if(fna[j]=='.')
{
fna[j-1]='1';
}
}
unsigned long mm = atoi(buf);
fp=fopen(fna,"wb");
int itr=1;
memset(buf,0,503);
while(itr*503<mm)
{
if ((recv_len = recvfrom(s, buf, 503, 0, (struct sockaddr *) &si_other,
&slen)) == -1)
{
die("recvfrom()");
}
fwrite(buf,503, 1, fp);
memset(buf,0,503);
itr++;
}
printf("%d",(mm%503));
recv_len = recvfrom(s, buf, (mm%503), 0, (struct sockaddr *) &si_other,
&slen);
fwrite(buf,(mm%503), 1, fp);
memset(buf,0,503);
fclose(fp);
close(s);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy