AssgB9 - UDP Socket
AssgB9 - UDP Socket
AssgB9 - UDP Socket
Problem Definition:
Problem Definition: Write a program using UDP Sockets to enable file transfer
(Script, Text, Audio and Video one file each) between two machines
1.1Prerequisite:
a) Socket Header b) Network Programming c) Ports
LearningObjectives:
NewConcepts:
1.3Theory:
1.3.1 Introduction
What is UDP?
UDP is a connectionless and unreliable transport protocol.The two ports serve to identify
the end points within the source and destination machines. User Datagram Protocol is used,
in place of TCP, when a reliable delivery is not required.However, UDP is never used to send
important data such as web-pages, database information, etc. Streaming media such as
video,audio and others use UDP because it offers speed.
Why UDP is faster than TCP?
Definitions for the most basic of socket structures with the BSD
Basic data types associated with structures within the BSD socket API <sys/types.h>
Socket API<sys/types.h>
Definitions for the socketaddr_in{} and other base data structures
<sys/un.h>
Definitions and data type declarations for SOCK_UNIX streams
UDP: UDP consists of a connectionless protocol with no guarantee of delivery. UDP packets
may arrive out of order, become duplicated and arrive more than once, or even not arrive at
all. Due to the minimal guarantees involved, UDP has considerably less overhead than TCP.
Being connectionless means that there is no concept of a stream or connection between two
hosts, instead, data arrives in datagrams. UDP address space, the space of UDP port
numbers (in ISO terminology, the TSAPs), is completely disjoint from that of TCP ports.
Server: Code may set up a UDP server on port 7654 as follows:
sock = socket(PF_INET,SOCK_DGRAM,0);
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(7654);
bound = bind(sock,(struct sockaddr *)&sa, sizeof(struct sockaddr));
if (bound < 0) fprintf(stderr, "bind(): %s\n",strerror(errno)); listen(sock,3);
bind() binds the socket to an address/port pair. listen() sets the length of the new
connections queue.
while (1)
{
printf ("recv test....\n");
recsize = recvfrom(sock, (void *)hz, 100, 0, (struct sockaddr *)&sa, fromlen);
printf ("recsize: %d\n ",recsize);
if (recsize < 0)
fprintf(stderr, "%s\n", strerror(errno));
sleep(1);
printf("datagram: %s\n",hz);
Client: A simple demo to send an UDP packet containing "Hello World!" to address
127.0.0.1, port 7654 might look like this:
#include #include #include #include #include
#include int main(int argc, char *argv[])
{
int sock; struct sockaddr_in sa;
int bytes_sent, buffer_length;
char buffer[200];
sprintf(buffer, "Hello World!");
buffer_length = strlen(buffer) + 1;
sock = socket(PF_INET, SOCK_DGRAM, 0);
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(0x7F000001);
sa.sin_port = htons(7654); bytes_sent = sendto(sock, buffer, buffer_length, 0, &sa,
sizeof(struct sockaddr_in) );
if(bytes_sent < 0) printf("Error sending packet: %s\n", strerror(errno) );
return 0;
}
In this code, buffer provides a pointer to the data to send, and buffer_length specifies the
size of the buffer contents. Typical UDP client code
● Create UDP socket to contact server (with a given hostname and service port
number)
● Create UDP packet.
● Call send(packet), sending request to the server.
● Possibly call receive(packet) (if we need a reply).
Questions:
CONCLUSION Thus we have successfully implemented the socket programming for TCP