Title: IEG 4180 Tutorial 2
1IEG 4180 Tutorial 2
- Prepared by Zero
- (Note Certain content adopt from Li Kins
tutorial notes)
2Outline
- Winsock Programming
- Check for Packet Loss
- Rate Control
- Statistics Display
3Winsock Programming (UDP)
UDP Program Flow
Captured from Lecture Note
4Winsock Programming (TCP)
TCP Program Flow
Captured from Lecture Note
5Winsock 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
6Winsock 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)
7Winsock 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
8Winsock 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.
9Winsock 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
10Winsock Programming
- Byte Ordering Functions
- Winsock provides functions for converting numbers
from one byte order to another
11Winsock Programming
- Accept returns a new socket
- Use new socket for sending or receiving packet
Captured from Lecture Note
12Winsock 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 )
13Winsock 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 )
14Winsock 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
15Other 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
16Check 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
17Rate 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
18Rate ControlPacketized Transmission
- Target
- Sending Rate 1024 bytes / second
While ( condition ) . . Send(1024byes) Sleep(1
second) . .
19Rate 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
20Statistics 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