0% found this document useful (0 votes)
79 views16 pages

Unix Network Programming: Herguan University 28th Sept., 2010

This document provides an overview of a university course on Unix Network Programming taught by Dr. Chethana Nagaraja. It outlines contact information, assessment details, and covers key topics like network layer addressing formats, address resolution protocols, introduction to sockets and common socket system calls like bind(), connect(), listen(), accept(), and close().
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views16 pages

Unix Network Programming: Herguan University 28th Sept., 2010

This document provides an overview of a university course on Unix Network Programming taught by Dr. Chethana Nagaraja. It outlines contact information, assessment details, and covers key topics like network layer addressing formats, address resolution protocols, introduction to sockets and common socket system calls like bind(), connect(), listen(), accept(), and close().
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Unix Network Programming

(CS546)

Lectured By
Dr. Chethana Nagaraja

Herguan University 28th Sept., 2010


Contact and Course Assessment

•Contact:

[email protected]
 408-481-9988

•Assessment:

 Attendance and Participation ---- 30%


 2 Assignments each 15% -------------- 30%
 End of Term Exam ---------------------- 40%

Herguan University 28th Sept., 2010


Contents

•Network Layer:

 Address format
 Address resolution

• Introduction to Sockets:

 Socket address format


 Socket system calls

Herguan University 28th Sept., 2010


Network Layer
•Governed by IP
•Information formatted as IP datagram
 Source and destination addresses
•IP functionality:
 Routing
 Fragmentation

IP Datagram

Fragmentation by source

IP Packet IP Packet

Reassembling by destination

IP Datagram

Herguan University 28th Sept., 2010


Address Format

IP Datagram

IP header (20 bytes) Data

Source address. (32-bits) Destination address.(32 bits)

Herguan University 28th Sept., 2010


Address Format

•4 formats
 Assigned by Network Information Centre (NIC) at SRI Intl.
• Class A

0 net-ID (7-bits) host-ID (24-bits)

• Class B

1 0 net-ID (14-bits) host-ID (16-bits)

• Class C

1 1 0 net-ID (21-bits) host-ID (8-bits)

•Class D
 Multicast addresses

Herguan University 28th Sept., 2010


Address Format

•Optional subnet addresses

Class A 0 net-ID (7-bits) host-ID (24-bits)

Class A 0 net-ID (7-bits) subnet-ID (8-bits) host-ID (16-bits)

Herguan University 28th Sept., 2010


Address Resolution

• Address Resolution Protocol (ARP):

 Resolves the Ethernet address by broadcasting the Internet address.

• Reverse Address Resolution Protocol (RARP):

 Resolves the Internet address by broadcasting Ethernet address via the


RARP packet within LAN.

Herguan University 28th Sept., 2010


Introduction to Sockets
• Sockets provide an API for communication within a computer network
SERVER

socket ( ) Socket System Calls

bind ( )

CLIENT
listen ( )
socket ( )

accept ( )
connect ( )
blocks until client connects connected

read ( ) write ( )
data (request)

process request

write ( ) read ( )
data (reply)

Herguan University 28th Sept., 2010


Socket Address Format
• Most of the socket system calls require socket address information.
• Socket address is stored as Structures in <netinet/in.h> header file.

AF_INET length AF_INET6


length

16-bits port no. 16-bits port no.

32-bits IPv4 address 32-bits flow label

unused 128-bits IPv6 address

(a) (b)

Socket address format (a) IPv4 fixed length (16 bytes) and (b) IPv6 fixed length (24 bytes)

Herguan University 28th Sept., 2010


Socket System Calls
• socket ( ):
 Initiates network I/O operations
 Format:
#include <sys/socket.h>
int socket (int family, int type, int protocol);

 Parameters:

Type Description
Family Description
SOCK_STREAM Stream socket
AF_INET IPv4 Protocol
SOCK_DGRAM Datagram socket
AF_INET6 IPv6 Protocol
SOCK_RAW Raw socket

protocol set to 0 except for raw sockets.

socket ( ) returns non-negative integer called socket descriptor on success,


else returns -1 on error.

Herguan University 28th Sept., 2010


Socket System Calls

• bind ( ):
 Assigns a local protocol address to the socket.

 Format:
#include <sys/socket.h>
int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addelen);

 bind ( ) returns 0 on success, else -1 on error.

 Purpose of bind ( ):

 Provides identity to server and client via an address which facilitates communication
between server and client.

Herguan University 28th Sept., 2010


Socket System Calls

• connect ( ):
 Used by the client to connect with the server.
 Format:
#include <sys/socket.h>
int connect (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);

 connect ( ) returns 0 on establishing a connection, else -1 on error.


 For TCP client, connect ( ) initiates 3-way hand-shake process.
 In case of error during 3-way hand-shake, connect ( ) returns with:
 ETIMEDOUT
 ECONNREFUSED
 EHOSTUNREACH
 ENETUNREACH

Herguan University 28th Sept., 2010


Socket System Calls

• listen ( ):
 Used by the server to indicate that it is ready to connect.

 Format:
#include <sys/socket.h>
int listen (int sockfd, int backlog);

 listen ( ) returns 0 on establishing a connection, else -1 on error.

 backlog specifies the maximum number of connections that the kernel can
queue.
 Incomplete connection queue: Sockets in SYN_RCVD state.
 Complete connection queue: Sockets in ESTABLISHED state.

Herguan University 28th Sept., 2010


Socket System Calls

• accept ( ):
 Executed by the server to return the completed connection from the front of the
queue.

 Format:
#include <sys/socket.h>
int accept (int sockfd, struct sock *cliaddr, socklen_t *addrlen);

 accept ( ) returns nonnegative integer called the connect socket on success, else
-1 on error.

 *addrlen is a value-result argument


 stores size of the connected client protocol address prior the call.
 stores the byte size returned by the kernel after the call.

Herguan University 28th Sept., 2010


Socket System Calls

• close ( ):
 Terminates the connection by closing the socket.

 Format:
#include <unistd.h>
int close (int sockfd);

 close ( ) returns 0 on success, else -1 on error.

Herguan University 28th Sept., 2010

You might also like