Pertemuan 10 Non Blocking

  • View
    38

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Transcript

  • Pertemuan 10Non BlockingMatakuliah: H0483 / Network ProgrammingTahun: 2005Versi: 1.0

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

  • Outline MateriIntroductionNonblocking read & writeNonblocking Connect Windows Socket

  • Beda Blocking dan Non Blocking System

  • >Non Blocking

  • >Kapan menggunakan sistem Blocking ?Kapan menggunakan sistem Non Blocking ?

  • Windows Socket Programming

  • Windows Socket Programming

  • WSAStartup() & WSACleanup()To initialize and release resourceWinsock is a Dynamic Link LibraryInitialization and deallocation are neededWSADATA 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.hLibrary: ws2_32.lib

  • WSAStartup() & WSACleanup()To initialize and release resourceWinsock is a Dynamic Link LibraryInitialization and deallocation are neededWSADATA 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.hLibrary: ws2_32.lib

  • socket() & closesocket()To obtain a socket to perform network I/O

    Socket is a handle that identify the resource in systemSOCKET skt;

    // To obtain a TCP socket// SOCK_STREAM tells function to create a stream socket// IPPROTO_TCP tells function to use TCP stackskt = 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 stackskt = socket(AP_INET, SOCK_DGRAM, IPPROTO_IP);

    /* other network programming codes */

    // To close a socketclosesocket(skt);

  • bind()To assign local protocol address to a socket// Declare a socket address structuresockaddr_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 addressbind( skt, (SOCKADDR*) &addr, sizeof(addr) );

  • Generic SocketAddress StructureA generic structure for addresses

    Independent of protocolsSize of structure1 + 1 + 14 = 16 bytesstruct sockaddr{ uint8_t sa_len;//length of structure sa_family_t sa_family;//AF_XXXX value char sa_data[14];//protocol-specific addr};

  • IPv4 Socket Address StructureIPv4-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};

  • Byte OrderingTwo different ways in representing multi-bytes integere.g. 137.189.96.168 89.BD.60.A8we can represent it as:

    Different machine may use different byte order

  • Byte Ordering FunctionsByte 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);

  • 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 queuedlisten( 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 AcceptAddrAcceptSocket = accept( skt, (struct sockaddr*)&AcceptAddr, &AcceptAddrLen );

  • connect()To connect a server

    Only client needs to call connect()No need to call bind(), listen() & accept() on clients socketCommunication 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) );

  • recv() & recvfrom()To receive data from socket

    // address to store client addresssockaddr_in addr;Int addrLen = sizeof(addr);

    // buffer for saving received datachar buf[1024];

    // No. of bytes actually receivedint 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 );

  • 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 sentchar buf[] = This is a message from other sender;

    // No. of bytes actually sentint 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) );

  • WSAGetLastError()The return codes of the socket APIs only specify whether the operation is successfulNo reason is given for the error in the return codesUse WSAGetLastError() immediately after the error to get the reasonif( connect( skt, (SOCKADDR*) &ServerAddr, sizeof(ServerAddr) ) == SOCKET_ERROR ){ switch(WSAGetLastError()){ case WSAENETUNREACH: printf(Unreachable network address\n); break; // }}

  • Debug ToolsIn case you already started writing the GUI in MFCASSERT(booleanExpression)If booleanExpression evaluate to 0The program terminate and tells you which line invoked ASSERTTRACE()printf in debug windowUse same parameters and format strings as printfPrint strings in the debug window of the IDEASSERT() & TRACE() are available for all CObject in debug mode

  • ReferencesOOPThe 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.htmlMSDNhttp://msdn.microsoft.com/Winsock Error Codeshttp://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.aspWinsock Data Typeshttp://msdn.microsoft.com/library/en-us/winsock/winsock/winsock_structures.aspDiagnostic Serviceshttp://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_diagnostic_services.asp