Docsity

UDP Client-Server Programming: A Laboratory Exercise, Exercises of Data Communication Systems and Computer Networks

Partial preview of the text

Download UDP Client-Server Programming: A Laboratory Exercise and more Exercises Data Communication Systems and Computer Networks in PDF only on Docsity!

  • LAB 3LAB

OBJECTIVEOBJECTIVE ˆ^ To Learn the basic concepts of UDPˆ^ To Learn the basic concepts of UDP^ Client/Server model in networkprogramming

AssignmentAss gnment ˆ^ Open a text editor e.g. notepad.p^

g^ p

ˆ^ Type the following line in the notepad editor“This is a test message.” ˆ^ Save the file as ‘studentname.txt’ where‘studentname’ is your name. ˆ^ Modify the UDP client/server such that:ˆ^ Modify the UDP client/server such that:^ ^ Client sends the file name to the serverprovided on the command line e.g.p^

g

./client studename.txt ^ Server receives the file name, open the

d f l^ d

h^

f^ h^ f l

5

requested file, reads the contents of the fileand sends back to client

docsity.com

AssignmentAss gnment^ ^ Server receives the file name, open the

f^ ,^

p

requested file, reads the contents of the fileand sends back to client and closes the file.Cli^ t^

i^ s th^

t^ ts^ f fil

t s

^ Client receives the contents of file, creates anew file with the name ‘studentnamelab3.txt’and writes these contents to this file ^ Finally client closes the file

Socket I/O

: socket()

Socket I/O socket() ˆ^ The UDP server must create a

datagram

socket…

int fd;^

/* socket descriptor */

if((fd =^ socket

(AF_INET,^ S

OCK_DGRAM ,^

perror(“socket”);exit(1);}} • socket returns an integer (

socket descriptor

)

-^ fd^ < 0 indicates that an error occurred• AF_INET: associates a socket with the Internet protocol family• SOCK DGRAM:

selects the UDP protocol

8

-^ SOCK_DGRAM: selects the UDP protocol

docsity.com

Socket I/O

: bind()

Socket I/O b nd() ˆ^ A^ socket^ can be bound to a

port

int^ fd;^

/* socket^ descriptor

int^ fd;^

/^ socket^ descriptor

struct^ sockaddr_in

srv;^ /* used

by^ bind()^ / / create^ the

socket^ / / bind:^ use

the^ Internet

address^ family

srv.sin_family

=^ AF_INET ;

/* bind:^ socket

‘fd’^ to^ port

/^ bind:^ socket

fd^ to^ port

srv.sin_port^

=^ htons(^80 ); /*^ bind:^ a^ client

may^ connect^

to^ any^ of^ my^

addresses^ */

srv.sin addr.s

addr^ =^ htonl(

INADDR^ ANY );

srv.sin_addr.s_addr

htonl( INADDR_ANY

if( bind (fd,^

(struct^ sockaddr*)

&srv,^ sizeof(srv))

<^ 0)^ {

perror("bind");

exit(1); }

9

} • Now the UDP server is ready to accept packets…

docsity.com

Socket I/O: recvfrom()ti^

d

continued...^ nbytes =^ recvfrom

(fd, buf, sizeof(buf), 0 /* flags */,( t^ t^ k dd *)

li^ li l

(struct sockaddr*) cli, &cli_len);

• The actions performed by

recvfrom

p^

y

• returns the number of bytes read (

nbytes )

• copies^ nbytes

of data into

buf

p^ y • returns the address of the client (

cli )

• returns the length of

cli^ ( cli_len

g^ (

• don’t worry about flags

UDP Cl

ient Example

UDP Cl ent Example

2 UDP Clients

ˆ^ How does a

UDP client

ports

communicate with a

UDP

server?

TCP

p

IP Ethernet Adapter

p^12 docsity.com

Review: UDP Client-ServerI t^

ti

Interaction

UDP Server^ socket()bind() UDP Cli^ t^

recvfrom()

UDP^ Client socket()

blocks until datagramreceived from a client

sendto()

received from a client data request

sendto()

recvfrom()

data reply

Lecture 3: 9-4-

14

close()

from UNIX Network Programming Volume 1, figure 8.

The UDP ServerThe UDP Server^ UDP Server

ˆ^ How can the

UDP server

service multiple ports

Port 2000

Port 3000^

simultaneously?

UDP IP Ethernet Adapter

15

p

Listener.cL stener.c/* ** listener c

a datagram sockets "server"

** listener.c -- a datagram sockets

server

demo*// #include #include <stdio.h>#include <stdlib.h>#i^ l d^

i td h

#include <unistd.h>#include <errno.h>#i^ l d^

i^ h

17

#include <string.h>#include <sys/types.h>

Listener.cL stener.c#include <sys/socket.h>#include <netinet/in h>#include <netinet/in.h>#include <arpa/inet.h>#define MYPORT 4950

// the port users will

be connecting tobe connecting to#d fi^ MAXBUFLEN 100#define MAXBUFLEN 100i i (^ id)

18

int main(void){

Listener.cL stener.cperror("socket");exit(1);exit(1);} dd

i^ f^

il^ AF^

INET^

my_addr.sin_family = AF_INET;

host byte ordermy^ addr sin

port = htons(MYPORT);

my_addr.sin_port = htons(MYPORT);

short, network byte ordermy^ addr sin

addr s^

addr = INADDR

ANY;

my_addr.sin_addr.s_addr = INADDR_ANY;// automatically fill with my IPmemset(&(my

addr sin

zero) '\0' 8); // zero

20

memset(&(my

_addr.sin_zero),

\0 , 8); // zero

the rest of the struct ˆ

Listener.cL stener.cif (bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr)) ==

sizeof(struct sockaddr)) == -1) {perror("bind");it(1)exit(1);} addr_len = sizeof(struct sockaddr);if ((numbytes=recvfrom(sockfd, buf,MAXBUFLEN-1 , 0,(^

k dd^ *)& h i

dd

21

(struct sockaddr *)&their_addr,&addr_len)) == -1) {