pertemuan 10 non blocking
Download
Skip this Video
Download Presentation
Pertemuan 10 Non Blocking

Loading in 2 Seconds...

play fullscreen
1 / 23

Pertemuan 10 Non Blocking - PowerPoint PPT Presentation


  • 131 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Pertemuan 10 Non Blocking' - chastity-walls


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
pertemuan 10 non blocking

Pertemuan 10Non Blocking

Matakuliah : H0483 / Network Programming

Tahun : 2005

Versi : 1.0

learning outcomes
Learning Outcomes

Pada akhir pertemuan ini, diharapkan mahasiswa

akan mampu :

  • menghasilkan program menggunakan protokol dengan Non Blocking I/O
outline materi
Outline Materi
  • Introduction
  • Nonblocking read & write
  • Nonblocking Connect
  • Windows Socket
introduction
<<Introduction>>
  • Beda Blocking dan Non Blocking System
introduction1
<< Introduction >>
  • Non Blocking
introduction2
<< Introduction >>
  • Kapan menggunakan sistem Blocking ?
  • Kapan menggunakan sistem Non Blocking ?
windows socket programming

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
windows socket programming1

UDP Client

UDP Server

WSAStartup()

socket()

socket()

data (request)

sendto()

recvfrom()

data (reply)

recvfrom()

sendto()

closesocket()

closesocket()

WSACleanup()

Windows Socket Programming
wsastartup wsacleanup
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”

wsastartup wsacleanup1
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”

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

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

generic socket address structure
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

};

ipv4 socket address structure
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

};

byte ordering

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
byte ordering functions

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

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

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

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

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

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

// ……

}

}

debug tools
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
references
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
ad