Operating Systems II Unit OS A: Networking A.2. Windows Sockets Programming
- Prof. Dr. Andreas Polze,
Andreas Grapentin, Bernhard Rabe
Operating Systems II Unit OS A: Networking A.2. Windows Sockets - - PowerPoint PPT Presentation
Operating Systems II Unit OS A: Networking A.2. Windows Sockets Programming Prof. Dr. Andreas Polze, Andreas Grapentin, Bernhard Rabe Roadmap for Sec.on A.2 General Concepts - Berkeley Sockets
Andreas Grapentin, Bernhard Rabe
2 ¡
3 ¡
4 ¡
5 ¡
Server ¡
socket() ¡ bind() ¡ listen() ¡ accept() ¡ read() ¡ write() ¡ socket() ¡
Client ¡
connect() ¡ write() ¡ read() ¡ connec.on ¡establishment ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡connec.on ¡ from ¡client ¡
6 ¡
Server ¡
socket() ¡ bind() ¡ recvfrom() ¡ sendto() ¡ socket() ¡
Client ¡
sendto() ¡ recvfrom() ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡data ¡ received ¡from ¡client ¡
7 ¡
Server ¡
t_open() ¡ t_bind() ¡ t_listen() ¡ t_rcv() ¡ t_snd() ¡ t_open() ¡
Client ¡
t_connect() ¡ t_snd() ¡ t_rcv() ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡connec.on ¡ from ¡client ¡ t_alloc() ¡ t_bind() ¡ t_alloc() ¡ t_accept() ¡ connec.on ¡establishment ¡
8 ¡
Server ¡
t_open() ¡ t_bind() ¡ t_rcvudata() ¡ t_sndudata() ¡ t_open() ¡
Client ¡
t_sndudata() ¡ t_rcvudata() ¡ data ¡(request) ¡ data ¡(reply) ¡ blocks ¡un.l ¡data ¡ received ¡from ¡client ¡ t_alloc() ¡ t_bind() ¡ t_alloc() ¡
9 ¡
#include ¡<winsock.h> ¡ ¡ SOCKET ¡socket ¡( ¡ ¡ ¡int ¡af, ¡int ¡type, ¡int ¡protocol ¡); ¡
10 ¡
#include ¡<winsock.h> ¡ ¡ SOCKET ¡accept ¡( ¡ ¡ ¡SOCKET ¡s, ¡struct ¡sockaddr ¡FAR ¡* ¡addr, ¡ ¡ ¡int ¡FAR ¡* ¡addrlen ¡); ¡
11 ¡
/* * Structure used by kernel to store most * addresses. */ struct sockaddr { u_char sa_len; /* total length */ u_char sa_family; /* address family */ char sa_data[14]; /* actually longer; address value*/ }; #define SOCK_MAXADDRLEN 255 /* longest possible addresses */
12 ¡
struct ¡sockaddr ¡{ ¡ ¡ u_short ¡sa_family; ¡ ¡ char ¡sa_data[14]; ¡ ¡ }; ¡ #include ¡<winsock.h> ¡ ¡ int ¡bind ¡( ¡ ¡ ¡SOCKET ¡s, ¡const ¡struct ¡sockaddr ¡FAR ¡* ¡name, ¡ ¡ ¡int ¡namelen ¡); ¡
13 ¡
– a ¡host ¡address, ¡the ¡protocol ¡number ¡(set ¡implicitly ¡to ¡UDP ¡or ¡TCP, ¡respec.vely), ¡and ¡a ¡ port ¡number ¡which ¡iden.fies ¡the ¡applica.on. ¡ ¡ – If ¡an ¡applica.on ¡does ¡not ¡care ¡what ¡address ¡is ¡assigned ¡to ¡it, ¡it ¡may ¡specify ¡an ¡Internet ¡ address ¡equal ¡to ¡INADDR_ANY, ¡a ¡port ¡equal ¡to ¡0, ¡or ¡both. ¡ ¡ – If ¡the ¡Internet ¡address ¡is ¡equal ¡to ¡INADDR_ANY, ¡any ¡appropriate ¡network ¡interface ¡ will ¡be ¡used; ¡this ¡simplifies ¡applica.on ¡programming ¡in ¡the ¡presence ¡of ¡mul.-‑homed ¡
– If ¡the ¡port ¡is ¡specified ¡as ¡0, ¡the ¡Windows ¡Sockets ¡implementa.on ¡will ¡assign ¡a ¡unique ¡ port ¡to ¡the ¡applica.on ¡with ¡a ¡value ¡between ¡1024 ¡and ¡5000. ¡ ¡
assigned ¡to ¡it ¡ – getsockname() ¡will ¡not ¡necessarily ¡fill ¡in ¡the ¡Internet ¡address ¡un.l ¡the ¡socket ¡is ¡ connected; ¡several ¡Internet ¡addresses ¡may ¡be ¡valid ¡if ¡the ¡host ¡is ¡mul.-‑homed. ¡
14 ¡
SOCKADDR_IN sin; SOCKET s; u_short alport = IPPORT_RESERVED; /* 1024 */ sin.sin_family = AF_INET; sin.sin_addr.s_addr = 0; for (;;) { sin.sin_port = htons(alport); if (bind(s, (LPSOCKADDR)&sin, sizeof (sin)) == 0) { /* it worked */ } if ( GetLastError() != WSAEADDRINUSE) { /* fail */ } alport--; if (alport == IPPORT_RESERVED/2 ) { /* fail--all unassigned reserved ports are in use.*/ } }
15 ¡
– releases ¡the ¡socket ¡descriptor ¡s, ¡so ¡that ¡further ¡references ¡to ¡s ¡will ¡fail ¡with ¡ the ¡error ¡WSAENOTSOCK. ¡ ¡ – If ¡this ¡is ¡the ¡last ¡reference ¡to ¡the ¡underlying ¡socket, ¡the ¡associated ¡naming ¡ informa.on ¡and ¡queued ¡data ¡are ¡discarded. ¡
#include ¡<winsock.h> ¡ ¡ int ¡closesocket ¡( ¡SOCKET ¡s ¡); ¡
16 ¡
#include ¡<winsock.h> ¡ ¡ int ¡connect ¡( ¡SOCKET ¡s, ¡ ¡ ¡ ¡const ¡struct ¡sockaddr ¡* ¡name, ¡ ¡ ¡ ¡int ¡namelen ¡); ¡
17 ¡
.me: ¡ ¡
– if ¡a ¡connec.on ¡request ¡arrives ¡with ¡the ¡queue ¡full, ¡the ¡client ¡will ¡receive ¡an ¡error ¡with ¡ an ¡indica.on ¡of ¡WSAECONNREFUSED ¡
#include ¡<winsock.h> ¡ ¡ int ¡listen ¡( ¡SOCKET ¡s, ¡int ¡backlog ¡); ¡
18 ¡
#include ¡<winsock.h> ¡ ¡ int ¡recv ¡( ¡SOCKET ¡s, ¡ ¡ ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags ¡); ¡
19 ¡
#include ¡<winsock.h> ¡ ¡ int ¡recvfrom ¡( ¡SOCKET ¡s, ¡ ¡ ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags, ¡ ¡ ¡struct ¡sockaddr ¡* ¡from, ¡int ¡* ¡fromlen ¡); ¡ ¡
20 ¡
This ¡argument ¡is ¡ignored ¡and ¡included ¡only ¡for ¡the ¡sake ¡of ¡compa.bility. ¡ ¡
An ¡op.onal ¡pointer ¡to ¡a ¡set ¡of ¡sockets ¡to ¡be ¡checked ¡for ¡readability. ¡ ¡
An ¡op.onal ¡pointer ¡to ¡a ¡set ¡of ¡sockets ¡to ¡be ¡checked ¡for ¡writability ¡ ¡
An ¡op.onal ¡pointer ¡to ¡a ¡set ¡of ¡sockets ¡to ¡be ¡checked ¡for ¡errors. ¡ ¡
The ¡maximum ¡.me ¡for ¡select() ¡to ¡wait, ¡or ¡NULL ¡for ¡blocking ¡opera.on. ¡ #include ¡<winsock.h> ¡ ¡ int ¡select ¡( ¡int ¡nfds, ¡fd_set ¡* ¡readfds, ¡ ¡ ¡ ¡fd_set ¡* ¡writefds, ¡fd_set ¡* ¡excepuds, ¡ ¡ ¡ ¡const ¡struct ¡.meval ¡* ¡.meout ¡); ¡
21 ¡
#include ¡<winsock.h> ¡ ¡ int ¡send ¡( ¡SOCKET ¡s, ¡ ¡ const ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags ¡); ¡ ¡
22 ¡
#include ¡<winsock.h> ¡ ¡ int ¡sendto ¡( ¡SOCKET ¡s, ¡ ¡ ¡const ¡char ¡* ¡buf, ¡int ¡len, ¡int ¡flags, ¡ ¡ ¡const ¡struct ¡sockaddr ¡* ¡to, ¡int ¡tolen ¡); ¡
s ¡= ¡socket(AF_INET6, ¡SOCK_STREAM, ¡0); ¡ ¡ struct ¡sockaddr_in6 ¡{ ¡ ¡sa_family_t ¡ ¡sin6_family; ¡/* ¡AF_INET6 ¡*/ ¡ ¡in_port_t ¡ ¡sin6_port; ¡/* ¡port ¡number ¡*/ ¡ ¡uint32_t ¡ ¡ ¡sin6_flowinfo; ¡/* ¡IPv6 ¡flow ¡informa.on ¡*/ ¡ ¡struct ¡in6_addr ¡ ¡sin6_addr; ¡/* ¡IPv6 ¡address ¡*/ ¡ ¡ ¡uint32_t ¡ ¡ ¡sin6_scope_id; ¡/* ¡Scope ¡ID ¡(new ¡in ¡2.4) ¡*/ ¡ }; ¡ ¡ struct ¡in6_addr ¡{ ¡ ¡unsigned ¡char ¡s6_addr[16]; ¡/* ¡IPv6 ¡address ¡*/ ¡ }; ¡
24 ¡