UDP Client Server-Computers And Network Programming-Lecture Slides, Slides of Network Programming

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

2011/2012

Uploaded on 07/23/2012

gannesh
gannesh 🇮🇳

4.4

(12)

75 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
NetworkProgramming
(UDPclient/server)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download UDP Client Server-Computers And Network Programming-Lecture Slides and more Slides Network Programming in PDF only on Docsity!

Network

Programming

(UDP

client/server)

UDP

Echo

Server:

main

Function

#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

UDP

Echo

Server:dg_echo Function

#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.

Read

datagram,

echo

back

to

sender

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

Read

datagram,

echo

back

to

sender

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

TCP

client/server

with

two

clients

Continued..

The main function is protocol

dependent (it creates a socket of protocol

AF_INET

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.

Continued..

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.

Fill

in

socket

address

structure

with

server's

address

An IPv socket address structure is filled in with the

IP

address and port number of the server. This structure will be passed to dg_cli, specifying where to send datagrams.

A

UDP

socket is created and the function dg_cli is called.

UDP

Echo

Client:

dg_cli Function

#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

Continued…

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.