IEG 4180 Tutorial 2 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

IEG 4180 Tutorial 2

Description:

http://msdn2.microsoft.com/en-us/library/ms740668(VS.85).aspx ... http://msdn2.microsoft.com/en-us/library/yd5xkb5c(VS.80).aspx ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 21
Provided by: courseIe
Category:
Tags: ieg | aspx | tutorial

less

Transcript and Presenter's Notes

Title: IEG 4180 Tutorial 2


1
IEG 4180 Tutorial 2
  • Prepared by Zero
  • (Note Certain content adopt from Li Kins
    tutorial notes)

2
Outline
  • Winsock Programming
  • Check for Packet Loss
  • Rate Control
  • Statistics Display

3
Winsock Programming (UDP)
UDP Program Flow
Captured from Lecture Note
4
Winsock Programming (TCP)
TCP Program Flow
Captured from Lecture Note
5
Winsock Programming
  • Winsock is a library from Microsoft
  • Initialization and de-initialization are needed.

WSADATA wsaData int iResult / Request Winsock
2.0 Details of actually obtained winsock version
is in wsaData / //Initialization iResult
WSAStartup( MAKEWORD(2,0), wsaData ) / Your
Network Program Codes are here / / Release
resources / //De-initilization iResult
WSACleanup()
Alternative samplehttp//en.wikipedia.org/wiki/U
ser_Datagram_Protocol
6
Winsock Programming
  • SOCKET skt
  • // Create a TCP socket.
  • // AP_INET is the internet address family formats
    for IPv4
  • // SOCK_STREAM tells function to create a stream
    socket.
  • skt socket(AP_INET, SOCK_STREAM, 0)
  • // Create a UDP socket.
  • // SOCK_DGRAM tells function to create a datagram
    socket.
  • skt socket(AP_INET, SOCK_DGRAM, 0)
  • / other network programming codes /
  • // To close a socket
  • closesocket(skt)

7
Winsock Programning
  • Address structure
  • Winsock provides an address structure specific to
    IPv4 addresses.

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_zero8
//unused struct in_addr in_addr_t s_addr
//IPv4 addr, Big-endian
8
Winsock Programming
  • // Declare a socket address structure
  • sockaddr_in addr
  • // Prepare to bind socket to address
    127.0.0.112345
  • addr.sin_family AF_INET
  • addr.sin_addr.s_addr inet_addr( "127.0.0.1" )
  • addr.sin_port htons( 12345 )
  • // bind the socket to the address
  • bind( skt, (SOCKADDR) addr, sizeof(addr) )
  • unsigned long inet_addr ( const char cp )
  • Convert from dot notation
  • returns an unsigned long value containing a
    suitable binary representation of the Internet
    address given.

9
Winsock Programming
  • Different machines use different ways for
    representing multi-byte integers.
  • The number 137.189.96.168 (89.BD.60.A8 in hex)
    can be represented in two ways

10
Winsock Programming
  • Byte Ordering Functions
  • Winsock provides functions for converting numbers
    from one byte order to another

11
Winsock Programming
  • Accept returns a new socket
  • Use new socket for sending or receiving packet

Captured from Lecture Note
12
Winsock Programming
  • send() / sento()
  • returns the total number of bytes sent, which can
    be less than the number indicated by len.

int send ( SOCKET s, const char buf, int len,
int flags ) int sendto ( SOCKET s, const char
buf, int len, int flags, const struct sockaddr
to, int tolen )
13
Winsock Programming
  • recv() / recvfrom()
  • returns the number of bytes received. If the
    connection has been gracefully closed, the return
    value is zero.

int recv ( int s, char buf, int len, int flags
) int recvfrom ( int s, char buf, int
len, int flags, struct sockaddr from, int
fromlen )
14
Winsock Programming
  • The return codes of the socket APIs only specify
    whether the operation is successful or not.
  • To obtain a more detailed error code, invoke
    WSAGetLastError() immediately after the error
    occurred. Error codeshttp//msdn2.microsoft.com/
    en-us/library/ms740668(VS.85).aspx

if( connect( skt, (SOCKADDR) ServerAddr,
sizeof(ServerAddr) ) SOCKET_ERROR
) switch(WSAGetLastError()) case
WSAENETUNREACH printf(Unreachable network
address\n) break
15
Other Useful Functions
  • int atoi( const char string )
  • Convert strings to interger.
  • Reference
  • http//msdn2.microsoft.com/en-us/library/yd5xkb5c
    (VS.80).aspx
  • VOID WINAPI Sleep(DWORD dwMilliseconds)
  • The minimum time interval for which execution is
    to be suspended, in milliseconds.
  • Only Windows 2000 and beyond are supported
  • Reference
  • http//msdn2.microsoft.com/en-us/library/ms686298
    (VS.85).aspx

16
Check for Packet Loss
  • Both TCP and UDP contain checksum to ensure
    correctness of received packet
  • TCP is reliable, packet loss can be assumed as
    zero
  • UDP is un-reliable (allowing packets to be
    dropped)
  • Adding sequence no. to the data part can help
  • Then simply check for gaps in-between

char pkt128 ((int ) pkt) 1
17
Rate ControlPacketized vs Fluid-like Transmission
  • Fluid-like transmission
  • Continuous flow of data
  • Achieve target transmission rate Tx at any
    instance
  • Packetized transmission
  • Data is sent in a packet-by-packet manner
  • Packets, with size P, are sent into the network
    at speed of Tx
  • Control average transmission rate by adjusting
    the interval between packet transmissions

Data
Time
P/Tx
18
Rate ControlPacketized Transmission
  • Target
  • Sending Rate 1024 bytes / second

While ( condition ) . . Send(1024byes) Sleep(1
second) . .
19
Rate ControlPacketized Transmission
  • To maintain average transmission rate
  • Send out one packet in every fixed interval?
  • Probably results in lower average transmission
    rate
  • There is (non-deterministic) overhead in
    preparing, processing and sending out packets
  • Dynamically adjust the transmission intervals by
    keep tracking time spent in previous
    transmissions

20
Statistics Display
  • All values can be shown in the same line
  • Achieve refreshing through Carriage-Return
    character

for (int i 0 I lt 10 i) printf(\r2d, i)
0
...
9
Write a Comment
User Comments (0)
About PowerShow.com