Six (6) foils per page - Klassen Enterprises Home Page

dargspurΔίκτυα και Επικοινωνίες

27 Οκτ 2013 (πριν από 3 χρόνια και 9 μήνες)

95 εμφανίσεις

Chris Klassen
Win32 Systems Programming 1
Winsock Basics
Windows Internet
Programming
Lecture 1
Winsock Basics
z OSI Model (The Big Picture)
z TCP/IP & Socket intro
z Winsock intro
z Winsock usage
z Winsock client/server
z Basic Socket I/O
Protocol Stack
Classroom
English
Spoken
OSI Model
Application
Presentation
Transport
Network
Data link
Physical
Protocol
API
FTP
SSL
TCP
IP
Ethernet
10BaseT
Session
OSI Model
Application
Presentation
Transport
Network
Data link
Physical
Session
Data
Data
AH+Data
PH+AH+Data
SH+PH+AH+Data
TH+SH+PH+AH+Data
NH+TH+SH+PH+AH+Data
Bits
AH
PH
SH
TH
NH
DH
DT
FTP
TCP
IP
Ethernet
10BaseT
Winsock in OSI Model
WinSock
Transport
Network
TCP
IP
ApplicationFTP
Chris Klassen
Win32 Systems Programming 2
What is TCP/IP?
z Transport Control Protocol
y Transport-layer protocol
y Handles End-to-end connection on internet
y Carries a higher level protocol (e.g. FTP)
y Also UDP for connection-less communication
z Internet Protocol
y Network (Machine to machine)
communication layer for the internet
y Addressing scheme for the internet
UDP and TCP
z TCP (Transport Control protocol)
y Connection-oriented (Stream service)
x Sets up a “connection” known by both computers
x Every transmission is acknowledged
z UDP (User Datagram protocol)
y Connectionless (Datagram service)
x No connection setup
x Sends datagrams containing destination
x Recipient does not (necessarily) “ack” receipt
What is a socket?
z Berkeley (BSD) Unix “socket” API
y ArpaNet Protocol - DOD VAX’s
y Handles TCP & UDP connections
y socket: The “end point” of a connection
How sockets work
z Each machine can have many sockets
y port: Number assigned to each active socket
x Range 0x0000 (0) - 0xFFFF (65535)
y Machine must have a free port to use socket
y Port numbers are reserved for specific apps
z Each machine is assigned an IP address
x Called an internet address
x More on IP addresses later
z Full address = IP Address and port#
What is Winsock?
z Winsock is Microsoft’s “vendor
independent” API
z Winsock is composed of:
y BSD interface (same functions, etc.)
y WSA - Win Socket Application interfaces
z Trojan Horse?
z Winsock II is now out
Winsock 1.0, 1.1, 2.0
z Winsock 1.0 - Microsoft’s first attempt
z Winsock 1.1 - Pretty much the standard
y 16 bit
y 32 bit
z Winsock 2.0 - Current new standard
y 32 bit
y Increasing in popularity
Chris Klassen
Win32 Systems Programming 3
Setup Winsock
z Network setup
y Add TCP/IP to network options
y Configure an IP address or DHCP
z API setup
y Part of MSVC 6.0
y winsock.lib/winsock.dll - Winsock 1.1, 16 bit
y wsock32.lib/wsock32.dll - Winsock 1.1, 32 bit
y ws2_32.lib/ws2_32.dll - Winsock 2.0, 32 bit
Winsock header files
z Winsock.h - Chief Winsock header
z Legacy (BSD) Headers
y Netdb.h Arpa.h
y Inet.h Sys.h
y Time.h Socket.h
z WSIPX.h - Required for IPX sockets
Initializing Winsock
z WSA = WinSocket Application
z WSAStartup
int WSAStartup(
[in] WORD wVersionRequested,
[out] LPWSADATA pWSAData );
y Request version of Winsock: 1.1 / 2.0, etc.
y pWSAData holds version + other data
0 1 0 1
WSAData
struct WSAData {
WORD wVersion;-Version used
WORD wHighVersion;-Highest possible
char systemDescription[…]; -Vendor
char szSystemStatus[…];
unsigned short iMaxSockets; -Max sockets
unsigned short iMaxUpdDg; -UDP msg size
char * FAR lpVendorInfo;-Vendor data
}
WSACleanup
int WSACleanup( void );
y Call when finished with the API
WSAGetLastError
z Returns last error code
z Like Win32 GetLastError()
z Errors in wsock32.h
y WSABASEERR - #defined to 10000
x Start of Winsock Errors
y Berkeley error codes are not used
x Some are defined in errno.h
x Winsock doesn’t use them
Chris Klassen
Win32 Systems Programming 4
Winsock Client/Server Model
Server App
Network
Client App
Winsock
Winsock
Winsock Server calls
z socket Create a socket object
z bind Bind socket to an IP address
z listen Prepare to receive clients
z accept Wait for client
y Returns another socket object
z send & recv Communicate with client
z closesocket Terminate communication
Winsock Client calls
z socket Create a socket object
z connect Connect to a server
y Server must be “accept”ing calls
y connect does NOT create another socket
z send & recv I/O with server
y Higher protocol determines who sends first
z closesocket Terminate connection
socket [server usage]
SOCKET PASCAL FAR socket
( int af,// AF_INET, AF_IPX
int type, // SOCK_STREAM,
SOCK_DGRAM
int protocol); // usually 0
z Creates SOCKET object
z Returns file handle or INVALID_SOCKET
SOCKET type
typedef u_int SOCKET;
z “File” handle to socket
z Can be used in functions like a file handle
y Use in ReadFile/WriteFile
y For Overlapped I/O
Address spaces, intro
struct sockaddr {
u_short sa_family; /* address family */
char sa_data[14]; /* up to 14 bytes of direct address */};
z Address family determines address structure
y AF_INET - Internet (IP) addresses
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr; // ulong
char sin_zero[8]; }
y AF_IPX - IPX addressing (wsipx.h)
struct sockaddr_ipx { short sa_family;
char sa_netnum[4];
char sa_nodenum[6];
unsigned short sa_socket;}
Chris Klassen
Win32 Systems Programming 5
bind [server only]
int PASCAL FAR bind
( SOCKET s,// open socket
const struct sockaddr FAR *addr,
int namelen);// addr size
z Bind address to socket
y Really only needs “port” number
z Returns SOCKET_ERROR on failure
y Call WSAGetLastError() for details
listen [server only]
int PASCAL FAR listen
(SOCKET s, int backlog);
z Puts socket in a listening state
z backlog is number of pending connections
y pending connection = clients waiting connect
y SOMAXCONN means max provider supported
x WSAECONNREFUSED - returned to client > max
y Call again to alter backlog
accept [server only]
SOCKET PASCAL FAR accept
( SOCKET s,
struct sockaddr FAR *addr,
int FAR *addrlen);
z Waits indefinitely for a client to connect
z Creates a new SOCKET
z addr holds client’s address data
z INVALID_SOCKET if something wrong
socket [client usage]
SOCKET PASCAL FAR socket
( int af,// AF_INET, AF_IPC
int type, // SOCK_STREAM,
SOCK_DGRAM
int protocol); // usually 0
z Creates SOCKET object
z Returns file handle or INVALID_SOCKET
connect [client only]
int PASCAL FAR connect
(SOCKET s, // open socket
const struct sockaddr FAR *name,
int namelen);
z Connect client socket to a server socket
z socketaddr contains address of server
z Returns SOCKET_ERROR on failure
Buffering
z Both client and server have buffers
y Input buffer
y Output buffer
z Data goes into destination input buffer
y When input buffer full
x data goes into source output buffer
y When output buffer full
x data is blocked until some reading occurs
Chris Klassen
Win32 Systems Programming 6
Basic I/O Model
Input Buffer
Input buffer
Output Buffer
Output buffer
Client
App
Server
App
Winsock
Winsock
recv (client or server)
int PASCAL FAR recv (
SOCKET s, // open socket
char FAR * buf, // data in
int len, // size of buf
int flags);
z Read data from socket input buffer
z Flags
y MSG_PEEK - Don’t remove from buffer
y MSG_OOB - Out of band - discuss later
send (client or server)
int PASCAL FAR send (
SOCKET s, // open socket
char FAR * buf, // data out
int len, // size of buf
int flags);
z Write data to socket output buffer
z Flags
y MSG_DONTROUTE - Provider can ignore
y MSG_OOB - Out of band - discuss later
Example 1 - Winsock1
z Exchange data (chat) with Winsock
z Winsock1.exe -c is a Winsock client
y Uses dialogs to get address info
y doclient() - socket, connect
z Winsock1.exe -s is a Winsock server
y Uses dialogs to get address info
y doserver() - socket, bind, listen, accept
z After connection, both are the same
y Call ReadThreadProc() in thread
x Calls recv
x Display results
y Call WriteThreadProc() in thread
x Create a dialog to gather message
x Calls send with result
Example 1 - Winsock1