Professional Documents
Culture Documents
transfer over
TCP and UDP
sockets
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.
Source Code:
tcpserver.c:
//socket programming in C using TCP
//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
}
//Listening t0 client
listen(s_descriptor , 3);
puts("Connection accepted");
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 <unistd.h>
#define SIZE 1024 // or anything else
}
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:
int main(void)
{
struct sockaddr_in SiOther;
int skt, i, slen=sizeof(SiOther);
char buf[BUFLEN]; //buffer fr0r st0ring inf0rmatin
char msg[BUFLEN];
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);
if (recvfrom(skt, buf, BUFLEN, 0, (struct sockaddr *) &SiOther, &slen) == -1) //get repy
back frm user
{
puts("Recieve this");
die("recvfrom()");
}
int main(void)
{
struct sockaddr_in si_me, si_other;
//bind s0cket
if( bind(s , (struct sockaddr*)&si_me, sizeof (si_me) ) == -1)
{
kill("bind");
}
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