Presentation is loading. Please wait.

Presentation is loading. Please wait.

Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket.

Similar presentations


Presentation on theme: "Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket."— Presentation transcript:

1 Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class

2 Socket Programming with Windows OS 1982 - Berkeley Software Distributions introduced sockets as an interface for communication between local processes1982 - 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 UNIX1986 - Berkeley extended the socket interface for use over the TCP/IP with UNIX Today many applications (FTP, Telnet, etc) depend on these interfacesToday many applications (FTP, Telnet, etc) depend on these interfaces 1982 - Berkeley Software Distributions introduced sockets as an interface for communication between local processes1982 - 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 UNIX1986 - Berkeley extended the socket interface for use over the TCP/IP with UNIX Today many applications (FTP, Telnet, etc) depend on these interfacesToday many applications (FTP, Telnet, etc) depend on these interfaces

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

4 Socket Programming with Windows OS Communication protocols connection oriented (Transmission Control Protocol - TCP/IP)connection oriented (Transmission Control Protocol - TCP/IP) connectionless (User Datagram Protocol -UDP and Inter-network Packet Exchange - IPX)connectionless (User Datagram Protocol -UDP and Inter-network Packet Exchange - IPX) Communication protocols connection oriented (Transmission Control Protocol - TCP/IP)connection oriented (Transmission Control Protocol - TCP/IP) connectionless (User Datagram Protocol -UDP and Inter-network Packet Exchange - IPX)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.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.dllWindows 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 Microsoft proposed an industry wide socket standard for Windows OS, called as Windows Socket Interface or WinSock.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.dllWindows 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 WinSock.dll FTP WinSock.dll TCP/IPIPXAppleTalkNetBIOS Remote Access Service (RAS) FTP TCP/IP ModemNetwork Drivers LAN Application Windows Socket, Protocols and Applications Phone Line

7 Differences Between Berkeley and WinSock Socket is an int data type in Berkeley, but a SOCKET data type in WinSockSocket 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 BerkeleySOCKET_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 terminatingApplications must call WSAStartup() before calling any WinSock functions, and should call the WSACleanup() function before terminating Socket is an int data type in Berkeley, but a SOCKET data type in WinSockSocket 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 BerkeleySOCKET_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 terminatingApplications 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 }; 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. There are also some functions to convert from and to native types. The internet address for a socket-based application composed of two pieces 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) 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. There are also some functions to convert from and to native types. The internet address for a socket-based application composed of two pieces 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 Actual Address Family Type Protocol Protocol AF_INET SOCK_DGRAM IPPROTO_UDP UDP AF_INET SOCK_STREAM IPPROTO_TCP TCP SOCKET PASCAL FAR socket (int af, int type, int protocol) Actual 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) u_long FAR * argp) Command Argp State FIONBIO non-zero non-blocking FIONBIO zero blocking Not to block on a socket call use a non-blocking socket. int PASCAL FAR ioctlsocket (SOCKET soc, long command, u_long FAR * argp) u_long FAR * argp) Command Argp State FIONBIO non-zero non-blocking FIONBIO zero blocking

12 Binding a Socket Associating address with a socketAssociating address with a socket The socket end point becomes a named entity visible in the name spaceThe 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) Associating address with a socketAssociating address with a socket The socket end point becomes a named entity visible in the name spaceThe 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 guaranteedData delivery is not guaranteed Data grams are directed towards a specific recipient.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 Data delivery is not guaranteedData delivery is not guaranteed Data grams are directed towards a specific recipient.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 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 You just listen if somebody calls your call-sign, then you answer the call, and vice versa 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 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 Use non-blocking sockets not to block on socket calls UDP is like making a radio communication. You just listen You just listen if somebody calls your call-sign, then you answer the call, and vice versa 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 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 Use non-blocking sockets not to block on socket calls

16 recvfrom/sendto SERVERCLIENT socket() & bind()socket() & bind() recvfrom()sendto() sendto()recvfrom() close()close() SERVERCLIENT socket() & bind()socket() & bind() recvfrom()sendto() sendto()recvfrom() close()close()

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

18 C++ UDPSocket Class Sends data to an internet address and port noSends data to an internet address and port no Internet address, pointer to send buffer, port noInternet address, pointer to send buffer, port no (if not default) must be passed void sendData(IP, char *, port=default_port) Sends data to an internet address and port noSends data to an internet address and port no Internet address, pointer to send buffer, port noInternet 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 portReceives data into a buffer through listening port Double array is used for my applicationsDouble array is used for my applications Parses and puts the received data into bufferParses and puts the received data into buffer bool receiveData (double *, port = default_port) Receives data into a buffer through listening portReceives data into a buffer through listening port Double array is used for my applicationsDouble array is used for my applications Parses and puts the received data into bufferParses and puts the received data into buffer bool receiveData (double *, port = default_port)

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


Download ppt "Outline Socket programming with Windows OSSocket programming with Windows OS C++ UDPSocket classC++ UDPSocket class Socket programming with Windows OSSocket."

Similar presentations


Ads by Google