1 / 23

Pertemuan 10 Non Blocking

Pertemuan 10 Non Blocking. Matakuliah : H0483 / Network Programming Tahun : 2005 Versi : 1.0. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : menghasilkan program menggunakan protokol dengan Non Blocking I/O. Outline Materi. Introduction

Download Presentation

Pertemuan 10 Non Blocking

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. Pertemuan 10Non Blocking Matakuliah : H0483 / Network Programming Tahun : 2005 Versi : 1.0

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

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

  4. <<Introduction>> • Beda Blocking dan Non Blocking System

  5. << Introduction >> • Non Blocking

  6. << Introduction >> • Kapan menggunakan sistem Blocking ? • Kapan menggunakan sistem Non Blocking ?

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

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

  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. 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. 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. 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. Generic SocketAddress 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. 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. Big-Endian, Network Byte Order Little-Endian 89 BD 60 A8 increasing memory address A8 60 BD 89 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

  16. htons s: short (16 bits), port number l: long (32 bites), IP address h: Host byte order n: Network byte order 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);

  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. 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. 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. 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. 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. 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. References • OOP • The Java Tutorial, Object-Oriented Programming Concepts, http://java.sun.com/docs/books/tutorial/java/concepts/ • Bruce Eckel , “Thinking in C++ 2nd Edition,” http://mindview.net/Books/TICPP/ThinkingInCPP2e.html • MSDN • http://msdn.microsoft.com/ • Winsock Error Codes • http://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.asp • Diagnostic Services • http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_diagnostic_services.asp

More Related