Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from 1-126. (128.x.x.x would not be a class A) 127.x.x.x is reserved.

Similar presentations


Presentation on theme: "Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from 1-126. (128.x.x.x would not be a class A) 127.x.x.x is reserved."— Presentation transcript:

1 Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from 1-126. (128.x.x.x would not be a class A) 127.x.x.x is reserved for loopback (127.0.0.1 often used) x.255.255.255 is used from broadcast x. 0.0.0 is used as a ‘default’ router This allows 16,277,214 (2 24 -2) hosts per class A network. Only a few class A licenses are not shared. covers 50% of the entire address space 0 7 bits/1 st byte 24 bits for host addresses/3 bytes

2 Class B Addresses The 1 st 2 bits of a class B address is 10 Allows for 2 14 or 16,384 network licences, 216-2 hosts per network # 127.x.x.x is reserved for loopback (127.0.0.1 often used) 126 network numbers x.x.255.255 is used for broadcast x. x.0.0 is used as a ‘default’ router This allows 16,384 (2 14 -2) hosts per class A network Represents 25% of all available IP addresses 1 st byte is from 128 to 191 10 14 bits 16 bits for host addresses/2 bytes

3 Class C Addresses The 1 st 3 bits of a class C address is 110 Allows for 2 21 or 2,097,152 network licences, 254 hosts per network 127.x.x.x is reserved for loopback (127.0.0.1 often used) 126 network numbers x.x.x.255 is used for broadcast x. x.x.0 is used as a ‘default’ router Represents 12.5% of all available IP addresses first byte is 192-223 110 21 bits/3 bytes 8 bits

4 Class D Addresses The 1 st 4 bits of a class B address is 1110 first byte is 192-223 Reserved by the Internet Address Number Authority (IANA) for broadcast purposes 1110 Addresses from 224.0.0.0 - 239.255.255.255

5 Class E Addresses The 1 st 5 bits of a class B address is 11110 first byte is 240-255 Reserved for experimental use 1110 Addresses from 224.0.0.0 - 239.255.255.255

6 Sockets And Connections (exploring Beej’s client and server program)

7 Writing a Client in C gettaddrinfo(...) – name+port -> IP address inet_ntop(...) - only needed to display address socket(...) - create an fd for the socket connect(...) - open the socket recv(...) - receive bytes from the socket close(...) or shutdown(..) – close the socekt

8 getaddrinfo(name,service,hints,result) name: a string, ie: humber.c service: a port# or service name, “80”, “http” hints: uses the addrinfo to suggest V4, V6... results: a linked list of addrinfo s

9 } int ai_flags; int ai_family; //AF_INET, AF_INET6, AF_UNIX // AF_UNSPE int ai_socktype; //SOCK_STREAM, SOCK_DGRAM int ai_protocol; size_t ai_addrlen; struct sockaddr *ai_addr; //different for V4, V6 char *ai_canonname; //not used? struct addinfo * ai_next; //it’s a linked list!jk struct addrinfo {

10 int socket(family,socketType,protocol) family: AF_INET, AF_INET6, AF_UNIX socketType: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW,... protocol - set to zero. Most socket types have only one protocol so we can ignore this

11 connect(fd,ipAddr,addrLength) The fd is the local end of the socket the ipAddr is either V4 or V6 – a string addrLength is the length of the string Fails if the service is not available. Not used with SOCK_DGRAM (connectionless)

12 recv(fd,buf,bufsize,flags) exactly like read – except for the flags unless socket is non-blocking it waits for input we don’t use flags in the 1st example – MSG_DONTWAIT – MSG_ERRQUEUE – MSG_OOB – MSG_PEEK

13 close(fd) Yes, it’s really simple!!!!

14 shutdown(fd, how) ‘how’ is either SHUT_RD, SHUT_WR, SHUT_RDWR SHUT_WR used to signal an EOF to the other end If we have multiple threads SHUT_RDWR decrements a count leaving the socket open

15 Writing a Server in C gettaddrinfo(...) - info about this machine socket(...) create a socket setsockopt(...) - define properties of the socket bind(...) listen(...) - wait for a connection – this blocks freeaddrinfo() - return the linked list’s memory sigaction (...) - reap any dead child processes accept(...) - accept the connection inet_ntop(...) - not req’d: display the connection fork a child to carry on the conversation – send(...) (can also receive) – close(....) - close the connection

16 getaddrinfo(NULL,service,&hints,&result) NULL - not needed service - port or service name as a string hints – field.ai_flags is AI_PASSIVE – use this host – ai_socktype - SOCK_STREAM – ai_family - AF_UNSPEC - whatever. AF_INET, AF_INET6 Returns: a list of IP addresses, usually 1 or an error code – service may be in use

17 int socket(family,socketType,protocol) family: AF_INET, AF_INET6, AF_UNIX socketType: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW,... protocol - set to zero. Most socket types have only one protocol so we can ignore this

18 setsockopt(fd,level,optname,&value,len) Used to set socket characteristics fd – the socket id from call to socket() level – always SOL_SOCKET - it corresponds to the protocol, and we don’t have any optname: SO_REUSEADDR - set this to 1/true other options will come up later. For a list of options, see: man 7 socket value - settings for options may be different data structures – pass an address len

19 bind(fd,addressInfo,length) fd – the socket we just opened addressInfo - AF_type, port – we have to and will match the info in the call to socket length – size bind generates a name for the socket in the proc directory. In the client program, the call to socket only connects to an existing socket. ONLY THE SUPERUSER can use a port <1024 If the ip address is 0 (INADDR_ANY) then you can bind to any network card in the machine.

20 listen(fd,nItems) Sets up a queue for the socket of nItems. If a server is busy handling a connection it might miss the next request for one, even if we fork quickly. However if the average interarrival time is too high, no size may be big enough. This is to handle surges in requests, ie: when a class logs into a service all at once.

21 int accept(fd, &theirAddr, length) blocks/waits for a connection returns their addresss info and the bidirectional socket to talk to

22 send(fd,buffer,length, flags) send a message to the client

23 close(fd) Yes, it’s that simple Can also use shutdown(fd,how)

24 Other Management Tasks memset(&memoryBlock,val, nBytes) freeaddrinfo(serverInfo) sigemptyset(signalset) – clears all signal handlers sigaction(SIGHCHILD, handler,lastUse) - advanced version of signal(signum,handler) - it can be ignored. Use ps and kill -9 to terminate rogue children


Download ppt "Class A Addresses The 1 st bit of a class A address is 0 The 1 st byte has a value from 1-126. (128.x.x.x would not be a class A) 127.x.x.x is reserved."

Similar presentations


Ads by Google