Week-4 Lab sheet UDP Sockets and File Transfer
Week-4 Lab sheet UDP Sockets and File Transfer
Systemsfessor,
Read, understand, and save the following file as client_udp.c. You can also download this file from Nalanda. While
reading, try to locate and understand the specific differences with the TCP based client program that we did in the
previous lab.
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#define BUFLEN 512 //Max length of buffer
int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
Page 1 of 9
Department of Computer Science & Information
Systemsfessor,
}
while(1)
{
printf("Enter message : ");
gets(message);
puts(buf);
}
close(s);
return 0;
}
Read, understand, and save the following file as server_udp.c. You can also download this file from
Nalanda. While reading, try to locate and understand the specific differences with the TCP based server
program that we did in the previous lab.
/* Simple udp server */
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
Page 2 of 9
Department of Computer Science & Information
Systemsfessor,
void die(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
Page 3 of 9
Department of Computer Science & Information
Systemsfessor,
}
close(s);
return 0;
}
The following diagram showing the sequence of function calls for the client and a server participating in a TCP
and UDP would help you understand the differences between TCP and UDP socket programming better.
Exercise #1
Modify the ECHO server and client programs to a guessing game, where the server will generate a
number (say between 1 to 6 or the name of a famous personality with some hint) and ask the client
to guess it. The user will enter the guessed number (or name) through the terminal. If the guess is
correct the client will win, otherwise it will lose. An appropriate message about the outcome can be
printed at the client side.
b) Designing and implementing simple FTP client and server with broken
download handling capability using TCP sockets.
In the second lab, we performed few experiments with Wireshark to understand functioning of standard
FTP protocol. Here, we will develop our own simple client/server based application to get a file from the
server. Currently, the program given here, simply downloads a predefined file from the server. It can be
extended to include many other functionalities such as directory listing, “get” and “put” commands, as
present in the standard FTP program. However, our program has the broken download capability which
allows the client to complete a file transfer by downloading the remaining portion of a file only, if the
file to be download is already present with the client partially. For example, if the client is already having
initial 100 bytes of a files then next time, instead of downloading the complete file from the server, the
client can request the server to send the file starting from byte number 101.
Read, understand, and save the following file as client_broken_ftp.c. You can also download this file
from Nalanda.
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
Page 4 of 9
Department of Computer Science & Information
Systemsfessor,
int main(void)
{
int sockfd = 0;
int bytesReceived = 0;
char recvBuff[256];
unsigned char buff_offset[10]; // buffer to send the File offset value
unsigned char buff_command[2]; // buffer to send the Complete File (0)
or Partial File Command (1).
int offset; // required to get the user input for
offset in case of partial file command
int command; // required to get the user input for command
memset(recvBuff, '0', sizeof(recvBuff));
struct sockaddr_in serv_addr;
/* Attempt a connection */
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
Page 5 of 9
Department of Computer Science & Information
Systemsfessor,
return 1;
}
if(bytesReceived < 0)
{
printf("\n Read Error \n");
}
return 0;
}
Page 6 of 9
Department of Computer Science & Information
Systemsfessor,
Read, understand, and save the following file as server_broken_ftp.c. You can also download this file from
Nalanda.
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(void)
{
int listenfd = 0;
int connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
int numrv;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5001);
while(1)
{
unsigned char offset_buffer[10] = {'\0'};
unsigned char command_buffer[2] = {'\0'};
int offset;
Page 7 of 9
Department of Computer Science & Information
Systemsfessor,
int command;
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
if(command == 0)
offset = 0;
else
{
printf("Waiting for client to send the offset\n");
while(read(connfd, offset_buffer, 10) == 0);
sscanf(offset_buffer, "%d", &offset);
/*
* There is something tricky going on with read ..
* Either there was error, or we reached end of file.
*/
Page 8 of 9
Department of Computer Science & Information
Systemsfessor,
if (nread < 256)
{
if (feof(fp))
printf("End of file\n");
if (ferror(fp))
printf("Error reading\n");
break;
}
}
close(connfd);
sleep(1);
}
return 0;
}
Exercise #2
So, now as you understand the programming with UDP sockets and the working of our simple broken
FTP application running over TCP socket, modify the above broken FTP client/server programs to make
it run using UDP sockets.
--------------------------------------------------------------Good Luck------------------------------------------------------------------------
Page 9 of 9