Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0.

Similar presentations


Presentation on theme: "1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0."— Presentation transcript:

1 1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0

2 2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : menghasilkan program menggunakan protokol dengan Non Blocking I/O

3 3 Outline Materi Introduction Nonblocking read & write Nonblocking Connect Windows Socket

4 4 > Beda Blocking dan Non Blocking System

5 5 > Non Blocking

6 6 > Kapan menggunakan sistem Blocking ? Kapan menggunakan sistem Non Blocking ?

7 7 Windows Socket Programming wait & accept client -> return a socket for client open socket for server initiate socket for server initiate socket for listening receive data from socket send data to socket open socket for client initiate connection receive data send data socket() bind() listen() accept() recv() send() closesocket() socket() connect() send() recv() closesocket() TCP ClientTCP Server data (request) data (reply) three-way handshake WSAStartup() WSACleanup()

8 8 Windows Socket Programming socket() recvfrom() sendto() closesocket() socket() sendto() recvfrom() closesocket() UDP ClientUDP Server data (request) data (reply) WSAStartup() WSACleanup()

9 9 WSAStartup() & WSACleanup() To initialize and release resource Winsock is a Dynamic Link Library –Initialization and deallocation are needed WSADATA wsaData; int iResult; /* Request Winsock 2.2 Details of actually obtained winsock version is in wsaData */ iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); /* Your Network Program Codes are here */ /* Release resources */ iResult = WSACleanup(void); Header File: “winsock2.h” Library: “ws2_32.lib”

10 10 WSAStartup() & WSACleanup() To initialize and release resource Winsock is a Dynamic Link Library –Initialization and deallocation are needed WSADATA wsaData; int iResult; /* Request Winsock 2.2 Details of actually obtained winsock version is in wsaData */ iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); /* Your Network Program Codes are here */ /* Release resources */ iResult = WSACleanup(void); Header File: “winsock2.h” Library: “ws2_32.lib”

11 11 socket() & closesocket() To obtain a socket to perform network I/O Socket is a handle that identify the resource in system SOCKET skt; // To obtain a TCP socket // SOCK_STREAM tells function to create a stream socket // IPPROTO_TCP tells function to use TCP stack skt = socket(AP_INET, SOCK_STREAM, IPPROTO_TCP); // To obtain a UDP socket // SOCK_DGRAM tells function to create a datagram socket // IPPROTO_IP tells function to use IP stack skt = socket(AP_INET, SOCK_DGRAM, IPPROTO_IP); /* other network programming codes */ // To close a socket closesocket(skt);

12 12 bind() To assign local protocol address to a socket // Declare a socket address structure sockaddr_in addr; // fill the information of localhost, i.e. protocol, address & port // inet_addr(char* cp) converts string into proper address for IN_ADDR structure addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr( "127.0.0.1" ); addr.sin_port = htons( 12345 ); // binds the socket with the address bind( skt, (SOCKADDR*) &addr, sizeof(addr) );

13 13 Generic Socket Address Structure A generic structure for addresses Independent of protocols Size of structure –1 + 1 + 14 = 16 bytes struct sockaddr{ uint8_t sa_len;//length of structure sa_family_t sa_family;//AF_XXXX value char sa_data[14];//protocol-specific addr };

14 14 IPv4 Socket Address Structure IPv4-specific address structure Size: 1 + 1 + 2 + 4 + 8 = 16 bytes struct sockaddr_in{ uint8_t sin_len;//length of structure sa_family_t sin_family;//AF_XXXX value in_port_t sin_port;//TCP/UDP port no. struct in_addr sin_addr;//IPv4 address char sin_zero[8];//unused }; struct in_addr{ in_addr_t s_addr;//IPv4 addr, Big-endian };

15 15 Byte Ordering Two different ways in representing multi- bytes integer –e.g. 137.189.96.168  89.BD.60.A8 –we can represent it as: Different machine may use different byte order 89BD60A8 89BD60A8 increasing memory address Big-Endian, Network Byte Order Little-Endian

16 16 Byte Ordering Functions Byte order converting functions Why not using btols (big-to-little), ltobs (little-to-big), …? uint16_t htons(uint16_t host16bitvalue); uint32_t htonl(uint32_t host32bitvalue); uint16_t ntohs(uint16_t net16bitvalue); uint32_t ntohl(uint32_t net32bitvalue); htons h: Host byte order n: Network byte order s: short (16 bits), port number l: long (32 bites), IP address

17 17 listen() & accept() Tell kernel the maximum number of connections should be queued for the socket Communication can then be done on AcceptSocket // set the backlog to 1 // backlog: no. of max. connections can be queued listen( skt, 1 ); SOCKET AcceptSocket; struct sockaddr_in AcceptAddr; Int AcceptAddrLen; // Accept an incoming connection // AcceptAddr stores the address of connecting client // AcceptAddrLen stores the length of AcceptAddr AcceptSocket = accept( skt, (struct sockaddr*)&AcceptAddr, &AcceptAddrLen );

18 18 connect() To connect a server Only client needs to call connect() –No need to call bind(), listen() & accept() on client’s socket Communication can then be done on skt sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr( "127.0.0.1“ ); addr.sin_port = htons( 12345 ); connect( skt, (SOCKADDR*) &ServerAddr, sizeof(ServerAddr) );

19 19 recv() & recvfrom() To receive data from socket // address to store client address sockaddr_in addr; Int addrLen = sizeof(addr); // buffer for saving received data char buf[1024]; // No. of bytes actually received int recved; // receive data from socket, and store them in buf // if skt is a stream socket, the following is equal to // recved = recv( skt, buf, 1024, 0 ); recved = recvfrom( skt, buf, 1024, 0, (SOCKADDR*) &addr, &addrLen );

20 20 send() & sendto() To send data to socket // Variable “addr” is the address of the receiver // addr is obtained from accept(), or manually entered // buffer storing the data to be sent char buf[] = “This is a message from other sender”; // No. of bytes actually sent int sent; // send data from buf to socket, bytes actually sent can be // smaller than request in non-blocking sockets // if skt is a stream socket, the following is equal to // sent = send( skt, buf, 1024, 0 ); sent = sendto( skt, buf, strlen(buf), 0, (SOCKADDR*) &addr, sizeof(addr) );

21 21 WSAGetLastError() The return codes of the socket APIs only specify whether the operation is successful –No reason is given for the error in the return codes Use WSAGetLastError() immediately after the error to get the reason if( connect( skt, (SOCKADDR*) &ServerAddr, sizeof(ServerAddr) ) == SOCKET_ERROR ){ switch(WSAGetLastError()){ case WSAENETUNREACH: printf(“Unreachable network address\n”); break; // …… }

22 22 Debug Tools In case you already started writing the GUI in MFC ASSERT(booleanExpression) –If booleanExpression evaluate to 0 –The program terminate and tells you which line invoked ASSERT TRACE() –“printf” in debug window –Use same parameters and format strings as printf –Print strings in the debug window of the IDE ASSERT() & TRACE() are available for all CObject in debug mode

23 23 References OOP –The Java Tutorial, Object-Oriented Programming Concepts, http://java.sun.com/docs/books/tutorial/java/concepts/ http://java.sun.com/docs/books/tutorial/java/concepts/ –Bruce Eckel, “Thinking in C++ 2 nd Edition,” http://mindview.net/Books/TICPP/ThinkingInCPP2e.html http://mindview.net/Books/TICPP/ThinkingInCPP2e.html MSDN –http://msdn.microsoft.com/http://msdn.microsoft.com/ Winsock Error Codes –http://msdn.microsoft.com/library/en- us/winsock/winsock/windows_sockets_error_codes_2.asphttp://msdn.microsoft.com/library/en- us/winsock/winsock/windows_sockets_error_codes_2.asp Winsock Data Types –http://msdn.microsoft.com/library/en- us/winsock/winsock/winsock_structures.asphttp://msdn.microsoft.com/library/en- us/winsock/winsock/winsock_structures.asp Diagnostic Services –http://msdn.microsoft.com/library/en- us/vcmfc98/html/_mfc_diagnostic_services.asphttp://msdn.microsoft.com/library/en- us/vcmfc98/html/_mfc_diagnostic_services.asp


Download ppt "1 Pertemuan 10 Non Blocking Matakuliah: H0483 / Network Programming Tahun: 2005 Versi: 1.0."

Similar presentations


Ads by Google