1 / 20

Outline

Outline. Socket programming with Windows OS C++ UDPSocket class. Socket Programming with Windows OS. 1982 - Berkeley Software Distributions introduced sockets as an interface for communication between local processes

tatiana
Download Presentation

Outline

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. Outline • Socket programming with Windows OS • C++ UDPSocket class

  2. Socket Programming with Windows OS • 1982 - Berkeley Software Distributions introduced sockets as an interface for communication between local processes • 1986 - Berkeley extended the socket interface for use over the TCP/IP with UNIX • Today many applications (FTP, Telnet, etc) depend on these interfaces

  3. Socket Programming with Windows OS • Socket • a programming interface • a logical, file handle like, construct that an application uses for communication (everything in Unix is a file) • not restricted to TCP/IP

  4. Socket Programming with Windows OS • Communication protocols • connection oriented (Transmission Control Protocol - TCP/IP) • connectionless (User Datagram Protocol -UDP and Inter-network Packet Exchange - IPX)

  5. Socket Programming with Windows OS • Microsoft proposed an industry wide socket standard for Windows OS, called as Windows Socket Interface or WinSock. • Windows socket based applications use the WinSock interface to access the default Windows WinSock implementation, WinSock.dll or an alternative implementation such as, the FTP WinSock.dll

  6. Windows Socket, Protocols and Applications Application FTP WinSock.dll WinSock.dll Remote Access Service (RAS) TCP/IP IPX AppleTalk NetBIOS FTP TCP/IP Modem Network Drivers Phone Line LAN

  7. Differences BetweenBerkeley and WinSock • Socket is an int data type in Berkeley, but a SOCKET data type in WinSock • SOCKET_ERROR is produced by all WinSock functions, but negative one (-1) in Berkeley • Applications must call WSAStartup() before calling any WinSock functions, and should call the WSACleanup() function before terminating

  8. Structures • struct sockaddr { // socket address information • u_short sa_family; // address family • char sa_data[14]; // up to 14 bytes of direct address • }; • struct in_addr { // internet address • u_long s_addr; // socket address • };

  9. Structures • struct sockaddr_in { // socket address internet • short sin_family; // 2 bytes • u_short sin_port; // 2 bytes port number • struct in_addr sin_addr; // 4 bytes net ID, host ID • char sin_zero[8]; // 8 bytes unused • }; • There are also some functions to convert from and to native types. • The internet address for a socket-based application composed of two pieces • - two-byte port number (sin_port) > 1023 • - four-byte network address (sin_addr)

  10. Creating a Socket • SOCKET PASCAL FAR socket (int af, int type, int protocol) • Actual • Address Family Type Protocol Protocol • AF_INET SOCK_DGRAM IPPROTO_UDP UDP • AF_INET SOCK_STREAM IPPROTO_TCP TCP

  11. A Non-blocking Socket • Not to block on a socket call use a non-blocking socket. • int PASCAL FAR ioctlsocket (SOCKET soc, long command, • u_long FAR * argp) • Command Argp State • FIONBIO non-zero non-blocking • FIONBIO zero blocking

  12. Binding a Socket • Associating address with a socket • The socket end point becomes a named entity visible in the name space • int PASCAL FAR bind (SOCKET soc, const struct sockaddr FAR * address, int addrlen)

  13. Sending UDP • Data delivery is not guaranteed • Data grams are directed towards a specific recipient. • int PASCAL FAR sendto (SOCKET soc, // my socket • const char FAR * buffer, // sending buffer • int len, // data length • int flags, // 0 • const struct sockaddr FAR * to, // recipient • int tolen) // recipient’s address length

  14. Receiving UDP • int PASCAL FAR recvfrom (SOCKET soc, // my socket • char FAR * buffer, // receiving buffer • int len, // data length • int flags, // 0 • struct sockaddr FAR * from, // this is me • int FAR * fromlen) // my address length

  15. Communicating UDP • UDP is like making a radio communication. • You just listen • if somebody calls your call-sign, then you answer the call, and vice versa • The difference is callings are made only once, if you miss a call, you miss the message • Use non-blocking sockets not to block on socket calls

  16. recvfrom/sendto • SERVER CLIENT • socket() & bind() socket() & bind() • recvfrom() sendto() • sendto() recvfrom() • close() close()

  17. C++ UDPSocket Class • Constructor • Creates non-blocking a socket with a default or passed port number • Initializes the Winsock.dll • Finds the internet address of the host computer • Binds the socket to this address • Destructor takes care of the closing operations

  18. C++ UDPSocket Class • Sends data to an internet address and port no • Internet address, pointer to send buffer, port no • (if not default) must be passed • void sendData(IP, char *, port=default_port)

  19. C++ UDPSocket Class • Receives data into a buffer through listening port • Double array is used for my applications • Parses and puts the received data into buffer • bool receiveData (double *, port = default_port)

  20. References • Network Programming in Windows NT, Alok K. Sinha • www.aw.com/cseng (sample code) • www.ecst.csuchico.edu/~beej/guide/net/

More Related