Академический Документы
Профессиональный Документы
Культура Документы
IMPLEMENTATION TIPS
Recall
application application
writes data reads data
socket socket
door door
TCP TCP
send buffer receive buffer
Packet ->
Sequence number
- Keeps track of which packet has been sent and received
ACK
- Indicates receipt of packet in good or bad form
NAK
Window, pipelining
2
- Allows for the sending of multiple yet-to-be-acknowledged packets
Transport Layer – TCP
B
Client - Server
Rclient_UDP.cpp Rserver_UDP.cpp
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF Empty
1 ABCDEF .
2 ABCDEF .
3 ABCDEF .
...
socket socket
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF Empty
1 ABCDEF .
2 ABCDEF .
3 ABCDEF .
...
socket socket
Port: 1235
Sample run:
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF Empty
1 ABCDEF .
2 ABCDEF .
3 ABCDEF .
...
socket socket
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF Empty
1 ABCDEF .
2 ABCDEF .
3 ABCDEF .
...
socket socket
The client sends the contents of the file line by line. One packet
contains exactly one line. In order to implement reliable data
transfer, you will have to modify the packet header to add more
details.
Client - Server
Rclient_UDP.cpp Rserver_UDP.cpp
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF Empty
1 ABCDEF .
2 ABCDEF .
3 ABCDEF .
...
socket socket
UNRELIABLE CHANNEL
NETWORK LAYER
Client - Server
Rclient_UDP.cpp Rserver_UDP.cpp
APPLICATION LAYER
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF 0 ABCDEF
1 ABCDEF 1 ABCDEF
2 ABCDEF 2 ABCDEF
3 ABCDEF 3 ABCDEF
... ...
socket socket
Optional in IPv4
PARAMETERS
Example:
sendto(s, sbuffer, strlen(sbuffer),0,(struct sockaddr*) to, &len);
recvfrom()
Receive data
int recvfrom(SOCKET s, char *msg, int msglen,
int flags, struct sockaddr *from, int *fromlen)
PARAMETERS
s = socket (inside the socket descriptor: port and IP address...)
msg = a pointer to a buffer
msglen = the length of the buffer
flags = 0
from =structure of address with the IP / port #
fromlen=length of the structure
Example:
recvfrom(s, rbuffer, 1, 0,(struct sockaddr *) &from, &len); 14
Client - Server
Rclient_UDP.cpp Rserver_UDP.cpp
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF 0 ABCDEF
1 ABCDEF 1 ABCDEF
2 ABCDEF 2 ABCDEF
3 ABCDEF 3 ABCDEF
... ...
socket socket
UDP SEGMENT
Destination Port: 1235 checksum Length of segment DATA
Client - Server
Rclient_UDP.cpp Rserver_UDP.cpp
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF 0 ABCDEF
1 ABCDEF 1 ABCDEF
2 ABCDEF 2 ABCDEF
3 ABCDEF 3 ABCDEF
... ...
socket socket
//nonblocking option
u_long iMode=1;
ioctlsocket(s, FIONBIO, &iMode);
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF 0 ABCDEF
1 ABCDEF 1 ABCDEF
2 ABCDEF 2 ABCDEF
3 ABCDEF 3 ABCDEF
... ...
socket socket
Reads file using fgets(), then sends the Receives file contents
contents 1 line at a time line-by-line, then stores into a file
fopen(“file1_Windows.txt”, r) fopen(“file1_Saved”, w)
Loop: Loop:
send_unreliably(data) recvfrom()
recvfrom() send_unreliably(ACK)
Reading the file contents
The client reads the file contents line by line using fgets() CLIENT
fgets(send_buffer, SEGMENT_SIZE, fin)
• stops reading the file when it encounters either:
• a new line character (copied into send_buffer)
• EOF (End-Of-File) character
• a NULL-termination character (‘\0’) is automatically appended
• this is counted as one of the characters
A 0 A 0
B 1 B 1
strlen()=4
C 2 C 2
‘\n’ 3 strlen()-1 ‘\0’ 3
‘\0’ 4 ‘\0’ 4
send_buffer send_buffer
Data Format
CLIENT
Remove the line feed character from the row of data read from the file
A 0 A 0
B 1 B 1
strlen()=4
C 2 C 2
‘\n’ 3 strlen()-1 ‘\0’ 3
‘\0’ 4 ‘\0’ 4
send_buffer send_buffer
Start-up Codes (Client – Server)
Rclient_UDP.cpp Rserver_UDP.cpp
CLIENT SERVER
File1_Windows.txt File1_Saved.txt
0 ABCDEF 0 ABCDEF
1 ABCDEF 1 ABCDEF
2 ABCDEF 2 ABCDEF
3 ABCDEF 3 ABCDEF
... ...
socket socket
fopen(“file1_Windows.txt”, r) fopen(“file1_Saved”, w)
Loop: Loop:
send_unreliably(data) recvfrom()
recvfrom() send_unreliably(ACK)
send_unreliably(”CLOSE”)
closesocket() Write everything into file1_Saved.txt
fclose()
closesocket()
Start-up Codes (Client – Server)
0 ABCDEF
0 ABCDEF
CLIENT 1 ABCDEF
2 ABCDEF
SERVER 1 ABCDEF
2 ABCDEF
3 ABCDEF
3 ABCDEF
...
...
fopen(“file1_Windows.txt”, r) fopen(“file1_Saved”, w)
Loop: Loop:
read one line from file recvfrom(receive_buffer)
if(not EOF){ trim ‘\r’, ‘\n’ from receive_buffer
create packet with header fields process receive_buffer
store packet into send_buffer if(receive_buffer contains DATA){
send_unreliably(send_buffer) create ACK packet
Sleep(1); send_unreliably(ACK)
recvfrom(receive_buffer) save_line_without_header
trim ‘\r’, ‘\n’ from receive_buffer } else {
} else { fclose()
fclose() }
send_unreliably(”CLOSE”)
}
closesocket()
closesocket()
Start-up Codes
CLIENT SERVER
Packets can be lost
Sample run:
Rclient 127.0.0.1 1235 0 0 Rserver 1235 0 0
CLIENT SERVER COMMENTS
00 00 Packets can never be corrupted nor lost
01 00 Client may lose packets
00 01 Server may lose ACK packets
01 01 Both client and server may lose packets
10 00 Client may have corrupt bits
00 10 Server may have corrupt bits
10 10 Both client and server may have corrupted bits
11 11 Both client and server may have corrupted bits and may
lose packets
Ultimate Test
Rclient_UDP.cpp Rserver_UDP.cpp
CLIENT SERVER
Bits can be corrupted
File1_Windows.txt File1_Saved.txt
0 ABCDEF 0 ABCDEF
1 ABCDEF 1 ABCDEF
2 ABCDEF 2 ABCDEF
3 ABCDEF 3 ABCDEF
... ...
Extending the codes
Go-Back N
N=Window size = 4
N = Window size = 4
nextSequenceNum
N = Window size = 4
ACKnum
Pipelining Protocol (Go Back-N)
CLIENT
(sender)
N = Window size = 4
0 1 2 3
ACKnum=1
0 1 2 3 4 5
time=1
base=ACKnum+1
baseMax=base+(N-1)
Pipelining Protocol (Go Back-N)
CLIENT
(sender)
N = Window size = 4
0 1 2 3
time=0
Transmit more packets
base=0 (up to baseMax)
baseMax=3
ACKnum=1
0 1 2 3 4 5
time=2
base=ACKnum+1
baseMax=base+(N-1)
WARNING: The following pseudo codes are not
complete. They are meant just to give you an idea
of how to implement a sliding Window protocol.
startTime = clock();
…
...
elapsedTime = (clock() - startTime) / CLOCKS_PER_SEC;
38
Extending the codes
CLIENT 0 ABCDEF SERVER 0 ABCDEF
1 ABCDEF 1 ABCDEF
fopen(“file1_Windows.txt”, r) 2 ABCDEF fopen(“file1_Saved”, w) 2 ABCDEF
3 ABCDEF 3 ABCDEF
Loop: ... Loop: ...