0% found this document useful (0 votes)
59 views22 pages

Course 2 Client Socket

This document discusses sockets and their use in client-server applications. Sockets allow processes to communicate over a network in a transparent way by respecting efficiency and compatibility. They can be used to establish connections between clients and servers. There are different types of sockets including datagram for connectionless communication and stream for connected data flow. The document provides examples of how to create sockets, bind them to an address, and use send and receive operations for client-server communication using UDP.

Uploaded by

KRist VErushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views22 pages

Course 2 Client Socket

This document discusses sockets and their use in client-server applications. Sockets allow processes to communicate over a network in a transparent way by respecting efficiency and compatibility. They can be used to establish connections between clients and servers. There are different types of sockets including datagram for connectionless communication and stream for connected data flow. The document provides examples of how to create sockets, bind them to an address, and use send and receive operations for client-server communication using UDP.

Uploaded by

KRist VErushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Aplikimet multimediale

Lecture: ada gogu


[email protected]

Objectives for the design of socket


interface

Sockets are designed to allowx the communivation


between processes through a network by respecting:
transparence : the communications between process in a
machine are identical even if these processes are situated
in different
efficacity : implemented as a system library in Kernel, they
have a direct access to drivers of network interface by
avoiding un double copy of data buffer . These
communications provoke a system call from the user
interface.
compatibility : sockets seen as FILE ID=> allows usual
operations of ridirecting the Input/Output flows(dup()) ;

Client server and connection


establishment
The most common characteristic of "client-server"
mode is the asymmetry:
- First, a server process is preparing, he is in a listening
mode toward a communication port. This is still the
"half of the binding.
- Then, a client process will send a connection request
to this already existing server Port.

- Then the connection is established and data can flow


in both directions.

Socket connection (1)

Socket connection (2)

Sockets type (1)


- The datagram type, enabling the exchange of
complete or structured messages without
prior negotiation (communication without
connection).
- The type connected, allowing the exchange of
data
flow
on
a previously established virtual circuit.

Sockets type (2)


1. sockets SOCK_DGRAM : datagrammes (messages),
-- non-connected mode. Protocol UDP over IP :
messages are limited in size.
no guarantee for receiving ("best effort"),
no guarantee for receiving in the sending order.

2. sockets SOCK_STREAM : data flows, connected mode,


protocole TCP/IP :
secure data transferring (no lose of data or reinverse order
. ),
receiving in the sending order.
no duplication,

Sockets types (3)


3. sockets SOCK_RAW : allows to access the
protocols IP (of lower level) and it is
reservedd to supervisor mode.

4. sockets SOCK_SEQPACKET : packets which


preserve are limited, delivred in secure mode,
without reversing the ordre.

Socket creation
From the user interface, a socket can been seen as a
file ID. We can direct the Input/Output flows of a
program to the sockets.
Sockets are the processes created by fork.
A socket is created from sd = socket(). The return
value is the ID and we use it to perform the read
and write operations.
The difference with the files ID is that the sockets
have to be binded with a doamin to whom it
belongs.

Creation of the socket


socket - create an endpoint for communication

#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
Domain: family protocol to interpret the address e.x. IPv4 (PF_INET),
IPv6 (PF_INET6), etc.
Type: SOCK_STREAM , SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET

Protocol: Usually, the third option is 0 because there is not yet any
disponible protocol (e.x. SOCK_DGRAM in the AF_INET has only
UDP, while SOCK_STREAM has only TCP).

Example
1) sd = socket (AF_INET, SOCK_DGRAM, 0);
2) sd = socket (AF_INET, SOCK_STREAM, 0);
Attachement (bind)
In linux----------$ man 2 bind
NAME
bind - bind a name to a socket
-----------------------------------------------------------------------------#include <sys/types.h>
#include <sys/socket.h>
int bind (int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
----addrlen is the size of the structure my_addr----

Client-server with UDP

send
$ man 2 send

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

ssize_t send ( int s, const void *msg, size_t len, int flags);
ssize_t sendto (int s, const void *msg, size_t len, int flags, const struct sockaddr *to,
socklen_t tolen);
ssize_t sendmsg (int s, const struct msghdr *msg, int flags);

DESCRIPTION
1.

Send, sendto, and sendmsg are used to transmit a message to


another socket. Send may be used only when the socket is in a
connected state, while sendto and sendmsg may be used at any time.

2.

The address of the target is given by to with tolen specifying its size.
The length of the message is given by len. If the message is too long to
pass atomically through the underlying protocol, the error EMSGSIZE
is returned, and the message is not transmitted.

3. Transmetimi mund t behet ose nga klienti ose nga serveri. I vetmi
kufizim sht se duhet t programohet n mnyr q te kete recv ()
aq sa destinacione ka ne send ().

Exemple de server and client UDP


An UDP server and a UDP client:
(1) server is put in waiting mode over an UDP port
(2) client creates its local socket
(3) client sends its message to the host and the port UDP
of server
(4) After receiving the message, the server fet the adress
(host port) of the sender, get the message and sends
the reply

sockets

socket

socket

Socket client (1)

Socket (2)

End second course

You might also like