0% found this document useful (0 votes)
3 views17 pages

Udp Socket Interface

Uploaded by

Mahammad azad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Udp Socket Interface

Uploaded by

Mahammad azad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UDP Sockets

Berkely Sockets for UDP

Datagram socket
UDP header
The function sequence of Datagram socket programming
UDP lost datagrams and error handling
Brief comparison of 2 types of socket programming

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 1 / 17


UDP Sockets
Overall Function of Interface

Create socket
bind, connect
listen, accept (TCP only)
send and receive data

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 2 / 17


UDP Sockets
Features (or lack) of UDP

no guarantee of packet delivery


datagrams can be lost and arrive out of sequence.
datagrams can be copied many times and
can be sent faster than the receiving node can process them.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 3 / 17


UDP Sockets
datagram format

source port optional


length in octets includes header and data
checksum optional or zero

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 4 / 17


UDP Sockets
datagram format

entire object including IP header used to compute checksum

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 5 / 17


UDP Sockets
Usage

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 6 / 17


UDP Sockets
operation

Create a UDP socket by specifying SOCK DGRAM. Bind with


INADDR ANY and the chosen port.
Create socket
Populate the sockaddr in structure
Bind to the socket
Receive and block using recvfrom()
Received data may need to be re-ordered on reception

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 7 / 17


UDP Sockets
Create a socket

domain is still AF INET, but the protocol family is SOCK DGRAM.


/* create a socket */
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (udpsocket == -1 ) {
fprintf(stderr, "Could not create a socket");
exit(1);
} else {
printf(Socket created.\n");
}

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 8 / 17


UDP Sockets
Bind

We use INADDR ANY so that our socket will bind to any of the local
addresses that are configured, and we use the port number that was
passed as an argument.
/* set up the server address and port */
/* use INADDR_ANY to bind to all local address
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = htonl(INADDR_ANY);
/* use the port passed as argument */
udpServer.sin_port = htons(atoi(argv[1]));
// atoi converts ascii to integer

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 9 / 17


UDP Sockets
Differences from TCP

In UDP server, we have not used listen(), accept() and read().


The recvfrom() is used as a blocking function, much like accept().
The UDP server needs to store the client’s address in a sockaddr in
structure.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 10 / 17


Sending and Receiving data
recvfrom()

The recvfrom() receives client’s message puts it in a buffer, and


also the client address info.
the recvfrom() call blocks until the message arrives.
The server will store msgs locally if it needs to be resent.
The server doesn’t automatically acknowledge receipt of the client’s
msg.
recvfrom(updSocket, buf, MAXBUF, 0, (struct sockaddr *)&udpClient, &addrlen)

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 11 / 17


UDP Sockets
operation

struct sockadd in must be filled with the server’s address and its
well-known port number.
A datagram socket is needed
The client’s sockaddr in structure is set up with the requried
information.
A specific port number for the client is not needed. The operating
system will assign one.
The client will use the sendto() function to send request to the server.
If the value we get back from the call to sendto() tells the request
was sent, the client gets ready to receive the server’s response using
recvfrom() function.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 12 / 17


Sending and Receiving
recvfrom()

/* create a datagram socket */


udpsocket = socket(AF_INET, SOCK_DGRAM, 0);
/* clinet address */
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = 0;
returnStatus = bind(udpSocket, (struct sockaddr *)&
sizeof(udpCl
/* server address */
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr(argv[1]);
udpServer.sin_port = htons(atoi(argv[2]));
returnStatus = sendto(udpSocket, buf, strlen(buf)+1);
(struct sockaddr *)&udpServer, sizeof(udpServer));

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 13 / 17


Sending data
sendto

sockfd contains the socket file descriptor of the sender;


buf contains the message to be sent;
dest addr contains the address of the destination;
the sendto() call will block till the message has been sent;
when control returns, sending is complete, but this does not imply
successful reception of the message.

ssize_t sendto(int sockfd, const void *buf, size_t len,


int flags, const struct sockaddr *dest_addr,
socklen_t addrlen);

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 14 / 17


UDP lost datagrams

If a datagram is lost (say it is discarded by some router between the client


and server),
the client will block forever in recvfrom(), because when the server
never receive the rquest.
This problem could happen in the client when the server’s reply is lost
on the way back.
In some cases the loss of the packet, or the acknowledgement, should
logically be ignored.
When the message or the ack is needed, we must place a timeout on
the client’s call to recvfrom().

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 15 / 17


Error Handling

It is important to check for all possible types of error.


Check the return value after every single system call.
You can use some built-in support features. (such as fprintf,
perror).
Not only will you save time and effort by checking your error codes,
but you’ll also give your application a solid foundation for security.
Some of the primary exploits used by crackers are something like
buffer overflows and unexpected payloads.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 16 / 17


Comparison, UDP vs TCP

The key difference between a UDP communication and a TCP


communication is that neither the server nor the client has any guarantee
that they will receive anything at all.
The client receive a response from the server provided no errors
occurred.
If the client didn’t receive a reply within a certain time frame, it
would typically issue the request again.
No confirmation message or other status messages would be sent, and
the server would take no action if the client didn’t receive the
response.

Dr Qahhar Muhammad Qadir Socket Programming May 7, 2022 17 / 17

You might also like