









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lecture was delivered by Dr. Ram Sai at Jaypee University of Engineering and Technology for Computers and Network Programming course. It includes: Network, Programming, Udp, Server, Client, Echo, Function, Server, Datagram, Sender, Tcp
Typology: Slides
1 / 16
This page cannot be seen from the preview
Don't miss anything!










#include “unp.h” int main(int argc, char **argv) { int sockfd;struck sockaddr_in servaddr,cliaddr; sockfd=Socket(AF_INET,SOCK_DGRAM,0);bzero(&servaddr,sizeof(servaddr));servaddr.sin_fammily=AF_INET;servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(SERV_PORT);bind(sockfd, (SA *) &servaddr,sizeof(servaddr)); dg_echp(sockfd, (SA *) &cliaddr,sizeof(cliaddr)); } sendto UDPclient recvfrom recvfrom sendto UDPserver fgets fputs stdin stdout Simple echo client ‐server using UDP UDP echo server
#include “unp.h” void dg_echo(int sockfd, SA *pcliaddr, socklen_t clilen) { int n;socklen_t len;char mesg[MAXLINE]; for( ; ; ) { len=clilen;n=Recvfrom(sockfd, mseg, MAXLINE, 0, pcliaddr, &len); sendto(sockfd, mesg, n, 0, pcliaddr, len); } } dg_echo funtion: echo lines on a datagram socket.
This function is a simple loop that reads the next datagram arriving at the server's port using recvfrom and sends it back using sendto. - First, this function never terminates. Since UDP is a connectionless protocol, there is nothing like an EOF as we have with TCP. - Next, this function provides an iterative server, not a concurrent server as we had with TCP. There is no call to fork, so a single server process handles any and all clients. - In general, most TCP servers are concurrent and most UDP servers are iterative
There are two connected sockets and each of the two connected sockets on the server host has its own socket receive buffer.
There is only one server process and it has a single socket on which it receives all arriving datagrams and sends all responses. - That socket has a receive buffer into which all arriving datagrams are placed
The main function is protocol
dependent (it creates a socket of protocol
and allocates and initializes an IPv socket address structure).
the dg_echo function is protocol
independent. The reason dg_echo is protocol
independent is because the caller must allocate a socket address structure of the correct size, and a pointer to this structure, along with its size, are passed as arguments to dg_echo.
The function dg_echo never looks inside this protocol ‐ dependent structure: It simply passes a pointer to the structure to recvfrom and sendto. - recvfrom fills this structure with the IP address and port number of the client, and since the same pointer (pcliaddr) is then passed to sendto as the destination address, this is how the datagram is echoed back to the client that sent the datagram.
An IPv socket address structure is filled in with the
address and port number of the server. This structure will be passed to dg_cli, specifying where to send datagrams.
socket is created and the function dg_cli is called.
#include “unp.h” void dg_cli(FILE *fp, int sockfd, const SA pservaddr, soklen_t servlen) { int n;char sendline[MAXLINE], recvline[MAXLINE+1]; while(Fgets(sendline, MAXLINE, fp) != NULL) { sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); recvline[n] = 0; / null terminate */ Fputs(recvline,stdout); } } dg_cli function: client processing loop
Notice that the call to recvfrom specifies a null pointer as the fifth and sixth arguments. This tells the kernel that we are not interested in knowing who sent the reply. - There is a risk that any process, on either the same host or some other host, can send a datagram to the client's IP address and port, and that datagram will be read by the client, who will think it is the server's reply. - As with the server function dg_echo, the client function dg_cli is protocol ‐ independent, but the client main function is protocol ‐ dependent. - The main function allocates and initializes a socket address structure of some protocol type and then passes a pointer to this structure, along with its size, to dg_cli.