EECS122 Communications Networks Socket Programming - PowerPoint PPT Presentation

Loading...

PPT – EECS122 Communications Networks Socket Programming PowerPoint presentation | free to download - id: 3d68a-MzlkN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

EECS122 Communications Networks Socket Programming

Description:

What mechanisms are available for a programmer who writes network applications? ... allows programmer to learn once, write anywhere ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 34
Provided by: roboticsE
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: EECS122 Communications Networks Socket Programming


1
EECS122Communications NetworksSocket Programming
  • January 30th, 2003
  • Jörn Altmann

2
Questions that will be Addressed During the
Lecture
  • What mechanisms are available for a programmer
    who writes network applications?
  • How to write a network application that sends
    packets between hosts (client and server) across
    an IP network?

IP Network
Client
Server
3
EE122 Projects S03
  • First assignment
  • Implement the client side of an client-server
    architecture (C programming)
  • Handle exceptions in the communication
  • Measure performance of the network and the server

4
Socket ProgrammingTable of Contents
  • Network Application Programming Interface
    Sockets and Internet Sockets
  • Network Programming Tips
  • Client-Server Architecture
  • Example Client Programming
  • Example Server Programming
  • Network Programmers Mistakes

5
Layers of the IP Protocol Suite
Application Layer
Transport Layer
Network Layer
Link Layer
6
Protocol Suite Location
  • Internet Protocol Layer

Application Layer
Transport Layer (TCP, UDP)
Network Layer (IP)
Link Layer
7
Network API
  • Operating system provides Application Programming
    Interface (API) for network application
  • API is defined by a set of function types, data
    structures, and constants
  • Desirable characteristics of the network
    interface
  • Simple to use
  • Flexible
  • independent from any application
  • allows program to use all functionality of the
    network
  • Standardized
  • allows programmer to learn once, write anywhere
  • Application Programming Interface for networks is
    called socket

8
Sockets
  • Sockets provide mechanisms to communicate between
    computers across a network
  • There are different kind of sockets
  • DARPA Internet addresses (Internet Sockets)
  • Unix interprocess communication (Unix Sockets)
  • CCITT X.25 addresses
  • and many others
  • Berkeley sockets is the most popular Internet
    Socket
  • runs on Linux, FreeBSD, OS X, Windows
  • fed by the popularity of TCP/IP

9
Internet Sockets
  • Support stream and datagram packets (e.g. TCP,
    UDP, IP)
  • Is Similar to UNIX file I/O API (provides a file
    descriptor)
  • Based on C, single thread model
  • does not require multiple threads

10
Types of Internet Sockets
  • Different types of sockets implement different
    communication types (stream vs. datagram)
  • Type of socket stream socket
  • connection-oriented
  • two way communication
  • reliable (error free), in order delivery
  • can use the Transmission Control Protocol (TCP)
  • e.g. telnet, ssh, http
  • Type of socket datagram socket
  • connectionless, does not maintain an open
    connection, each packet is independent
  • can use the User Datagram Protocol (UDP)
  • e.g. IP telephony
  • Other types exist similar to the one above

11
Network Programming Tips
  • Byte Ordering
  • Naming
  • Addressing

12
Byte Ordering of Integers
  • Different CPU architectures have different byte
    ordering

D3
F2
Integer representation (2 byte)
13
Byte Ordering Problem
  • Question What would happen if two computers with
    different integer byte ordering communicate?
  • Answer
  • Nothing if they do not exchange integers!
  • But If they exchange integers, they would get
    the wrong order of bytes, therefore, the wrong
    value!
  • Example

Message is Hello,1
14
Byte Ordering Solution
  • There are two solutions if computers with
    different byte ordering system want to
    communicate
  • They must know the kind of architecture of the
    sending computer(bad solution, it has not been
    implemented)
  • Introduction of a network byte order. The
    functions are
  • uint16_t htons(uint16_t host16bitvalue)
  • uint32_t htonl(uint32_t host32bitvalue)
  • uint16_t ntohs(uint16_t net16bitvalue)
  • uint32_t ntohs(uint32_t net32bitvalue)
  • Note use for all integers (short and long),
    which are sent across the network
  • Including port numbers
  • But not for IP addresses

15
Network Programming Tips
  • Byte Ordering
  • Naming
  • Addressing

16
Naming and Addressing
  • Host name
  • identifies a single host (see Domain Name System
    slides)
  • variable length string (e.g. www.berkeley.edu)
  • is mapped to one or more IP addresses
  • IP Address
  • 32 bits (not a number!)
  • written as dotted octets (e.g. 10.0.0.1)
  • Port number
  • identifies an application on a host
  • 16 bit number

17
Client-Server Architecture
response
Client
Server
request
  • Client requests service from server
  • Server responds with sending service or error
    message to client
  • Example Remote Procedure Call

18
Simple Client-Server Example
response
Client
Server
request
socket()connect()send()recv()close()
socket()bind()listen()accept()recv()send()
recv()close()
Connectionestablishment
Data request
Data response
End-of-file notification
19
Example Client Programming
  • Create stream socket (socket() )
  • Connect to server (connect() )
  • While still connected
  • send message to server (send() )
  • receive (recv() ) data from server and process it

20
Initializing Socket
  • Getting the file descriptor
  • int chat_sock
  • if ((chat_sock socket( AF_INET,
    SOCK_STREAM,
  • IPPROTO_TCP)) lt 0)
  • perror("socket")
  • printf("Failed to create socket\n")
  • abort ()
  • 1.parameter specifies protocol/address family
  • 2.parameter specifies the communication type
  • 3.parameter specifies the protocol

21
Connecting to Server
  • struct sockaddr_in sin
  • struct hostent host gethostbyname (argv1)
  • unsigned int server_address (unsigned long
    ) host-gth_addr_list0
  • unsigned short server_port atoi (argv2)
  • memset (sin, 0, sizeof (sin))
  • sin.sin_family AF_INET
  • sin.sin_addr.s_addr server_address
  • sin.sin_port htons (server_port)
  • if (connect(chat_sock, (struct sockaddr ) sin,
  • sizeof (sin)) lt 0)
  • perror("connect")
  • printf("Cannot connect to server\n")
  • abort()

22
Sending Packets
  • int send_packets(char buffer, int buffer_len)
  • sent_bytes send(chat_sock, buffer,
    buffer_len, 0)
  • if (send_bytes lt 0)
  • perror (send")
  • return 0
  • Needs socket descriptor,
  • Buffer containing the message, and
  • Length of the message

23
Receiving PacketsSeparating Data in a Stream
Fixed length record
Variable length record
Variable length record
Fixed length record
A
B
C3
C2
0
1
3
2
9
4
6
8
7
5
  • Use records (data structures) to partition the
    data stream

Variable length record
Variable length record
D
D
0
0
9
6
8
7
5
24
Receiving Packets
  • int receive_packets(char buffer, int buffer_len,
    int bytes_read)
  • int left buffer_len - bytes_read
  • received recv(chat_sock, buffer
    bytes_read, left, 0)
  • if (received lt 0)
  • perror (recv")
  • if (received lt 0)
  • return close_connection()
  • bytes_read received
  • while (bytes_read gt RECORD_LEN)
  • process_packet(buffer, RECORD_LEN)
  • bytes_read - RECORD_LEN
  • memmove(buffer, buffer RECORD_LEN,
    bytes_read)
  • return 0

25
Example 1 Server Programming
  • create stream socket (socket() )
  • Bind port to socket (bind() )
  • Listen for new client (listen() )
  • While
  • Wait for (select() )
  • user connects (accept() )
  • data arrives from client (recv() )
  • data has to be send to client (send() )

26
Example 2 Server Programming
  • create stream socket (socket() )
  • Bind port to socket (bind() )
  • Listen for new client (listen() )
  • Wait until (select() ) returns
  • Switch based on return value of select
  • user connects (accept() )
  • data arrives from client (recv() )
  • data has to be send to client (send() ) or
  • timeout (select() )

27
bind()
  • struct sockaddr_in sin
  • struct hostent host gethostbyname (argv1)
  • unsigned int server_address (unsigned long
    ) host-gth_addr_list0
  • unsigned short server_port atoi (argv2)
  • memset (sin, 0, sizeof (sin))
  • sin.sin_family AF_INET
  • sin.sin_addr.s_addr server_address
  • sin.sin_port htons (server_port)
  • if (bind(chat_sock, (struct sockaddr ) sin,
  • sizeof (sin)) lt 0)
  • perror("bind")
  • printf("Cannot bind server application to
    network\n")
  • abort()

28
bind() Characteristics
  • bind() code of the server application is similar
    to connect() code of the client application
  • bind() tells operating system (i.e. network
    layer) at which port the server application has
    to run
  • Since servers run at specific ports, the
    programmer has to tell the operating system
    (using bind()) these port numbers
  • Client application does not need bind() since
    operating system assigns available port
  • The server application will get the port number
    of the client application through the UDP/TCP
    packet header
  • Port number of server must be known by client
    application in order to connect to the server
  • Note a port number represents an application

29
I/O Multiplexing using select()
  • waits on multiple file descriptors and timeout
  • returns when any file descriptor
  • is ready to be read or
  • written or
  • indicate an error, or
  • timeout exceeded
  • advantages
  • simple
  • application does not consume CPU cycles while
    waiting
  • disadvantages
  • does not scale to large number of file descriptors

30
Network Programmers Mistakes
  • byte ordering
  • separating records in streams
  • use of select()
  • misinterpreting the project specification
  • not knowing all available system calls

31
There are more System Calls
  • Depends on communication type
  • Datagram sockets use recvfrom() and sendto() for
    receiving and sending data
  • Closing connection close(), shutdown()
  • Getting information about a host gethostbyname()

32
Literature
  • Unix Network Programming, volumes 1 and 2 by W.
    Richard Stevens. Published by Prentice Hall
    ISBNs for volumes 1 and 2 013490012X,
    0130810819.
  • Advanced Programming in the Unix Environment by
    W. Richard Stevens. Published by Addison Wesley.
    ISBN 0201563177.
  • man pages on a Unix computer

33
The End
About PowerShow.com