Network Programming Using Internet Sockets - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Network Programming Using Internet Sockets

Description:

addrlen: length of addr in bytes ... len: the length of that data in bytes. flags: ... fromlen: the length of the address stored in from. 20. Return values ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 26
Provided by: webEngrOr
Category:

less

Transcript and Presenter's Notes

Title: Network Programming Using Internet Sockets


1
Network Programming Using Internet Sockets
Jong-Hoon Youn CS372 Winter 2001
2
A closer look at network structure
  • network edge applications and hosts
  • network core
  • routers
  • network of networks

3
The network edge
  • end systems (hosts)
  • run application programs
  • e.g., WWW, email
  • at edge of network?

4
Network edge connection-oriented service
  • TCP service RFC 793
  • reliable, in-order byte-stream data transfer
  • loss acknowledgements and retransmissions
  • flow control
  • sender won?t overwhelm receiver
  • congestion control
  • senders slow down sending rate? when network
    congested
  • Goal data transfer between end sys.
  • handshaking setup (prepare for) data transfer
    ahead of time
  • set up state? in two communicating hosts
  • TCP - Transmission Control Protocol
  • Internet?s connection-oriented service

5
Network edge connectionless service
  • Goal data transfer between end systems
  • UDP - User Datagram Protocol
  • Internet's connection-less service
  • unreliable data transfer
  • no flow control
  • no congestion control
  • App?s using TCP
  • HTTP (WWW), FTP (file transfer), Telnet (remote
    login), SMTP (email)
  • App?s using UDP
  • streaming media, teleconferencing, Internet
    telephony

6
The Network Core
  • mesh of interconnected routers
  • the fundamental question how is data transferred
    through net?
  • Circuit switching dedicated circuit per call
    telephone net
  • packet-switching data sent thru net in discrete
    chunks?

7
Sockets
  • Basic definition - "endpoint of communication"
  • Sockets are dominant TCP/IP application API
  • Allows connect streams (TCP) or discrete messages
    (UDP) between processes on same machine or cross
    network.

8
Creating a Socket
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • int socket(int domain, int type, int protocol)
  • domain AF_INET, AF_UNIX
  • type SOCK_STREAM, SOCK_DGRAM
  • protocol 0 the default protocol for the given
    address family and socket type.
  • This call returns a socket descriptor that can be
    used as a parameter to reference the socket in
    subsequent system calls, or -1 on error.

9
Styles of Communication
  • Datagram Sockets (SOCK_DGRAM)
  • connectionless and unreliable UDP
  • Messages may be lost, duplicated or received out
    of order.
  • Stream Sockets (SOCK_STREAM)
  • connection-oriented and reliable TCP
  • The transport protocol ensure that data is
    delivered in order without loss, error or
    duplication.

10
Binding a Socket to a Port
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • int bind(int sockfd, struct sockaddr my_addr,
    int addrlen)
  • sockfd the socket descriptor returned by
    socket().
  • my_addr a pointer to a struct sockaddr that
    contains information about the local address
  • addrlen sizeof (struct sockaddr)
  • bind associates the given socket with a port on
    your local machine. It returns -1 on error.

11
  • include ltnetinet/in.hgt
  • struct sockaddr_in
  • uint8_t sin_len / length of structure (16) /
  • sa_family_t sin_family / AF_INET /
  • in_port_t sin_port / 16-bit Port number /
  • struct in_addr sin_addr / 32-bit Internet
    address /
  • char sin_zero8 / not used /
  • struct in_addr
  • in_addr_t s_addr / 32-bit IPv4 address /
  • / it used to be a union /

12
  • sockaddr_in example

length
AF_INET
16
AF_INET
16-bit port
7777
32-bit IPv4
2160141825
address
(address)
(Unused)
(Unused)
Fixed length (16 bytes)
13
  • ltExamplegt
  • int sockfd
  • struct sockaddr_in my_addr
  • sockfd socket(AF_INET, SOCK_STREAM, 0)
  • / do some error checking! /
  • my_addr.sin_family AF_INET / host byte order
    /
  • my_addr.sin_port htons(7777)
  • / short, network byte order /
  • my_addr.sin_addr.s_addr inet_addr("132.241.5.10"
    )
  • bzero((my_addr.sin_zero), 8) / zero the rest
    of the struct /
  • bind(sockfd, (struct sockaddr )my_addr,
  • sizeof(struct sockaddr))
  • / don't forget your error checking for
    bind() /

14
Listening for connections
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • int listen(int sockfd, int queuelen)
  • sockfd the socket descriptor returned by
    socket().
  • queuelen the maximum number of queue connection
    that will be allowed.
  • Listen initializes a queue for waiting connection
    requests. It returns -1 on error.

15
Accepting a connection
  • include ltsys/socket.hgt
  • int accept(int sockfd, void addr, int addrlen)
  • sockfd the listening socket descriptor
  • addr the address of peer socket on new
    connection (returns client ip/port).
  • addrlen length of addr in bytes
  • Accept takes the first pending connection request
    off the queue and returns a socket descriptor for
    a new, connected socket. If there are no pending
    connection requests, accept blocks. It returns -1
    on error.

16
  • ltExamplegt
  • int sockfd, new_fd, sin_size
  • struct sockaddr_in their_addr
  • . . . socket()
  • . . . bind()
  • listen(sockfd, 5)
  • sin_size sizeof(struct sockaddr_in)
  • new_fd accept(sockfd, their_addr, sin_size)

17
Establishing a connection
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • int connect(int sockfd, struct sockaddr
    serv_addr, int addrlen)
  • sockfd the socket descriptor returned by
    socket().
  • serv_addr a pointer to a struct sockaddr that
    contains information about the server address
  • addrlen sizeof (struct sockaddr)
  • connect will return -1 on error

18
Send(), receive()
  • int send(int sockfd, char msg, int len, int
    flags)
  • sockfd socket descriptor you want to send data
    to
  • msg a pointer to the data
  • len the length of that data in bytes
  • flags usually 0 (cf. MSG_OOB).
  • int recv(int sockfd, char buf, int len, int
    flags)
  • sockfd the socket descriptor to read from
  • buf the buffer to read the information into
  • len the maximum length of the buffer
  • flags usually 0 (cf. MSG_PEEK).

19
sendto(), receivefrom()
  • int sendto(int sockfd, char msg, int len, int
    flags, struct sockaddr to, int tolen)
  • to a pointer to a struct sockaddr which contains
    the destination IP address and port.
  • tolen sizeof(struct sockaddr).
  • int recvfrom(int sockfd, char buf, int len, int
    flags, struct sockaddr from, int fromlen)
  • from a pointer to a struct sockaddr that will be
    filled with the IP address and port of the
    origin.
  • fromlen the length of the address stored in from.

20
  • Return values
  • send(sendto) returns the number of byte accepted
    by the transport layer, or -1 if some error is
    detected.
  • recv(recvfrom) returns the number of bytes
    actually received, or -1 if some error is
    detected.
  • By default, all the reading and writing calls are
    blocking
  • Read calls do not return until at least one byte
    of data is available for reading.
  • Write calls block until there is enough buffer
    space to accept some or all of the data being
    sent.

21
Closing connection
  • int close(sockfd)
  • This will prevent any more reads and writes to
    the socket. Anyone attempting to read or write
    the socket on the remote end will receive an
    error.

22
Byte order conversion
  • There are two types short(two bytes) and long
    (four bytes)
  • htons() Host to Network Short
  • htonl() Host to network Long
  • ntohs() Network to Host short
  • ntohl() Network to Host Long
  • Some hosts (such as the VAX), have a host order
    for integer values that reverse the order of
    bytes. gt put your bytes in network order before
    you put them on the network (portable).

23
little-endian byte order
increasing memory address
address A1
address A
high-order byte
low-order byte
MSB
LSB
16-bit value
high-order byte
low-order byte
address A1
address A
big-endian byte order
increasing memory address
24
Other useful functions
  • Convert an IP address in number-and-dot notation
    into an unsigned long.
  • struct sockaddr_in ina
  • ina.sin_addr.s_addr inet_addr(132.241.5.10?)
  • Convert string IP addresses to unsigned long.
  • printf(s?, inet_ntoa(ina.sin_addr))

25
  • include ltnetdb.hgt
  • struct hostent gethostbyname(char name)
  • returns a pointer to the filled struct hostent,
    or NULL on error.
  • struct hostent
  • char h_name / official name of the host /
  • char h_aliases / alterminate names for the
    host /
  • int h_addrtype / usually AF_INET /
  • int h_length / length of the address in
    bytes(4) /
  • char h_addr_list / network addresses for the
    host /
  • define h_addr h_addr_list0
Write a Comment
User Comments (0)
About PowerShow.com