PG 2
PG 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.
Server forms the listener socket while client reaches out to the server.
Stages for server
● Socket creation:
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.
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.
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