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

Pertemuan 10 Non Blocking

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

Citation preview

Page 1: Pertemuan 10 Non Blocking

1

Pertemuan 10Non Blocking

Matakuliah : H0483 / Network Programming

Tahun : 2005

Versi : 1.0

Page 2: Pertemuan 10 Non Blocking

2

Learning Outcomes

Pada akhir pertemuan ini, diharapkan mahasiswa

akan mampu :

• menghasilkan program menggunakan protokol dengan Non Blocking I/O

Page 3: Pertemuan 10 Non Blocking

3

Outline Materi

• Introduction

• Nonblocking read & write

• Nonblocking Connect

• Windows Socket

Page 4: Pertemuan 10 Non Blocking

4

<<Introduction>>

• Beda Blocking dan Non Blocking System

Page 5: Pertemuan 10 Non Blocking

5

<< Introduction >>

• Non Blocking

Page 6: Pertemuan 10 Non Blocking

6

<< Introduction >>

• Kapan menggunakan sistem Blocking ?

• Kapan menggunakan sistem Non Blocking ?

Page 7: Pertemuan 10 Non Blocking

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 Client TCP Server

data (request)

data (reply)

three-way handshake

WSAStartup()

WSACleanup()

Page 8: Pertemuan 10 Non Blocking

8

Windows Socket Programming

socket()

recvfrom()

sendto()

closesocket()

socket()

sendto()

recvfrom()

closesocket()

UDP Client UDP Server

data (request)

data (reply)

WSAStartup()

WSACleanup()

Page 9: Pertemuan 10 Non Blocking

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”

Page 10: Pertemuan 10 Non Blocking

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”

Page 11: Pertemuan 10 Non Blocking

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 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);

Page 12: Pertemuan 10 Non Blocking

12

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) );

Page 13: Pertemuan 10 Non Blocking

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};

Page 14: Pertemuan 10 Non Blocking

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};

Page 15: Pertemuan 10 Non Blocking

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

89 BD 60 A8

89BD60A8

increasing memory address

Big-Endian, Network Byte Order

Little-Endian

Page 16: Pertemuan 10 Non Blocking

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);

htonsh: Host byte ordern: Network byte order

s: short (16 bits), port numberl: long (32 bites), IP address

Page 17: Pertemuan 10 Non Blocking

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 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 );

Page 18: Pertemuan 10 Non Blocking

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) );

Page 19: Pertemuan 10 Non Blocking

19

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 );

Page 20: Pertemuan 10 Non Blocking

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 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) );

Page 21: Pertemuan 10 Non Blocking

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; // …… }}

Page 22: Pertemuan 10 Non Blocking

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

Page 23: Pertemuan 10 Non Blocking

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_struct

ures.asp• Diagnostic Services

– http://msdn.microsoft.com/library/en-us/vcmfc98/html/_mfc_diagnostic_services.asp