Sockets

22
Sockets The Standard Network Programming API

description

Sockets. The Standard Network Programming API. Agenda. Evolution API Components (Sockets/Winsock) Protocol Configuration Tools. Evolution. Mid 80’s  Berkley Sockets 1991  birds-of-a-feather 1992  Winsock 1.0 1993  Winsock 1.1 Now  2.0+. Winsock vs. “Berkley” Sockets. - PowerPoint PPT Presentation

Transcript of Sockets

Page 1: Sockets

Sockets

The StandardNetwork Programming API

Page 2: Sockets

Agenda

EvolutionAPI Components (Sockets/Winsock)Protocol ConfigurationTools

Page 3: Sockets

Evolution

Mid 80’s Berkley Sockets1991 birds-of-a-feather

1992 Winsock 1.01993 Winsock 1.1

Now 2.0+

Page 4: Sockets

Winsock vs. “Berkley” Sockets Berkley Sockets were the original socket implementation Berkley Sockets == X-Platform

Work on any TCP/IP stack Unix / Linux Consoles Mac Windows

Winsock specific begins with “WSA” Winsock specific

WSAStartup() WSAAsynch…()

Generic Sockets socket() recvfrom()

Page 5: Sockets

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)

Page 6: Sockets

File I/O “Stream” SimilaritiesWinsock 2.n added “Unified I/O”

MSDN mentions deprecationSocket Handle == File Handle

CreateFile(…)ReadFile(…) / WriteFile(…)CloseFile(…)

Serial I/O sameCom Handle == File Handle

Page 7: Sockets

Sockets API Components

InitConnectService

DisconnectDeInit

Page 8: Sockets

InitializationWinsock Specific

Winsock2.h Include this header

WS2_32.lib Link to this library

WSAStartup ( in ReqVer, out VerInfo ) Can succeed even if is doesn’t support your requested version in it’s entirety.

BAH!!! View VerInfo to make certain the version. Should provision for fallback.

WSACleanup () Close all sockets before calling this function. Make sure no pending blocking sockets.

WSAGetLastError () Call this to determine extended error information on error from any Winsock

method. Does not work until successful return from WSAStartup ().

Page 9: Sockets

Domain Name ResolutionFunctions

gethostbyname (char *domainName) Deprecated

WSAAsyncGetHostByName (char *) Deprecated

getaddrinfo ( char *domainName, …) New to Winsock 2

Reverse lookups also available IPDomain getnameinfo() Winsock 2 gethostbyaddr() Winsock 1 (deprecated)

Page 10: Sockets

Create SocketSpecifying Protocol

socket (in family, in stream, in protocol) Address Family:

AF_INET Stream: (Connected or Connectionless)

SOCK_STREAM SOCK_DGRAM SOCK_RAW

Protocol: IPPROTO_TCP IPPROTO_UDP

Returns an unconnected handle to a socket. closesocket (socket)

Closes connection Releases the resources

Page 11: Sockets

ConnectInitiate a connection

connect ( socket, addr, addrlen)socket = handle created with call to ‘socket’addr = port & IP of place to connect.

must specify the address family in the addr struct.addrlen = sizeof(addr).

WSAConnect(…)Supports Quality of Service .Facilitates initial data packet transfer upon

connect.

Page 12: Sockets

BindSpecify “Incoming” Address

bind ( in socket, in address, in sizeof(address) ) Socket = socket handle Address

family = AF_INET sin_port = htons( port # ) sin_addr.s_addr = htonl (ADDR_ANY)

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.

Implicit bind done on connect(). You must bind before an accept().

Cannot rebind.

Page 13: Sockets

ListenListen for a connection requests

listen ( in socket, in backlog )socketBacklog

# of pending connections allowed before additional connections are refused.

Call listen() once to set listening state. Returns immediately.

To stop listening on the socket, close it.closesocket ( socket ).

Page 14: Sockets

AcceptAccepting a connection

newSock accept ( in socket, out addr, in sizeof(addr)) Socket

One you are listening on. newSocket

New socket created by the accept() method. Use this socket to send() / recv() on the new connection.

NOTE This is NOT the socket you passed to listen(). It is a brand spanking new socket.

Addr Address of connection accepted (filled out by the accept function).

Steps socket () bind () listen () accept ()

Loop on accept() until you have all the connections you want, then close the socket passed to listen().

Page 15: Sockets

SendSending Data

send ( socket, buff, bufflen, flags) Flags

MSG_OOB Priority message. Can’t use with UDP

Used with connection based e.g., TCP sendto (…, addr, sizeof(addr))

Addr = address to send to. Overrides bind() if socket was bound.

Used with connectionless e.g., UDP WSASend (ditto send, overlap struct, OL funk)

Use with overlapped I/O WSASendTo (…)

Use with overlapped I/O

Page 16: Sockets

ReceiveReceiving Data

recv (sock, buff, bufflen, flags) Buff = incoming data Bufflen = number of bytes in buffer or bytes to receive (whichever is less) Flags

MSG_PEEK Leave data in system buffers. Inefficient

MSG_OOB Get out-of-band packets

recvfrom (…) UDP version

WSARecv (…) Overlapped version

WSARecvFrom (…) Overlapped version

WSARecvEx (…) Adds protocol for large messages via. MSG_PARTIAL notification. Normally

handled by application with standard socket’s recv(). Not used with overlapped I/O.

Page 17: Sockets

Protocol Configuration

Socket OptionsI/O Control

Page 18: Sockets

Socket Options (get/set)sockopt (sock, sol*, so*, val, sizeof(val))

“sol” socket option level SOL_SOCKET

Generic Level IPPROTO_TCP

TCP/IP specific “so” Common Sock Options

SO_BROADCAST SO_KEEPALIVE SO_MAX_MSG_SIZE SO_RCVBUF SO_RCVTIMEO SO_SNDBUF

Much more Multicast group membership management

Page 19: Sockets

I/O Control ioctlsocket (sock, command, arg)

FIONBIOOn/Off non-blocking mode (blocking is default)

Additional Multicast configurationSet Keep Alive intervalFlush send bufferAnd more…

WSAIoctl (…)Overlapped version

Page 20: Sockets

Tools

Making Development EasierGetting the Job Done!!!

Page 21: Sockets

Tools Shims

DLL that sits between Winsock and IP stack, or IP stack and device driver. Let’s you monkey with everything. Hacker tool

Ping App that sends an ICMP message to an IP. Reports round trip time. Good for determining average latency and general internet connection state.

Trace Route App that sends a packet to an IP. Reports hops and time between hops. Good for locating problematic net hardware

NetStat App that reports all packet activity

Ipconfig/winifcfg App that reports local IP addresses

Sniffers Similar to SHIMS, but often a piece of hardware Can sit between NIC and cable, or attach to cable anywhere Expensive equipment, time consuming to learn and use effectively

Page 22: Sockets

Socket Wrappers Platform SDKs

Special O/S specific e.g. xbox achievement, session, … Speech

Middle Ware Server Components Lobby Client Tested Architecture

High Performance (really?) Encryption Schemes Compression Schemes Standard/Flexible Message Format X-Platform Flexible API

See references link on class web site for venders