Sockets
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

Sockets PowerPoint PPT Presentation


  • 196 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Sockets

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Sockets

Sockets

The Standard

Network Programming API


Agenda

Agenda

  • Evolution

  • API Components (Sockets/Winsock)

  • Protocol Configuration

  • Tools


Evolution

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

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()


Winsock 2 0

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)


File i o stream similarities

File I/O “Stream” Similarities

  • Winsock 2.n added “Unified I/O”

    • MSDN mentions deprecation

  • Socket Handle == File Handle

    • CreateFile(…)

    • ReadFile(…) / WriteFile(…)

    • CloseFile(…)

  • Serial I/O same

    • Com Handle == File Handle


Sockets api components

Sockets API Components

Init

Connect

Service

Disconnect

DeInit


Initialization winsock specific

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 ().


  • Domain name resolution functions

    Domain Name ResolutionFunctions

    • gethostbyname (char *domainName)

      • Deprecated

    • WSAAsyncGetHostByName (char *)

      • Deprecated

    • getaddrinfo ( char *domainName, …)

      • New to Winsock 2

    • Reverse lookups also available

      • IPDomain

      • getnameinfo() Winsock 2

      • gethostbyaddr() Winsock 1 (deprecated)


    Create socket specifying protocol

    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


    Connect initiate a connection

    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.


    Bind specify incoming address

    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.


    Listen listen for a connection requests

    ListenListen for a connection requests

    • listen ( in socket, in backlog )

      • socket

      • Backlog

        • # 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 ).


    Accept accepting a connection

    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().


    Send sending data

    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


    Receive receiving data

    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.


    Protocol configuration

    Protocol Configuration

    Socket Options

    I/O Control


    Socket options

    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


    I o control

    I/O Control

    • ioctlsocket (sock, command, arg)

      • FIONBIO

        • On/Off non-blocking mode (blocking is default)

      • Additional Multicast configuration

      • Set Keep Alive interval

      • Flush send buffer

      • And more…

    • WSAIoctl (…)

      • Overlapped version


    Tools

    Tools

    Getting the Job Done!!!

    Making Development Easier


    Tools1

    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


    Socket wrappers

    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


  • Login