Title: IEG 4180 Tutorial 1
1IEG 4180 Tutorial 1
- Prepared by Zero
- (Note Certain content adopt from Zhang Shenglis
tutorial notes)
2Outline
- Project 1 - NetProbe Console Edition
- Description
- The BIG picture
- WinSock Programming
- Check for Packet Loss
- Rate Control
- Statistics Output
3Project 1 NetProbe Console Edition
- Goal Develop a tool for throughput testing
- Console application using WinSock
- Three Modes
- HostInfo Mode
- Send Mode
- Receive Mode
4HostInfo Mode
- Resolve and find out addresses and names for a
given hostname(DNS) or IP address - For example www.google.com
5Send Mode
- Send data using multi-threaded, blocking I/O
- When using blocking I/O, all functions block the
entire program until they finish, so the program
cant show the statistics according to refresh
interval - Transmission and statistics display function
should be placed into separate threads
6Receive Mode
- Receive data using multi-threaded, blocking I/O
(similar to send mode) - Keep track of the traffic statistics and
summarize them
7NetProbe Console Edition
- Things you need to know in order to do the
project - C
- Blocking I/O
- Multi-threaded programming
8The BIG picture (Network view)
Send
Receive
Start listen(receive mode start)
Start connect (SYN)(send mode start)
Accept connection (SYNACK)
Keep receiving packet (ACK)
Certain logic to maintain sending rate
Stop sending data (FIN)(required number of
packets reached)
Stop receiving (ACK FIN)
(ACK)
Suggested flow only
9The BIG picture (Program view)
Send
Program start
Receive
Program start
Listen and wait
Try to connect
Use the same primary thread
Use the same primary thread
Create thread
Create thread
Loop to sleep and update the statistics
Send dataaccording tosending rate
Loop to sleep and update the statistics
Loop toreceivedata
Update statistics
Update statistics
Fetch statistics
Fetch statistics
Shared data
Shared data
Suggested flow
10Winsock Programming (UDP)
UDP Program Flow
Captured from Lecture Note
11Winsock Programming (TCP)
TCP Program Flow
Captured from Lecture Note
12Winsock 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
13Winsock 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)
14Winsock 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
15Winsock 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.
16Winsock 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
17Winsock Programming
- Byte Ordering Functions
- Winsock provides functions for converting numbers
from one byte order to another
18Winsock Programming
- Accept returns a new socket
- Use new socket for sending or receiving packet
Captured from Lecture Note
19Winsock 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 )
20Winsock 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 )
21Winsock 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
22Other 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
23Check 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
24Rate 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
25Rate ControlPacketized Transmission
- Target
- Sending Rate 1024 bytes / second
While ( condition ) . . Send(1024byes) Sleep(1
second) . .
26Rate 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
27Statistics 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(\rd, i)
0
...
9