20
Network sockets Quick overview

Network sockets

Embed Size (px)

Citation preview

Page 1: Network sockets

Network sockets

Quick overview

Page 2: Network sockets

Sockets typesRaw socket

Stream socket (SOCK_STREAM)

Datagram socket (SOCK_DGRAM)

Page 3: Network sockets

Data typesstruct addrinfo

struct sockaddr

struct sockaddr_in

struct in_addr

struct sockaddr_storage

Page 4: Network sockets

Helper functionsinet_pton() - convert address from str to int

inet_ntop() - convert address from int to str

Page 5: Network sockets

Address binding

Local addr.port Remote addr.port Description

*.port *.* All local interfaces

laddr.lport *.* Specific local interface

laddr.lport raddr.rport One client

Page 6: Network sockets

Server initgetaddrinfo() - convert hostname to ip address

socket() - create a socket

bind() - associate with a port to listen on

listen() - marks the socket referred as a passive socket and setups the number of pending connections

accept() - block waiting on a new connection

getnameinfo() - get host and port as strings

Note: Use setsockopt with SO_REUSEADDR to steal the port

Page 7: Network sockets

Client initgetaddrinfo() - convert hostname to ip address

socket() - create a socket

connect() - connect to a remote port (optionally)

Page 8: Network sockets

Communication over TCP socketssend() - returns number of sent bytes or -1

recv() - returns number of received bytes or 0 (closed conn on a remote side) or -1

Page 9: Network sockets

Communication over UDP socketssendto() - returns number of sent bytes or -1

recvfrom() - returns number of received bytes or -1

OR, if connect() was called, use:

send() - returns number of sent bytes or -1

recv() - returns number of received bytes or -1

Page 10: Network sockets

Closing socketclose() - close and free

shutdown() - close not free with option:SHUT_RD

SHUT_RDWR

SHUT_WR

Page 11: Network sockets

Get infogetpeername() - remote side address

gethostname() - local hostname

Page 12: Network sockets

Socket settingsBlocking mode - fcntl(O_NONBLOCK)

Page 13: Network sockets

Multiplexing servergetaddrinfo() - convert hostname to ip address

socket() - create a socket

bind() - associate with a port to listen on

listen() - marks the socket referred as a passive socket and setups the number of pending connections

select/poll/epoll/kqueue - blocks waiting on multiple connections events

getnameinfo() - get host and port as strings

Page 14: Network sockets

Multiplexing using selectselect(num, readfds, writefds, exceptfds, timeout)

1. Manipulate bitmaps of max size 1024 bits using macros FD_SET, FD_CLR, FD_ISSET, FD_ZERO

2. Select returns modifies bitmaps thus applications should refill the interest sets for every call.

3. Thus both application and kernel have to scan entire bitmaps on each call to figure out interest sets and the result sets.

4. Moreover kernel iterates over interest set to find out which file descriptor is ready.

Conclusion : Using select is slow

Page 15: Network sockets

Multiplexing using pollCons

App and kernel still have to scan to figure out interest and result fd sets

Kernel still have to check which fd of interest are ready as far as there is no state saved.

API

poll(struct pollfd *fds, int nfds, int timeout)

Pros

Relies on array of file descriptors instead of bitmaps. Thus no 1024 limit.

Interest and result sets are separated, so app does not need to update it each time

Page 16: Network sockets

Multiplexing using epoll in LinuxAPI

int epoll_create(int size) - Create interest set

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) - Add fd

int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout) - Wait

Two modes: level triggered and edge triggered

Pros

Stateful, no scan on each time is required

Multiple interest sets in one process

Cons

Can not add multiple descriptors at once

Page 17: Network sockets

Multiplexing using kqueue in FreeBSDAPI

int kqueue(void);int kevent(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents, const struct timespec *timeout);

Pros

Stateful, no scan on each time is required

Multiple interest sets in one process

Can add multiple descriptors at once

Can work not only for sockets, but for files, timers, signals, ...

Page 18: Network sockets

Broadcastsetsockopt (SO_BROADCAST)

Page 19: Network sockets

ReferencesUsing network sockets

Scalable Event Multiplexing: epoll vs. kqueue

The C10K problem

Re: Linux's implementation of poll() not scalable?

How to use epoll? A complete example in C

Page 20: Network sockets

My blogLearning Network Programming