1 / 22

Sockets

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.

alika
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sockets The Standard Network Programming API

  2. Agenda • Evolution • API Components (Sockets/Winsock) • Protocol Configuration • Tools

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

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

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

  6. 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

  7. Sockets API Components Init Connect Service Disconnect DeInit

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

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

  10. 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

  11. 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.

  12. 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.

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

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

  15. 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

  16. 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.

  17. Protocol Configuration Socket Options I/O Control

  18. 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

  19. 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

  20. Tools Getting the Job Done!!! Making Development Easier

  21. 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

  22. 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

More Related