Client Socket Algorithms - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Client Socket Algorithms

Description:

Get servers IP address / FQDN (fully qualified domain name) ... 1. Find the IP address and protocol port number of the server with which ... Local IP Address ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 20
Provided by: wwwusersI
Category:
Tags: algorithms | client | ip | socket

less

Transcript and Presenter's Notes

Title: Client Socket Algorithms


1
Client Socket Algorithms
  • Lecture 3

2
Learn Algorithms!!!
  • Don't focus on language/functions/etc..
  • Focus on algorithms
  • Knowledge of how protocols work
  • How operating systems use resources
  • If you know WHAT to do, HOW you do it is easy
  • Always design programs before coding
  • Build abstractions!

3
Client Architecture
  • Client programs are usually simpler applications
  • Normally don't have to deal with
  • concurrency
  • daemonisms
  • process resources
  • shared memory/variables/etc.
  • security
  • Can be parameterized, but usually not

4
Identifying Location of Server
  • Remember all TCP/IP connections are made via IP
    address, not name
  • Get servers IP address / FQDN (fully qualified
    domain name)?
  • If FQDN, software must lookup IP address
  • With IP address, convert to sockaddr_in struct
  • Ways to get IP address
  • Hardcoding (not a good idea!)?
  • From a parameter
  • Configuration (database, xml, ini, registry,
    ldap, etc....)?
  • multicast/broadcast

5
Parsing an Address Argument
  • e.g. (NOTE book assumes IPv4 only)?
  • merlin.cs.purdue.edu
  • 128.10.2.3
  • One quick trick is to try to put the argument
    (whatever it is) into a hostent by
    gethostbyname() and if it fails, you know that
    you need to use the other name, and if that
    fails, it is not valid (parsing is not fun!)?
  • IPAddressPortProtocol some method to get
    parameters, up to you...

6
Looking Up a FQDN (not just domain name)?
  • Again, sockets only work with IP address, not
    name
  • Need to represent server with 32bit address
  • 2 functions
  • inet_addr() - returns an ip address in binary
  • gethostbyname() - returns a hostent
  • Return of gethostbyname is invalid/false if it
    fails
  • gethostbyname holds an array of addresses
  • In the case the DNS server holds more than 1 IP
    address for a given FQDN (and it is done often
    for large redundant network applications)?

7
Looking up a Port by Name
  • Ability to get a port number via a name
  • Instead of port 23, use string 'telnet'
  • or port 25, use string 'smtp'
  • Use function getservbyname()?
  • Will return a servent struct that holds
  • name
  • aliases
  • integer port number
  • protocol
  • Again, return value is invalid/false if it fails
    to lookup
  • Looks up values in /etc/services, or equiv on
    Windows

8
Port Numbers and Network Byte Order
  • All data represented on a TCP/IP connection is
    either
  • ASCII characters
  • Converted to network byte order
  • inet_ntoa, inet_aton (network, address)?
  • inet_pton, inet_ntop (protocol, network)?
  • ntohl htonl (host / network long)?
  • ntohs htons (host / network short)?
  • Done for you in getservbyname() function

9
Looking up a protocol by Name
  • Passing in protocol name (TCP/UDP), will return a
    protoent structure
  • struct protoent
  • char p_name
  • char p_aliases
  • int p_proto
  • Gets data from /etc/protocols
  • Has things like ICMP/pipe/TCP/UDP/IP/etc.... (etc
    is not a protocol)?

10
TCP Client Algorithm
  • Algorithm 6.1
  • 1. Find the IP address and protocol port number
    of the server with which communication is desired
  • 2. Allocate a socket
  • 3. Specify that the connection needs to be
    arbitraty, unused protocol port on the local
    machine and allow TCP to choose one
  • 4. Connect the socket to the server
  • 5. Communicate with the server using the
    application-level protocol
  • 6. Close the connection

11
2 Allocate the socket
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • int s
  • s socket(PF_INET, SOCK_STREAM, 0)

12
Choosing a Local Protocol Port Number
  • Client does not care as long as
  • the port does not conflict with the ports that
    other processes on the machine are already using
  • the port has not been assigned to a well-known
    service
  • This happens as a side effect of the connect()
    call
  • Local IP Address
  • For systems with more than 1 address, application
    does not know which one will route to the
    destination
  • Can leave local IP address empty and let TCP fill
    it in

13
Connect TCP Socket to Server
  • Connect system call creates the 3-way handshake
  • Return from connect() either
  • timeout
  • connection established
  • retcode connect(s, remaddr, remaddrlen)?

14
Communicating with Server Using TCP
  • Upon successful TCP connect() return value
  • use send() to initiate (call) server with data
  • use recv()- NOTE call recv() until there are
  • no bytes left to read (return value of 0)?
  • error (return value of -1)?
  • NOTE TCP guarantees delivery of data, but it may
    break up the data into segments and transmit them
    out of order, but by the time they get to your
    application through the recv() call, they will be
    in order!

15
Closing a Server Connection from a Client
  • close() will rip down the connection to the
    server
  • shutdown() allows for 1-way shutdown
  • can close down reading 0
  • can close down writing 1
  • can close down in both ways 2
  • This will alert the server end by an EOF (end of
    file)?

16
Programming a UDP Client
  • Algorithm 6.2
  • 1. Find the IP address and protocol port number
    of the server with which communication is desired
  • 2. Allocate a socket
  • 3. Specify that the communication needs an
    arbitrart unused protocol port on the local
    machine and allow UDP to choose one
  • 4. Specify the server to which messages must be
    sent
  • 5. Communicate with the server using the app
    level protocol
  • 6. Close the connection

17
Connected and Unconnected UDP Clients
  • UDP sockets can be either connected or
    unconnected.
  • Connected mode
  • use connect() to connect to a remote endpoint
  • NOTE no checks to ensure other end is even
    valid!!!
  • used like the TCP model
  • only useful when sending data to one endpoint
  • Unconnected mode (most used)?
  • Need to send remote end address to each call
  • Can do multicast or broadcast messages

18
Communication with a Server Using UDP
  • If a client uses connected UDP
  • can use send/recv, but keep in mind that there is
    no guarantee with UDP, so possibility of deadlock
    is easy
  • UDP transmissions come in datagrams, so more data
    is truncated
  • Only real difference is only one recv() call is
    needed

19
Closing UDP
  • For connected UDP
  • Since there are no mechanisms to deal with
    transmission guarantees, there is no message with
    close to tell the other side to stop need for
    timeout mechanism
  • shutdown() function still will not tell the other
    side that it is shutdown
Write a Comment
User Comments (0)
About PowerShow.com