0% found this document useful (0 votes)
12 views6 pages

PG 2

The document outlines the use and functioning of system calls for network programming in Linux, focusing on socket programming with TCP and UDP protocols. It details the stages for both server and client socket communication, including socket creation, binding, listening, accepting connections, and data transmission functions like send and read for TCP, as well as sendto and recvfrom for UDP. The document emphasizes the differences between the connection-oriented TCP and the connectionless UDP protocols.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

PG 2

The document outlines the use and functioning of system calls for network programming in Linux, focusing on socket programming with TCP and UDP protocols. It details the stages for both server and client socket communication, including socket creation, binding, listening, accepting connections, and data transmission functions like send and read for TCP, as well as sendto and recvfrom for UDP. The document emphasizes the differences between the connection-oriented TCP and the connectionless UDP protocols.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

No:2

Date:
SYSTEM CALLS USED FOR NETWORK PROGRAMMING IN LINUX

Aim:
familiarize and understand the use and functioning of system calls used for
network programming in Linux
socket programming
Socket programming is a way of connecting two nodes on a network to
communicate with each other. One socket(node) listens on a particular
port at an IP, while other socket reaches out to the other to form a
connection.

Two types of transmission protocols: TCP and UDP

TCP is the abbreviation of Transfer Control Protocol. It is a connection


oriented protocol to ensure reliable transmission. Through TCP protocol
transmission, we get a sequential error free data stream. A connection
must be established between the two paired sockets of the sender and
the receiver in order to communicate on the basis of TCP protocol.
When one socket (usually server socket) waits to establish a connection,
the other socket can request a connection. Once the two sockets are
connected, they can carry out two-way data transmission, Both parties
can send or receive.

Socket communication of TCP protocol

Server forms the listener socket while client reaches out to the server.
Stages for server
● Socket creation:

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)
type: communication type
SOCK_STREAM: TCP(reliable, connection oriented)
SOCK_DGRAM: UDP(unreliable, connectionless)
protocol: Protocol value for Internet Protocol(IP), which is 0.
● Bind:

● int bind(int sockfd, const struct sockaddr *addr,

socklen_t addrlen);
After creation of the socket, bind function binds the socket to the
address and port number specified in addr(custom data structure).
Listen:
int listen(int sockfd, int backlog);
It puts the server socket in a passive mode, where it waits for the client
to approach the server to make a connection. The backlog, defines the
maximum length to which the queue of pending connections for sockfd
may grow. If a connection request arrives when the queue is full, the
client may receive an error with an indication of ECONNREFUSED.
Accept:
int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t
*addrlen);
It extracts the first connection request on the queue of pending
connections for the listening socket, sockfd, creates a new
connected socket, and returns a new file descriptor referring to that
socket. At this point, connection is established between client and
server, and they are ready to transfer data.
send
ssize_t send(int socket, const void *buffer,
size_t length, int flags);
The send() function shall initiate transmission of a
message from the specified socket to its peer. The send()
function shall send a message only when the socket is
connected.

The send() function takes the following arguments:

socket
Specifies the socket file descriptor.
buffer
Points to the buffer containing the message to send.
length
Specifies the length of the message in bytes.
flags
Specifies the type of message transmission.

Upon successful completion, send() shall return the


number of bytes sent. Otherwise, -1 shall be returned
and errno set to indicate the error.
read
ssize_t read(int fildes, void *buf, size_t nbyte);
The read() function shall attempt to read nbyte bytes from the
file associated with the open file descriptor, fildes, into the
buffer pointed to by buf.
Upon successful completion, read shall return a non-negative
integer indicating the number of bytes actually read.
Otherwise, the functions shall return -1

Stages for Client


Socket connection: Exactly same as that of server’s socket creation
Connect:
int connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
The connect() system call connects the socket referred to by
the file descriptor sockfd to the address specified by addr.
Server’s address and port is specified in addr.

Socket communication of UDP Protocol

UDP is the abbreviation of User Datagram Protocol. It is a


connectionless protocol. Each datagram is an independent information,
including a complete source address or destination address. It is
transmitted to the destination by any possible path on the network.
Therefore, whether it can reach the destination, the time to reach the
destination and the correctness of the content cannot be guaranteed.

Datagrams upon arrival contain the address of the sender which the
server uses to send data to the correct client.
The entire process can be broken down into the following steps :
UDP Server :
1. Create a UDP socket.
2. Bind the socket to the server address.
3. Wait until the datagram packet arrives from the client.
4. Process the datagram packet and send a reply to the client.
5. Go back to Step 3.
UDP Client :
1. Create a UDP socket.
2. Send a message to the server.
3. Wait until response from the server is received.
4. Process reply and go back to step 2, if necessary.
5. Close socket descriptor and exit.

Necessary Functions :
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen)
Send a message on the socket
Arguments :
sockfd – File descriptor of the socket
buf – Application buffer containing the data to be sent
len – Size of buf application buffer
flags – Bitwise OR of flags to modify socket behavior
dest_addr – Structure containing the address of the
destination
addrlen – Size of dest_addr structure

ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,


struct sockaddr *src_addr, socklen_t *addrlen)
Receive a message from the socket.
Arguments :
sockfd – File descriptor of the socket
buf – Application buffer in which to receive data
len – Size of buf application buffer
flags – Bitwise OR of flags to modify socket behavior
src_addr – Structure containing source address is returned
addrlen – Variable in which size of src_addr structure is returned

int close(int fd)


Close a file descriptor
Arguments:
fd – File descriptor

You might also like