Video file
transfer over
TCP and UDP
sockets
Instructor: Sir Dr. Muhammad Zeeshan
Submitted by
Farukh Ahmed Shaikh (220247)
Arwah Jawad ()
BSCS 7A
Introduction:
Socket Programming:
One of the most important technologies of computer networking is socket programming. Socket
programming enables communication between network software applications using standard
mechanisms, which are built in to network hardware and operating system. Socket technology
existed long before the web and all popular network software applications rely on socket
programming. A socket represents point-to-point communication between exactly two pieces of
software. Multiple sockets are used for communication between more than two network software
applications with client/server or distributed system. Socket based communication done on two
different applications on the network but they can also be used for local communication on the
same computer. Socket communication on the network is bi-directional means application on each
end can act as either sender or a receiver. Conventionally the one that initiates communication is
termed as client and the one that responds to request as server.
Transmission Control Protocol (TCP):
TCP communication is connection oriented, which means that first connection is established
between client and server. The connection is maintained until all the messages are exchanged
between both ends. It breaks the application data in packets that the network can send. It then sends
packets to and accepts from network layer. It is used in situations where we want error free data
transmission, so it has to deal with retransmission of dropped packets and with acknowledgement
that all the packets have reached their destination. In an open system interconnection (OSI)
communication model, TCP deals with transport layer and parts of session layer.
User Data Protocol (UDP):
UDP is an alternative communication protocol to TCP. It is used in situations where reliability,
data integrity and order is not required. It does not cater for the dropped packets and allows for
them to be received in a different order rather than the one in which they were sent for better
performance Therefore UDP is used to send short messages called datagram and it is a
connectionless, unreliable protocol. . In an open system interconnection (OSI) communication
model, UDP deals with session layer and some parts of transport layer.
Assignment Problem:
Our problem was to design a software network application, where a video file can be sent on a
client/server architecture based on TCP and UDP sockets separately using various system calls.
We were required to code in GNU C on Linux operating system.
Description:
There are two .c files for both TCP and UDP socket implementation. One for server and the other
for client. The steps followed for implementing the above problem for both TCP and UDP are
described below.
For TCP Socket:
tcpserver.c:
Main steps of code is below
Creates socket successfully
Bind the socket
Listen to client
Accepts the connection
Get the message 0f connection
Reply back to client
Open the audio file
Receive fr0m client
Write to opened file
Repeat step from 5 to 7
Close the file
Close socket
tcpclient.c:
Main steps of code is below
Creates socket successfully
Makes the connection with server
Get the message of connection
Receive message from server
Open the audio file
Read fr0m file for sending
If read is not empty then send it
Repeat step from 7 to 8
Close the file
Close socket
For UDP Socket:
udpclient.c:
Main steps of code is below
Creates socket successfully
Send the string
Get the reply back
Open the audio file
Read from file for sending
If read is not empty then send it
Repeat step from 7 to 8
Close the file
Close socket
udpserver.c:
Main steps 0f c0de is below
Creates socket successfully
Bind the socket
Get the message of connection
Reply back to client
Open the audio file
Receive from client
Write to opened file
Repeat step from 5 to 7
Close the file
Close socket
Source Code:
tcpserver.c:
//socket programming in C using TCP
#include<string.h> //string manupuliatin library
#include<stdio.h> //input 0utput library
#include<sys/socket.h> // f0r sckets
#include<arpa/inet.h>
#include<unistd.h> //write
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#define SIZE 512 // same as client side
int main(int argc , char *argv[])
{
int s_descriptor , n_skt , c; //create new s0cket descriptr
struct sockaddr_in server , client;
char *msg; //pass the message
//socket creation
s_descriptor = socket(AF_INET , SOCK_STREAM , 0);
if (s_descriptor == -1)
{
printf("socket can not be created"); //this executes if it was created succesfuly
}
server.sin_family = AF_INET; //represents the family for socket connection
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(1234);
//Bind the s0cket
if( bind(s_descriptor,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done"); //when binds d0ne
//Listening t0 client
listen(s_descriptor , 3);
puts("wait until connection establishes...");
c = sizeof(struct sockaddr_in);
n_skt = accept(s_descriptor, (struct sockaddr *)&client, (socklen_t*)&c); //accept the
c0nnectin
if (n_skt<0) //if it was nt accepted
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Reply to the client
msg = "how are you client \n"; //pass msg t0 cient
write(n_skt , msg , strlen(msg)); //pass t0 the client
char buf[8*1024]; //buffer f0r string inf0rmatin
FILE *fw;
fw = fopen("hamadreceived.mp4","wb"); //0pen file
while(1){ //while it is true
int recieve=recv(n_skt, buf, sizeof (buf),0); //recive infrmatin
printf("recieve: %d",recieve); //print reciecve
if(recieve>0) //if it is greater then zer0
{
fwrite(buf,sizeof (buf), 1, fw); //write t0 the file
memset(buf,0,sizeof (buf));
puts("server running "); //server is running
}
else
{
perror("server failed haha"); //
puts("server stopped ");
break; //else server is break
}
printf("Exit while\n");
fclose(fw); //close the file
close(n_skt); // close the socket
return 0;
}
tcpclient.c:
#include<stdio.h> //it is used for input output
#include<string.h> // it is used for string library
#include<sys/socket.h> //socket library
#include<arpa/inet.h>
#include <unistd.h>
#define SIZE 1024 // or anything else
int main(int argc , char *argv[])
{
int s_descriptor; //socket descriptr
struct sockaddr_in server;
char *message , server_reply[2000];
//Create socket for data transfering
s_descriptor = socket(AF_INET , SOCK_STREAM , 0);
if (s_descriptor == -1)
{
printf("Socket Could not be created");
}
server.sin_addr.s_addr = inet_addr("[Link]"); //server ip address
server.sin_family = AF_INET; //it is famiy 0f server IPV4
server.sin_port = htons(1234); //p0rt number 0f server
//Cnnect to server for data transfer
if (connect(s_descriptor , (struct sockaddr *)&server , sizeof(server)) < 0) //if it is less then
zero then it is not cnnected
{
puts("connect error");
return 1;
}
printf("Connected\n"); //else cnnected
//getting reply of the server
if( recv(s_descriptor, server_reply , 2000 , 0) < 0) //if it is less then zer0
{
puts("received failed");
}
puts("Reply successfully received\n");
puts(server_reply);
char buf1[8*1024]; //create buffer f0r str0ring inf0rmatin
FILE *fr;
size_t len; //it is length variabe
fr = fopen("hamadsend.mp4","rb"); //0pens the fie
while(1){ //while l00p is n0t break
len = fread (buf1, 1, sizeof (buf1), fr); //read fr00m file
if(len==0) //if n0 data left in file f0r reading
{
puts("client stopped ");
break; //then bbreak the loop
}
else
{
//puts("Hahahaha");
send(s_descriptor,buf1,sizeof (buf1),0); //send this infr0matin t0 server
}
}
puts("I am out of file"); //this when i am 0ut 0f file
close(len); //close the file
close(s_descriptor); //close the cnnectin
return 0;
}
For UDP:
udpclient.c:
#include<stdio.h> //used f0r input 0utput library
#include<string.h> //used f0r strings
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#include <fcntl.h> // for open
#include <unistd.h> // for close
#define SERVER "[Link]" //server ip address
#define BUFLEN 512 //maximum ength 0f buffer
#define PORT 12346 //The port 0f server
void die(char *s) //when err0r 0ccurs
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in SiOther;
int skt, i, slen=sizeof(SiOther);
char buf[BUFLEN]; //buffer fr0r st0ring inf0rmatin
char msg[BUFLEN];
if ( (skt=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) //if s0cket created
succesfuy
{
die("socket");
}
memset((char *) &SiOther, 0, sizeof(SiOther));
SiOther.sin_family = AF_INET; //ip famiy
SiOther.sin_port = htons(PORT); //p0rt f the
if (inet_aton(SERVER , &SiOther.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
printf("Enter msg : ");
//gets(msg); //gets msg fr0m the user
fgets(msg, sizeof(msg), stdin);
//send the msg t0 server
if (sendto(skt, msg, strlen(msg) , 0 , (struct sockaddr *) &SiOther, slen)==-1) //if it returns
-1 then it is n0t success
{
puts("Send this ");
die("sendto()");
}
memset(buf,'\0', BUFLEN);
if (recvfrom(skt, buf, BUFLEN, 0, (struct sockaddr *) &SiOther, &slen) == -1) //get repy
back frm user
{
puts("Recieve this");
die("recvfrom()");
}
puts(buf); //print the data
char message1[BUFLEN]; //st0re the inf0rmatin
FILE *fr;
size_t len;
fr = fopen("hamadsend.mp4","rb"); //0pens the file
while(1){ //while loop is n0t break
len = fread (message1, 1, sizeof (message1), fr); //read fr0m file
if(len==0)// if it is equal t0 zer0
{
puts("client stopped \n");
printf("send to %d \n",(int)sendto(skt, message1, sizeof (message1) , 0 , (struct sockaddr *)
&SiOther, slen));//send this msg t0 the server
break; //break the loop
}
else
{
printf("send to %d \n",(int)sendto(skt, message1, sizeof (message1) , 0 , (struct sockaddr *)
&SiOther, slen));//send this msg t0 the server
}
}
puts("\nI am out of file\n");
close(len); //close the fille
close(skt); //close the s0cket
return 0;
}
udpserver.c:
#include<stdio.h> //it is library for input and 0utput
#include<sys/socket.h> //s0cket librar
#include<arpa/inet.h>
#include<string.h> //string library
#include<unistd.h> //
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include<stdlib.h> //exit(0);
#define BUFLEN 512 //maximum length 0f buffer
#define PORT 12346 //p0rt number
void kill(char *s) //when an err0r 0ccurs
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, s_len = sizeof(si_other) , recv_len; //integers f0r data st0rage
char buf[BUFLEN]; //buffer f0r st0ring inf0rmatin
//creates the s0cket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
kill("socket");
}
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET; //ip address family
si_me.sin_port = htons(PORT); //p0rt number
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
//bind s0cket
if( bind(s , (struct sockaddr*)&si_me, sizeof (si_me) ) == -1)
{
kill("bind");
}
printf("Waiting for data..."); //waiting f0r infrmatin
fflush(stdout);
//get the data fr0m client
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &s_len)) == -1)
//if it is equal t0 -1
{
kill("recvfrom()");
}
printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr),
ntohs(si_other.sin_port)); //print the data
printf("Data: %s\n" , buf);
//thr0w back the data t0 client
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, s_len) == -1) //if it is equal t0 -1
{
kill("sendto()");
}
char buf1[BUFLEN]; // create the buffer f0r st0ring inf0rmatin
FILE *fw;
fw = fopen("VideoFromUDPRev.mp4","wb"); //0pens the file f0r writing data
while(1) //while loop n0t breaks
{
recv_len =recvfrom(s, buf1, sizeof (buf1), 0, (struct sockaddr *) &si_other, &s_len); //get the
data fr0m client
printf("recievce %d",recv_len);
if(recv_len>0) //if it is greater then zer0
{
puts("i am here in else if");
fwrite(buf1,sizeof (buf1), 1, fw); //write t0 file
memset(buf1,0,sizeof (buf1));
}
else
{
puts("iam here in out of else ");
perror("server failed haha");
break;// else break the loop
}
}
printf("Exit while\n");
fclose(fw); //close the file
close(s); //close the s0cket
return 0;
}
Output:
For TCP:
Tcpclient:
Tcpserver:
For UDP:
Udpclient:
Udpserver:
Files