1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 [email protected].

25
1 COMP 445 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 [email protected]

Transcript of 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 [email protected].

Page 1: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

1

COMP 445COMP 445

Lab tutorialLab assignment 2

Fall [email protected]

Page 2: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

2

Lab 2 Specification

• Transfer files using UDP.

• Packets might be lost during the transfer. (router.cpp code drops packets)

• Output of program is the same as Lab1. There are couple of new concepts:– Stop and Wait protocol: to guarantee delivery– Three way handshake: to start the S/W

protocol.

Page 3: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

3

Lab 1

Client server

TCP guarantees that all packets are delivered.

Page 4: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

4

Stop and wait

Client server

If a packet is dropped, the server will not know

Page 5: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

5

Stop and wait

Client server

Client waits for an Ack for every packet

packet

packet

packet

Ack

Ack

Ack

Page 6: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

6

Stop and wait

Client server

Client waits for an Ack for every packet

packet

Packet LOST

packet

Ack

Ack

Wait for an ack.

After a timeout, send the same packet again

Page 7: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

7

Stop and wait

Client server

How will the server know that the third packet is the same as the second packet.

Packet

Packet

Packet

Ack

Ack

Wait for an ack.

After a timeout, send the same packet again

Ack LOST

Page 8: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

8

Stop and wait

Client server

How will the server know that the third packet is the same as the second packet.

Solution: number the packets

Packet1

Packet2

Packet2

Ack1

Ack2

Wait for an ack.

After a timeout, send the same packet again

Ack2 LOST

After checking the number, the client ignore this packet

Page 9: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

9

Stop and wait numbering system

Client server

How will the server know that the third packet is the same as the second packet.

Solution: number the packets

Packet0

Packet1

Packet1

Ack0

Ack1

Wait for an ack.

After a timeout, send the same packet again

Ack1 LOST

Client expects packet0, when packet1 is received, it ignores it.

Page 10: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

10

Three way hand shake

• To fix the starting numbers at the source and destination, its good idea to add some hand shake above all this process.

Page 11: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

11

Three way hand shake36

Client server

36

36, 55

Client generates random number let it be equal to 36

The server save this number, and generate its own number 55.

55

36

55

36

55 36

55

Page 12: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

12

send36

Client server

36

36, 55

Client generates random number let it be equal to 36

The server save this number, and generate its own number 55.

55

36

55

36

55

36

55packet1

ack1

0

1

Page 13: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

13

receive36

Client server

36

36, 55

Client generates random number let it be equal to 36

The server save this number, and generate its own number 55.

55

36

55

36

55

36

55

packet0

ack0

0

1

Page 14: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

14

Program architectureProgram architecture

Clientport 5000

port 7000 | port 7001

Router

discard delay

Serverport 5001

Page 15: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

15

Main differences - socketMain differences - socket

Creating a UDP socket• Instead of:

s = socket(AF_INET,SOCK_STREAM,0)

• Use:s = socket(AF_INET,SOCK_DGRAM,0)

Binding: Client: binds its socket with a physical address (IP : INADDR_ANY

and port number : 5000)

Server: binds its socket with a physical address (IP : INADDR_ANY and port number : 5001)

Page 16: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

16

SOCKET s;SOCKADDR_IN sa; // port 5000, client IP addressSOCKADDR_IN sa_in; // router info, IP, port(7000)if((s = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET)

throw "Socket failed\n";

memset(&sa,0,sizeof(sa));sa.sin_family = AF_INET;sa.sin_port = htons(port);sa.sin_addr.s_addr = htonl(INADDR_ANY); //host to network//bind the port to the socketif (bind(s,(LPSOCKADDR)&sa,sizeof(sa)) == SOCKET_ERROR)

throw "can't bind the socket";

cin >> remotehost;rp=gethostbyname(remotehost);memset(&sa_in,0,sizeof(sa_in));memcpy(&sa_in.sin_addr, rp->h_addr, rp->h_length);sa_in.sin_family = rp->h_addrtype; sa_in.sin_port = htons(7000);

CLIENT

Page 17: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

17

SOCKET s;SOCKADDR_IN sa; // port 5001, server IP addressSOCKADDR_IN sa_in; // router info, IP, port(7001)if((s = socket(AF_INET,SOCK_DGRAM,0))==INVALID_SOCKET)

throw "Socket failed\n";

memset(&sa,0,sizeof(sa));sa.sin_family = AF_INET;sa.sin_port = htons(port);sa.sin_addr.s_addr = htonl(INADDR_ANY);//bind the port to the socketif (bind(s,(LPSOCKADDR)&sa,sizeof(sa)) == SOCKET_ERROR)

throw "can't bind the socket";

router IP and port numbers are obtained from the recvfrom(…) function.

SERVER

Page 18: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

18

Main differences - sending Main differences - sending Sending frames – you pass the destination

address to the send() function.• Instead of:ibytessent = send(s, (char*)&message_frame, sizeof(message_frame), 0);

• Use:ibytessent = sendto(s, (const char*)&message_frame, sizeof(message_frame), 0,(struct sockaddr*) &sa_in, sizeof(sa_in));

Page 19: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

19

Main differences - receivingMain differences - receivingReceiving frames (you receive the socket address of the

sender)• Instead of:

ibytesrecv = recv(s, (char*)& message_frame, sizeof(message_frame),0)

• Use:fd_set readfds; //fd_set is a typeFD_ZERO(&readfds); //initialize FD_SET(s, &readfds); //put the socket in the set

if(!(outfds = select (1 , &readfds, NULL, NULL, & timeouts)))

{//timed out, return}

if (outfds == 1) //receive frame

ibytesrecv = recvfrom(s, (char*)& message_frame, sizeof(message_frame),0, (struct sockaddr*)&fromAddr, &fromAddrSize);

If this parameter is NULL, you are listening forever

Page 20: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

20

How to define a timer with 300ms for select?

#define UTIMER 300000

#define STIMER 0

struct timeval timeouts;

timeouts.tv_sec=STIMER;

timeouts.tv_usec=UTIMER;

Page 21: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

21

Stop and wait protocolStop and wait protocolClient

Send a packet

if time_out

send the packet again

else //ack received

send a new packet

Page 22: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

22

Stop and wait protocolStop and wait protocolServer

wait for the first packet

send acknowledge

while(!EOF)

Get a packet;

Send an ack;

Page 23: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

23

Packet structure usedPacket structure used#define STOPNWAIT 1 //Bits width For stop and waitstruct MESSAGE_FRAME {unsigned char header;unsign int snwseq:STOPNWAIT;char data[MAX_SIZE];//or error message

} message_frame;

struct THREE_WAY_HS{int client_number;int server_number;int direction;char file_name[FILE_NAME_SIZE];

} three_way_hs;

Page 24: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

24

Three way hand-shakingThree way hand-shaking#include <stdlib.h>THREE_WAY_HS three_way_hs;srand((unsigned)time(NULL));

three_way_hs.client_number = rand();

CLIENT SERVER

three_way_hs.server_number = rand();

sequence_number = three_way_hs.server_number % 2;

Send an acknowledgment with sequence_number

You may send the file name and the direction here

Page 25: 1 COMP 445 Lab tutorial Lab assignment 2 Fall 2006 ae_abdal@cs.concordia.ca.

25

If the client sends out its first packet, the router has received it and forwarded it to the server side. Then the Router displays an error”Socket error 10054” and the server seems to receive nothing. What’s wrong with my program?

check your receiving code of the server side, make sure you did every thing right(e.g when you are binding the server socket, you give the right port number to the server socket address)

””Socket error 10054”Socket error 10054”