Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sockets The Standard Network Programming API Agenda Evolution Evolution API Components (Sockets/Winsock) API Components (Sockets/Winsock) Protocol Configuration.

Similar presentations


Presentation on theme: "Sockets The Standard Network Programming API Agenda Evolution Evolution API Components (Sockets/Winsock) API Components (Sockets/Winsock) Protocol Configuration."— Presentation transcript:

1

2 Sockets The Standard Network Programming API

3 Agenda Evolution Evolution API Components (Sockets/Winsock) API Components (Sockets/Winsock) Protocol Configuration Protocol Configuration Tools Tools

4 Evolution Mid 80’s  Berkley Sockets 1991  birds-of-a-feather 1992  Winsock 1.0 1993  Winsock 1.1 Now  2.0+

5 Winsock vs. “Berkley” Sockets Berkley Sockets were the original socket implementation Berkley Sockets were the original socket implementation Berkley Sockets == X-Platform Berkley Sockets == X-Platform Work on any TCP/IP stack Work on any TCP/IP stack Unix / Linux Unix / Linux Consoles Consoles Mac Mac Windows Windows Winsock specific begins with “WSA” Winsock specific begins with “WSA” Winsock specific Winsock specific WSAStartup() WSAStartup() WSAAsynch…() WSAAsynch…() Generic Sockets Generic Sockets socket() socket() recvfrom() recvfrom()

6 Winsock 2.0 QoS (Quality of Service) Reserve bandwidth Multicast Conserves bandwidth by allowing send lists. One packet to multiple recipients. Requires multicast aware hardware Overlapped I/O High performance I/O model !!! NOT PORTABLE !!! IPv6 (making provisions for) Expanded address space IPSec (Data Encryption)

7 File I/O “Stream” Similarities Winsock 2.n added “Unified I/O” Winsock 2.n added “Unified I/O” MSDN mentions deprecation MSDN mentions deprecation Socket Handle == File Handle Socket Handle == File Handle CreateFile(…) CreateFile(…) ReadFile(…) / WriteFile(…) ReadFile(…) / WriteFile(…) CloseFile(…) CloseFile(…) Serial I/O same Serial I/O same Com Handle == File Handle Com Handle == File Handle

8 Sockets API Components InitConnectServiceDisconnectDeInit

9 Initialization Winsock Specific Winsock2.h Winsock2.h Include this header Include this header WS2_32.lib WS2_32.lib Link to this library Link to this library WSAStartup ( in ReqVer, out VerInfo ) WSAStartup ( in ReqVer, out VerInfo ) Can succeed even if is doesn’t support your requested version in it’s entirety. BAH!!! Can succeed even if is doesn’t support your requested version in it’s entirety. BAH!!! View VerInfo to make certain the version. View VerInfo to make certain the version. Should provision for fallback. Should provision for fallback. WSACleanup () WSACleanup () Close all sockets before calling this function. Close all sockets before calling this function. Make sure no pending blocking sockets. Make sure no pending blocking sockets. WSAGetLastError () WSAGetLastError () Call this to determine extended error information on error from any Winsock method. Call this to determine extended error information on error from any Winsock method. Does not work until successful return from WSAStartup (). Does not work until successful return from WSAStartup ().

10 Domain Name Resolution Functions gethostbyname (char *domainName) gethostbyname (char *domainName) Deprecated Deprecated WSAAsyncGetHostByName (char *) WSAAsyncGetHostByName (char *) Deprecated Deprecated getaddrinfo ( char *domainName, …) getaddrinfo ( char *domainName, …) New to Winsock 2 New to Winsock 2 Reverse lookups also available Reverse lookups also available IP  Domain IP  Domain getnameinfo() Winsock 2 getnameinfo() Winsock 2 gethostbyaddr() Winsock 1 (deprecated) gethostbyaddr() Winsock 1 (deprecated)

11 Create Socket Specifying Protocol socket ( in family, in stream, in protocol) socket ( in family, in stream, in protocol) Address Family: Address Family: AF_INET AF_INET Stream: (Connected or Connectionless) Stream: (Connected or Connectionless) SOCK_STREAM SOCK_STREAM SOCK_DGRAM SOCK_DGRAM SOCK_RAW SOCK_RAW Protocol: Protocol: IPPROTO_TCP IPPROTO_TCP IPPROTO_UDP IPPROTO_UDP Returns an unconnected handle to a socket. Returns an unconnected handle to a socket. closesocket (socket) closesocket (socket) Closes connection Closes connection Releases the resources Releases the resources

12 Connect Initiate a connection connect ( socket, addr, addrlen) connect ( socket, addr, addrlen) socket = handle created with call to ‘socket’ socket = handle created with call to ‘socket’ addr = port & IP of place to connect. addr = port & IP of place to connect. must specify the address family in the addr struct. must specify the address family in the addr struct. addrlen = sizeof(addr). addrlen = sizeof(addr). WSAConnect(…) WSAConnect(…) Supports Quality of Service. Supports Quality of Service. Facilitates initial data packet transfer upon connect. Facilitates initial data packet transfer upon connect.

13 Bind Specify “Incoming” Address bind ( in socket, in address, in sizeof(address) ) bind ( in socket, in address, in sizeof(address) ) Socket = socket handle Socket = socket handle Address Address family = AF_INET family = AF_INET sin_port = htons( port # ) sin_port = htons( port # ) sin_addr.s_addr = htonl (ADDR_ANY) sin_addr.s_addr = htonl (ADDR_ANY) Can specify IP if more than one NIC and want specific one. Can specify IP if more than one NIC and want specific one. Fails if IP/port combo in use. Keep track of ports in use in app and try unused one. May have to cycle through a few. Fails if IP/port combo in use. Keep track of ports in use in app and try unused one. May have to cycle through a few. Implicit bind done on connect(). You must bind before an accept(). Implicit bind done on connect(). You must bind before an accept(). Cannot rebind. Cannot rebind.

14 Listen Listen for a connection requests listen ( in socket, in backlog ) listen ( in socket, in backlog ) socket socket Backlog Backlog # of pending connections allowed before additional connections are refused. # of pending connections allowed before additional connections are refused. Call listen() once to set listening state. Call listen() once to set listening state. Returns immediately. Returns immediately. To stop listening on the socket, close it. To stop listening on the socket, close it. closesocket ( socket ). closesocket ( socket ).

15 Accept Accepting a connection newSock accept ( in socket, out addr, in sizeof(addr)) newSock accept ( in socket, out addr, in sizeof(addr)) Socket Socket One you are listening on. One you are listening on. newSocket newSocket New socket created by the accept() method. Use this socket to send() / recv() on the new connection. New socket created by the accept() method. Use this socket to send() / recv() on the new connection. NOTE NOTE This is NOT the socket you passed to listen(). It is a brand spanking new socket. This is NOT the socket you passed to listen(). It is a brand spanking new socket. Addr Addr Address of connection accepted (filled out by the accept function). Address of connection accepted (filled out by the accept function). Steps Steps socket () socket () bind () bind () listen () listen () accept () accept () Loop on accept() until you have all the connections you want, then close the socket passed to listen(). Loop on accept() until you have all the connections you want, then close the socket passed to listen().

16 Send Sending Data send ( socket, buff, bufflen, flags) send ( socket, buff, bufflen, flags) Flags Flags MSG_OOB MSG_OOB Priority message. Can’t use with UDP Priority message. Can’t use with UDP Used with connection based e.g., TCP Used with connection based e.g., TCP sendto (…, addr, sizeof(addr)) sendto (…, addr, sizeof(addr)) Addr = address to send to. Overrides bind() if socket was bound. Addr = address to send to. Overrides bind() if socket was bound. Used with connectionless e.g., UDP Used with connectionless e.g., UDP WSASend (ditto send, overlap struct, OL funk) WSASend (ditto send, overlap struct, OL funk) Use with overlapped I/O Use with overlapped I/O WSASendTo (…) WSASendTo (…) Use with overlapped I/O Use with overlapped I/O

17 Receive Receiving Data recv (sock, buff, bufflen, flags) recv (sock, buff, bufflen, flags) Buff = incoming data Buff = incoming data Bufflen = number of bytes in buffer or bytes to receive (whichever is less) Bufflen = number of bytes in buffer or bytes to receive (whichever is less) Flags Flags MSG_PEEK MSG_PEEK Leave data in system buffers. Inefficient Leave data in system buffers. Inefficient MSG_OOB MSG_OOB Get out-of-band packets Get out-of-band packets recvfrom (…) recvfrom (…) UDP version UDP version WSARecv (…) WSARecv (…) Overlapped version Overlapped version WSARecvFrom (…) WSARecvFrom (…) Overlapped version Overlapped version WSARecvEx (…) WSARecvEx (…) Adds protocol for large messages via. MSG_PARTIAL notification. Normally handled by application with standard socket’s recv(). Adds protocol for large messages via. MSG_PARTIAL notification. Normally handled by application with standard socket’s recv(). Not used with overlapped I/O. Not used with overlapped I/O.

18 Protocol Configuration Socket Options I/O Control

19 Socket Options (get/set)sockopt (sock, sol*, so*, val, sizeof(val)) (get/set)sockopt (sock, sol*, so*, val, sizeof(val)) “sol” socket option level “sol” socket option level SOL_SOCKET SOL_SOCKET Generic Level Generic Level IPPROTO_TCP IPPROTO_TCP TCP/IP specific TCP/IP specific “so” Common Sock Options “so” Common Sock Options SO_BROADCAST SO_BROADCAST SO_KEEPALIVE SO_KEEPALIVE SO_MAX_MSG_SIZE SO_MAX_MSG_SIZE SO_RCVBUF SO_RCVBUF SO_RCVTIMEO SO_RCVTIMEO SO_SNDBUF SO_SNDBUF Much more Much more Multicast group membership management Multicast group membership management

20 I/O Control ioctlsocket (sock, command, arg) ioctlsocket (sock, command, arg) FIONBIO FIONBIO On/Off non-blocking mode (blocking is default) On/Off non-blocking mode (blocking is default) Additional Multicast configuration Additional Multicast configuration Set Keep Alive interval Set Keep Alive interval Flush send buffer Flush send buffer And more… And more… WSAIoctl (…) WSAIoctl (…) Overlapped version Overlapped version

21 Tools Making Development EasierGetting the Job Done!!!

22 Tools Shims Shims DLL that sits between Winsock and IP stack, or IP stack and device driver. DLL that sits between Winsock and IP stack, or IP stack and device driver. Let’s you monkey with everything. Let’s you monkey with everything. Hacker tool Hacker tool Ping Ping App that sends an ICMP message to an IP. App that sends an ICMP message to an IP. Reports round trip time. Reports round trip time. Good for determining average latency and general internet connection state. Good for determining average latency and general internet connection state. Trace Route Trace Route App that sends a packet to an IP. App that sends a packet to an IP. Reports hops and time between hops. Reports hops and time between hops. Good for locating problematic net hardware Good for locating problematic net hardware NetStat NetStat App that reports all packet activity App that reports all packet activity Ipconfig/winifcfg Ipconfig/winifcfg App that reports local IP addresses App that reports local IP addresses Sniffers Sniffers Similar to SHIMS, but often a piece of hardware Similar to SHIMS, but often a piece of hardware Can sit between NIC and cable, or attach to cable anywhere Can sit between NIC and cable, or attach to cable anywhere Expensive equipment, time consuming to learn and use effectively Expensive equipment, time consuming to learn and use effectively

23 Socket Wrappers Platform SDKs Platform SDKs Special O/S specific e.g. xbox achievement, session, … Special O/S specific e.g. xbox achievement, session, … Speech Speech Middle Ware Middle Ware Server Components Server Components Lobby Client Lobby Client Tested Architecture Tested Architecture High Performance (really?) High Performance (really?) Encryption Schemes Encryption Schemes Compression Schemes Compression Schemes Standard/Flexible Message Format Standard/Flexible Message Format X-Platform X-Platform Flexible API Flexible API See references link on class web site for venders See references link on class web site for venders


Download ppt "Sockets The Standard Network Programming API Agenda Evolution Evolution API Components (Sockets/Winsock) API Components (Sockets/Winsock) Protocol Configuration."

Similar presentations


Ads by Google