SlideServe
Menu
Préférences
© SlideServe · v2026

Comprehensive Guide to Socket Options in Network Applications

reginaf avatar
12 diapositives · 159 Views · Jan 04, 2020

Learn about the sockets API, TCP/IP stack layers, socket options, programming with sockets, and practical examples in Linux. Enhance your understanding of networking fundamentals and socket manipulation techniques.

Comprehensive Guide to Socket Options in Network Applications - slide 1
01 / 12Comprehensive Guide to Socket Options in Network Applications
1 / 17

Télécharger la présentation

Comprehensive Guide to Socket Options in Network Applications
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Socket options A way for network applications to ‘tweak’ the processing done at lower-levels of the TCP/IP stack

  2. The TCP/IP stack Application layer FTP, HTTP, SSH, DHCP, etc The sockets API Transport layer TCP, UDP, etc Network layer IP, ICMP, etc Link layer ARP, RARP, etc Physical layer DSL, FDDI, etc

  3. Encapsulation message data Application segment UDP Header data Transport datagram IP Header UDP Header data Network frame Frame Header IP Header UDP Header data Frame CRC Link stream-of-bits preamble Frame Headerd IP Header UDP Header data Frame CRC Inter-Frame Gap

  4. Key library functions • int socket() • int bind(), int connect() • int listen(), int accept() • int write(), int read() • int send(), int recv() • int sendto(), int recvfrom() • int shutdown(), int close()

  5. Simple program-flow example int sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); connect( sock, (sockaddr*)&saddr, sizeof( saddr ) ); write( sock, message, sizeof( message ) ); close( sock );

  6. socket-address object struct sockaddr_in saddr; socklen_t salen = sizeof( saddr ); bzero( &saddr, salen ); saddr.sin_family = AF_INET; saddr.sin_port = htons( port_number ); saddr.sin_addr.s_addr = htonl( peer_ip_address ); Using the sockets API requires allocating and initializing data-objects known as ‘socket-addresses’ (aka ‘socket-names’), with some numeric fields which require using the Internet’s standard ‘big-endian’ byte-order, rather than the ‘little-endian’ byte-order employed within Intel’s x86 processors. But helper- functions, like htons() and htonl(), will convert host-order into network-order.

  7. Using socket ‘options’ • The default behavior of the kernel routines for the lower-layers of the TCP/IP protocol stack may not be fully suitable for the aims of particular network application programs • But applications usually can’t alter code in an operating system’s protected kernel • The sockets API offers a ‘workaround’ for such situations, i.e.: int setsockopt()

  8. Function prototypes int getsockopt( int sd, int level, int optname, void *optval, socklen_t *optlen ); int setsockopt( int sd, int level, int optname, void *optval, socklen_t optlen ); There are various socket options, which apply to various levels in the networking system’s software hierarchy, and which selectively apply to various types of sockets -- and which are implemented to varying degrees within different versions of popular operating systems. We will demonstrate use of two socket options available in Linux for datagram sockets: SO_BROADCAST and SO_BINDTODEVICE. Our demo also illustrate the use of ‘write()’ for a connected socket.

  9. Demo: ‘bindtoif.cpp’ • This program lets a privileged user write a ‘broadcast’ message to all of the hosts on our classroom’s Local Area Network – as you can confirm using our ‘nicwatch’ tool • And -- the user can choose the interface! • Normally an application wouldn’t be able to send a ‘broadcast’ message, nor be able to select which interface gets used

  10. Overview Get the name of the desired network interface from the command-line Open an internet datagram socket Turn on the ‘SO_BROADCAST’ socket-option Bind the chosen network interface to that socket Connect the socket to the network‘s broadcast address Write a message to the connected socket Show the user a confirmation message

  11. Programming details • Now we take a ‘timeout’ from these slides to look carefully line-by-line at the source- code which will implement those steps • Then we will be ready to compile and run our ‘bindtoif’ demo-program (using ‘sudo’) • You should all be able to watch the arrival of the broadcast message at your desktop!

  12. ‘ifconfig’ • Remember that your classroom computer leaves the ‘eth1’ interface disabled after a reboot – so you will need to use ‘ifconfig’ to enable that interface and also assign it an appropriate Internet Protocol address: $ sudo /sbin/ifconfig eth1 192.168.1.xxx up