Starting from:

$30

Lab 4: UDP/IP Socket Programming

COEN 146: Computer Networks
Lab 4: UDP/IP Socket Programming

Objectives
1.    To develop client - server (P2P) applications using UDP/IP Sockets
2.    To write a C program to transfer file over UDP/IP Socket

UDP/IP Client-Server  
The following figure shows the steps taken by each program:

 

On the client and server sides:

The socket() function creates an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets.

int sockfd = socket(domain, type, protocol)
•    sockfd: socket descriptor, an integer (like a file-handle)
•    domain: integer, communication domain e.g., AF_INET (IPv4 protocol) , AF_INET6 (IPv6 protocol), AF_UNIX (local channel, similar to pipes)
•    type: communication type
SOCK_STREAM: TCP (reliable, connection oriented)
SOCK_DGRAM: UDP (unreliable, connectionless)
SOCK_RAW (direct IP service)
•    protocol: This is useful in cases where some families may have more than one protocol to support a given type of service. Protocol value for Internet Protocol (IP), which is 0. This is the same number which appears on protocol field in the IP header of a packet.

#include <sys/socket.h>
...
...if ((server_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
{
    perror(“cannot create socket”); 
    return 0; 
}



On the server side:

After creation of the socket, bind() function binds the socket to the address and port number specified in addr(custom data structure). In the example code, we bind the server to the localhost, hence we use INADDR_ANY to specify the IP address.

int bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen);
•    addr: Points to a sockaddr structure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket.
•    addrlen: Specifies the length of the sockaddr structure pointed to by the addr argument. 

sendto() and receivefrom() over socket:

     sendto(sockfd, sbuf, strlen(sbuf), 0, (struct sockaddr *)&servAddr, 
                                                     sizeof(struct sockaddr));

     recvfrom(sockfd, rbuf, strnlen(rbuf), 0,(struct sockaddr *)&clienAddr, 
                                                     sizeof(struct sockaddr));
    
Structures:

Address format
An IP socket address is defined as a combination of an IP interface address and a 16-bit port number. The basic IP protocol does not supply port numbers, they are implemented by higher level protocols like UDP and TCP. On raw sockets sin_port is set to the IP protocol.

struct sockaddr_in {
    sa_family_t    sin_family; /* address family: AF_INET */
    in_port_t      sin_port;   /* port in network byte order */
    struct in_addr sin_addr;   /* internet address */
};

/* Internet address. */
struct in_addr {
    uint32_t       s_addr;     /* address in network byte order */
};

This is defined in netinet/in.h

sin_family is always set to AF_INET. 

sin_port contains the port in network byte order. The port numbers below 1024 are called privileged ports (or sometimes: reserved ports). Only privileged processes) may bind to these sockets.

sin_addr is the IP host address. 

s_addr member of struct in_addr contains the host interface address in network byte order. 
in_addr should be assigned one of the INADDR_* values (e.g., INADDR_ANY) or set using the inet_aton, inet_addr, inet_makeaddr library functions or directly with the name resolver (see gethostbyname).

INADDR_ANY allows your program to work without knowing the IP address of the machine it was running on, or, in the case of a machine with multiple network interfaces, it allowed your server to receive packets destined to any of the interfaces. 

INADDR_ANY has the following semantics: When receiving, a socket bound to this address receives packets from all interfaces. For example, suppose that a host has interfaces 0, 1 and 2. If a UDP socket on this host is bound using INADDR_ANY and udp port 8000, then the socket will receive all packets for port 8000 that arrive on interfaces 0, 1, or 2. If a second socket attempts to Bind to port 8000 on interface 1, the Bind will fail since the first socket already “owns” that port/interface.
Example:
serv_addr.sin_addr.s_addr = htonl (INADDR_ANY);
•    Note: "Network byte order" always means big endian. "Host byte order" depends on architecture of host. Depending on CPU, host byte order may be little endian, big endian or something else. 
•    The htonl() function translates a long integer from host byte order to network byte order.

To bind socket with localhost, before you invoke the bind function, sin_addr.s_addr field of the sockaddr_in structure should be set properly. The proper value can be obtained either by 
my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1")
or by 
my_sockaddress.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
To convert an address in its standard text format into its numeric binary form use the inet_pton() function. The argument af specifies the family of the address. 

#define _OPEN_SYS_SOCK_IPV6
#include <arpa/inet.h>

int inet_pton(int af, const char *src, void *dst);

Recap - File transfer:
•    Binary file: jpg, png, bmp, tiff etc.
•    Text file: txt, html, xml, css, json etc.
You may use functions or system calls for file tranfer. C Function connects the C code to file using I/O stream, while system call connects C code to file using file descriptor.
•    File descriptor is integer that uniquely identifies an open file of the process.
•    I/O stream sequence of bytes of data.
A Stream provide high level interface, while File descriptor provide a low-level interface. Streams are represented as FILE * object, while File descriptors are represented as objects of type int.

C Functions to open and close a binary/text file
fopen(): C Functions to open a binary/text file, defined as:
FILE *fopen(const char *file_name, const char *mode_of_operation);
where:
•    file_name: file to open
•    mode_of_operation: refers to the mode of the file access, For example:- r: read , w: write , a: append etc
•    fopen() return a pointer to FILE if success, else NULL is returned
•    fclose(): C Functions to close a binary/text file.

fclose(): C Functions to close a binary/text file, defined as:
fclose( FILE *file_name);
Where:
•    file_name: file to close
•    fclose () function returns zero on success, or EOF if there is an error

C Functions to read and write a binary file
fread(): C function to read binary file, defined as:
fread(void * ptr, size_t size, size_t count, FILE * stream);
where:
•    ptr- it specifies the pointer to the block of memory with a size of at least (size*count) bytes to store the objects.
•    size - it specifies the size of each objects in bytes.
•    count: it specifies the number of elements, each one with a size of size bytes.
•    stream - This is the pointer to a FILE object that specifies an input stream.
•    Returns the number of items read


fwrite (): C function to write binary file, defined as:
fwrite (void *ptr, size_t size, size_t count, FILE *stream);
where:
•    Returns number of items written
•    *arguments of fwrite are similar to fread. Only difference is of read and write.

For example:
To open "lab4.dat" file in read mode then function would be:
FILE* demo; // demo is a pointer of type FILE
char buffer[100]; // block of memory (ptr)
demo= fopen("lab4.dat", "r"); // open lab4.dat in read mode
fread(&buffer, sizeof(buffer), 1, demo); // read 1 element of size = size of buffer (100)
fclose(demo); // close the file

C Functions to read and write the text file.
fscanf (): C function to read text file.
fscanf(FILE *ptr, const char *format, ...)
Where:
•    Reads formatted input from the stream.
•    Ptr: File from which data is read.
•    format: format of data read.
•    returns the number of input items successfully matched and assigned, zero if failure

fprintf(): C function to write a text file.
fprintf(FILE *ptr, const char *format, ...);
Where:
•    *arguments similar to fscanf ()

For example:
FILE *demo; // demo is a pointer of type FILE
demo= FILE *fopen("lab4.txt", "r"); // open lab4.txt in read mode
/* Assuming that lab4.txt has content in below format
City
Population
….
*/
char buf[100]; // block of memory
fscanf(demo, "%s", buf); // to read a text file
fclose(demo); // close the file
*to read whole file use while loop


System Call to open, close, read and write a text/binary file.
open(): System call to open a binary/text file, defined as:
open (const char* Path, int flags [, int mode ]);
Where:
•    returns file descriptor used on success and -1 upon failure
•    Path :- path to file
•    flags :- O_RDONLY: read only, O_WRONLY: write only, O_RDWR: read and write, O_CREAT: create file if it doesn’t exist, O_EXCL: prevent creation if it already exists

close(): System call to close a binary/text file, defined as:
close(int fd);
where:
•    return 0 on success and -1 on error.
•    fd : file descriptor which uniquely identifies an open file of the process

read(): System call to read a binary/text file.
read (int fd, void* buf, size_t len);
where:
•    returns 0 on reaching end of file, -1 on error or on signal interrupt
•    fd: file descriptor
•    buf: buffer to read data from
•    len: length of buffer

write(): System call to write a binary/text file.
write (int fd, void* buf, size_t len);
where:
•    *arguments and return of write are similar to read(). 

For example:
int fd = open("lab4.dat", O_RDONLY | O_CREAT); //if file not in directory, file is 
created
Close(fd);


Implementation steps:
Step 1.    [40%] Write a C program for a UDP server that receives a file from a client. 
Step 2.    [35%] Write a C program for a UDP client sends a file to a server. Recall UDP is connectionless and reliable. In Lab5, you will implement a reliable data transfer over UDP.
Step 3.    Compile and run. Note: you may use the IP address 127.0.0.1 (loop back IP address) for a local host, i.e. both of your client and server run on the same machine. 

[25%] Demonstrate your program to the TA:
a.    Your client and server on your same machine
b.    Your client and your classmate’s server IP address (be careful!). You may to discuss with the TA if you run into access problems   

Requirements to complete the lab
1.    Demo to the TA correct execution of your programs [recall: a successful demo is 25% of the grade]
2.    Submit the source code of your program as .c files 

Please start each program with a descriptive block that includes minimally the following information:
/* 
* Name: <your name> 
* Date: <date> (the day you have lab)
* Title: Lab4 - Part …. 
* Description: This program  … <you should 
* complete an appropriate description here.> 
      */

More products