Course 2 Client Socket
Course 2 Client Socket
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.
#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----
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.
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 ().
sockets
socket
socket
Socket (2)