Chapter 12 Windows Sockets and Network Programming JMH
- Slides: 41
Chapter 12 Windows Sockets and Network Programming JMH Associates © 2000, All rights reserved 1
OBJECTIVES Upon completion of this chapter, you will be able to: w w w Understand the basic Windows Sockets API Address portability issues between Berkeley and Windows Sockets Use Windows Sockets over TCP/IP for peer-to-peer and client/server applications Consider changes introduced by Windows Sockets 2 See how the Service Registration APOI provides protocol transparency JMH Associates © 2000, All rights reserved 2
WINDOWS SOCKETS (1 of 2) An extension of the Berkeley Sockets API w Into the Windows environment Porting of code already written for Berkeley Sockets w Windows stations easily integrated into TCP/IP networks Exceptions when sockets treated as file descriptors w w Under UNIX Also, extended functions with WSA prefix JMH Associates © 2000, All rights reserved 3
WINDOWS SOCKETS (2 of 2) Functions that manipulate files also work with sockets read() write() ioctl() close() Behave like overlapped Windows file handles Other network communication options w w Named pipes Remote Procedure Calls JMH Associates © 2000, All rights reserved 4
INITIALIZE THE Win. Sock DLL (1 of 2) int WSAStartup( WORD w. Version. Required, LPWSADATA lp. WSAData); JMH Associates © 2000, All rights reserved 5
INITIALIZE THE Win. Sock DLL (2 of 2) w. Version. Required w w w Indicates the highest version of the Win. Sock DLL you need Returns a non-zero value if the DLL cannot support the version you want Low byte specifies the major version High byte specifies the minor version 0 x 0101 version 1. 1 lp. WSAData points to a WSADATA structure that returns information on the configuration of the DLL WSAGet. Last. Error() for error number JMH Associates © 2000, All rights reserved 6
ALLOCATE A SOCKET (1 of 3) Sockets are analogous to handles w A communication channel Call socket() to create (or open) a socket w Actually a HANDLE JMH Associates © 2000, All rights reserved 7
ALLOCATE A SOCKET (2 of 3) w Server: “Listening socket” for client connection requests typedef unsigned int SOCKET; SOCKET socket(int af, int type, int protocol); JMH Associates © 2000, All rights reserved 8
ALLOCATE A SOCKET (3 of 3) w w af denotes the address family PF_INET or AF_INET designates the Internet protocol type specifies connection-oriented (SOCK_STREAM) or datagram communications (SOCK_DGRAM) protocol unnecessary for TCP/IP § Use 0 socket returns INVALID_SOCKET upon failure JMH Associates © 2000, All rights reserved 9
BIND (1 of 3) Next, bind the socket to its address and service endpoint w w w int bind ( SOCKET s, const struct sockaddr *saddr, int namelen); s is an “unbound” SOCKET returned by socket() saddr specifies the address family and protocol-specific information namelen is sizeof(sockaddr) Returns SOCKET_ERROR in case of error JMH Associates © 2000, All rights reserved 10
BIND (2 of 3) SOCKADDR structure struct sockaddr { u_short sa_family; char sa_data[14]; }; typedef struct sockaddr SOCKADDR, *PSPOCKADDR; JMH Associates © 2000, All rights reserved 11
BIND (3 of 3) sa_data is protocol-specific TCP/IP sockaddr_in: struct sockaddr_in { short sin_family; // AF_INET u_short sin_port; struct in_addr sin_addr; //4 -byte IP addr char sin zero [8]; }; typedef struct sockaddr_in SOCKADDR_IN, *PSOCKADDR_IN; JMH Associates © 2000, All rights reserved 12
BIND CONSIDERATIONS List of hosts (mapped to IP addresses) can be found in %System. Root%system 32driversetchosts List of Services(mapped to services) can be found in %System. Root%system 32driversetcservices If you bind to a specific IP address, you can only receive incoming packets over that IP address If you have more than one IP address, bind to hotnl(INADDR_ANY) “host to network long” JMH Associates © 2000, All rights reserved 13
BIND SETUP EXAMPLE (1 of 3) BOOL WINAPI WNet. Get. Host. Address( LPCSTR lpsz. Host, LPCSTR lpsz. Service, LPCSTR lpsz. Proto, LPSOCKADDR lp. Addr) /* Fill in a SOCKADDR using host, protocol, service */ { LPHOSTENT lp. Host; LPSERVENT lp. Serv; SOCKADDR_IN sin; Zero. Memory(&sin, sizeof(sin)); JMH Associates © 2000, All rights reserved 14
BIND SETUP EXAMPLE (2 of 3) sin_family = PF_INET; sin_addr. s_addr = htonl(INADDR_ANY); if(lpsz. Host != NULL) { lp. Host = gethostbyname(lpsz. Host); if(lp. Host != NULL) { Copy. Memory(&sin. sin_addr, lp. Host->h_addr_list[0], lp. Host->h_length); } } lp. Serv = getservbyname(lpsz. Service, lpsz. Proto); JMH Associates © 2000, All rights reserved 15
BIND SETUP EXAMPLE (3 of 3) if(lp. Serv != NULL) { sin_port = lp. Serv->s_port; Zero. Memory(sin. sin_zero, sizeof(sin. sin_zero)); Copy. Memory(lp. Addr, &sin, sizeof(SOCKADDR)); return TRUE; /* lp. Addr is now ready for bind() */ } return FALSE; } The address returned by WNet. Get. Host. Address() can be passed directly to the bind() function JMH Associates © 2000, All rights reserved 16
listen () listen() makes server available for client connection w Socket goes from “bound” to “listening” state int listen(SOCKET s, int n. Queue. Size); n. Queue. Size indicates the number of connection requests you are willing to have queued at the socket w Up to SOMAXCON (5 for 1. 1, “unlimited” in 2. 0) JMH Associates © 2000, All rights reserved 17
accept () (1 of 2) w w The call to listen() places the socket into the listening state The server application calls accept() § w w Returning a “connected socket” accept() blocks until a client request for a connection arrives accept() return value gives the server a new socket for exchanging data JMH Associates © 2000, All rights reserved 18
accept () (2 of 2) SOCKET accept( SOCKET s, /* Listening socket */ LPSOCKADDR lp. Addr, /* Find client details here */ LPINT lpn. Addr. Len /* Length of the returned structure */ ); JMH Associates © 2000, All rights reserved 19
THE CLIENT SIDE (1 of 2) A client station wishing to connect to a server must also create a socket by calling socket() If it is not bound by an address, Windows Sockets will assign it a unique address to use for the duration of the connection JMH Associates © 2000, All rights reserved 20
THE CLIENT SIDE (2 of 2) int connect( SOCKET s, LPSOCKADDR lp. Name, int n. Name. Len); lp. Name points to a SOCKADDR structure designating the server machine name and port address JMH Associates © 2000, All rights reserved 21
EXCHANGE OF DATA (1 of 2) w w Partner stations exchange data using send() and recv() have identical arguments: int send ( SOCKET s, LPSTR lp. Buffer, int n. Buffer. Len, int n. Flags); JMH Associates © 2000, All rights reserved int recv ( SOCKET s, LPSTR lp. Buffer, int n. Buffer. Len, int n. Flags); 22
EXCHANGE OF DATA (2 of 2) w w n. Flags == MSG_OOB indicates urgency § OOB for out-of-band MSG_PEEK can be used to peek at the data without removing it These are standard Berkeley Sockets calls § But read() and write() are more common under UNIX Not atomic or message oriented § § Loop until full message is received Or sent, although incomplete send() is rare JMH Associates © 2000, All rights reserved 23
DATAGRAM SERVICE (1 of 2) Stations send and receive data using sendto() and recvfrom() Take the same arguments as send() and recv(), but add two to designate the partner station int sendto ( SOCKET s, LPSTR lp. Buffer, int n. Buffer. Len, int n. Flags, LPSOCKADDR lp. Addr, int n. Addr. Len); JMH Associates © 2000, All rights reserved int recvfrom ( SOCKET s, LPSTR lp. Buffer, int n. Buffer. Len, int n. Flags, LPSOCKADDR lp. Addr, LPINT pn. Addr. Len); 24
DATAGRAM SERVICE (2 of 2) With datagrams, the n. Flags argument cannot be MSG_OOB You cannot send or receive urgent data JMH Associates © 2000, All rights reserved 25
CLOSING A SOCKET w w In standard Berkeley Sockets, call close() In Windows Sockets call void closesocket(SOCKET s); w For connection-oriented communications, the server side of the exchange closes the socket created by accept() § Not the one returned by socket() w Only when the server goes out of service should it close the listening socket Finally, call void WSACleanup(void); § To break your connection to WSOCK 32. DLL w § This function is also non-portable JMH Associates © 2000, All rights reserved 26
SOCKETS SUMMARY (1 of 2) Connection Oriented Service Server socket() bind() listen() accept() Client socket() connect() send()/recv() sendto()/recvfrom() closesocket() JMH Associates © 2000, All rights reserved 27
SOCKETS SUMMARY (2 of 2) Connectionless (Datagram) Server socket() listen() Client socket() connect() send()/recv() sendto()/recvfrom() closesocket() JMH Associates © 2000, All rights reserved 28
BERKELEY vs WINDOWS SOCKETS Standard Berkeley Sockets calls will port to Windows Sockets, with these exceptions: w w You must call WSAStartup() to initialize the sockets DLL Though you can use UNIX-style read() and _write() to receive and send data, you must first convert the Windows socket to an operating system handle by calling _open_osfhandle() § w w In Sockets 1. 1, you also have to call setsockopt() to force sockets to be opened as non-overlapped handles You must use closesocket() (which is not portable), rather than close() (which is), to close a socket You must call WSACleanup() to shut down the DLL JMH Associates © 2000, All rights reserved 29
OVERLAPPED I/O WITH WINDOWS SOCKETS (1 of 2) In Win. Sock 1. 1, Windows opens sockets as overlapped file handles You can pass a socket to Read. File() or Write. File() without modification w Wait for an operation to complete by tying it to an event handle, and call Wait. For. Single. Object() Wait. For. Multiple. Objects() or Msg. Wait. For. Multiple. Objects JMH Associates © 2000, All rights reserved 30
OVERLAPPED I/O WITH WINDOWS SOCKETS (2 of 2) w w Use I/O completion routines with Read. File. Ex() / Write. File. Ex() and the extended wait functions Wait. For. Single. Object. Ex(), Wait. For. Multiple. Objects. Ex(), and Sleep. Ex() Use an I/O completion port with Read. File() / Write. File(), Create. Io. Completion. Port(), and Get. Queued. Completion. Status() JMH Associates © 2000, All rights reserved 31
WINDOWS SOCKETS 2 (1 of 2) Windows Sockets 2, available in NT 4. 0, adds several areas of functionality Note: Use 1. 1 for interoperability reasons w w Standardized support for overlapped I/O Scatter/gather I/O § Sending and receiving from non-contiguous buffers in memory JMH Associates © 2000, All rights reserved 32
WINDOWS SOCKETS 2 (2 of 2) w The ability to request quality of service from the Sockets support layer § w The ability to organize sockets into groups § § § w w Speed and reliability of transmission The quality of service of a socket group can be configured It does not have to be done on a socket-by-socket basis The sockets belonging to a group can be prioritized Piggybacking of data onto connection requests Multipoint connections (conference calls) JMH Associates © 2000, All rights reserved 33
STANDARDIZATION OF OVERLAPPED I/O (1 of 2) w w The most important addition to Windows Sockets 2 is the standardization of overlapped I/O Sockets are no longer created automatically as overlapped file handles socket() will create a non-overlapped handle To create an overlapped socket, call WSASocket() and explicitly ask for one JMH Associates © 2000, All rights reserved 34
STANDARDIZATION OF OVERLAPPED I/O (2 of 2) SOCKET WSAAPI WSASocket( int i. Address. Family, int i. Socket. Type, int i. Protocol, LPWSAPROTOCOL_INFO lp. Protocol. Info, GROUP g, DWORD dw. Flags); JMH Associates © 2000, All rights reserved 35
SCATTER/GATHER I/O (1 of 4) Win. Sock 2 adds the ability to collect and distribute data for transmission or reception from non-contiguous memory buffers typedef struct WSABUF { u_long len; char *buf; } WSABUF, *LPWSABUF; JMH Associates © 2000, All rights reserved 36
SCATTER/GATHER I/O (2 of 4) w w w buf points to the data len is the number of bytes in this particular buffer WSASend(), WSARecv(), WSASend. To(), and WSARecv. From() take an array of WSABUF structures JMH Associates © 2000, All rights reserved 37
SCATTER/GATHER I/O (3 of 4) int WSARecv( SOCKET s, LPWSABUF lp. Recv. Buffers, DWORD dw. Buffers, LPDWORD lpdw. Bytes. Received, LPDWORD lpdw. Flags, LPWSAOVERLAPPED lp. Overlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lp. Completion. Routine); JMH Associates © 2000, All rights reserved 38
SCATTER/GATHER I/O (4 of 4) w w w lp. Recv. Buffers points to an array of WSABUF structures dw. Buffers is the number of structures in the array When data arrives, the Win. Sock 2 drivers disperse it among the buffers passed JMH Associates © 2000, All rights reserved 39
LAB B– 1 (1 of 2) Use Windows Sockets to connect clients (the client program) to a “command line server” w w The clients take a command line and send it to a known server for execution The server returns the results to the client over the socket The solution consists of two components: w w Client. c § Run Client. exe in its own window Server. c JMH Associates © 2000, All rights reserved 40
LAB B– 1 (2 of 2) w w Server. c is built as Server. exe and runs in its own window or is controlled with the job management commands from Module 5 Place the commands to be executed in the same directory as Server. exe The header file, Clnt. Srvr. h, contains definitions used in the various programs, such as the message format WSOCK 32. LIB must be included when linking Challenge: Extend server to set up service name in services file and client finds it with WNet. Get. Host. Address JMH Associates © 2000, All rights reserved 41
- Stevens unix network programming
- Joint medical holdings
- Jmh weather
- Jmh associates
- Sockets and threads
- Censer
- Tcp ip sockets in c
- Tcp/ip sockets in java
- Function of steering wheel
- Sockets direct protocol
- What is berkeley sockets
- Reliable datagram sockets
- What are raw sockets
- D3azeg_clwi -site:youtube.com
- Cleanroom 380v sockets
- Socket programing
- Tanenbaum linux
- Secure sockets
- 도나도나 tcp
- Reliable datagram sockets
- Berkeley sockets
- Groups in the windows movie maker ribbon
- Windows media player 9
- Alternatief voor windows live mail
- Windows driver kit windows 7
- Microsoft windows movie maker windows 7
- Windows media player skins windows 10
- Windows identity foundation windows 10
- Windws update
- Winxp virtual machine
- Windows mobile center windows 10
- Windows movie maker 2012
- Ipseq
- Xp mode windows 7
- Mouse events in windows programming
- Qm for windows linear programming
- _tmain
- Introduction to windows programming
- Windows system programming
- Visual studio
- System programming vs application programming
- Perbedaan linear programming dan integer programming