Introduction to sockets
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

Introduction to Sockets PowerPoint PPT Presentation


  • 59 Views
  • Uploaded on
  • Presentation posted in: General

shri. Introduction to Sockets. K.C. Rao ([email protected]). Socket Interface. What is the Socket interface? It is a protocol independent interface to multiple transport layer primitives

Download Presentation

Introduction to Sockets

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Introduction to sockets

shri

Introduction to Sockets

K.C. Rao

([email protected])

CDAC-Mumbai


Socket interface

Socket Interface

  • What is the Socket interface?

    • It is a protocol independent interface to multiple transport layer primitives

    • It is an interface (a “door”) into which an application process can both send and receive messages to/from another (remote or local) application process

  • History

    • The first implementation was released along with BSD 4.2. Since then, it has undergone several improvements

CDAC-Mumbai


Typical client server interaction using tcp

Typical Client-Server interaction using TCP

Server

Client

Server

socket()

well-known port

bind()

socket()

listen()

connect()

accept()

Connection establishment

TCP three-way handshake

Block until

connection from client

write()

Data (request)

read()

Process requests

write()

Data (reply)

read()

close()

read()

End-of-file notification

CDAC-Mumbai

close()


Creating a socket

Creating a Socket

#include <sys/socket.h>

int socket(int family, int type, int protocol);

Returns: non-negative socket descriptor if OK, –1 on error

  • family specifies address or protocol family. Can be AF_INET, AF_INET6, AF_LOCAL, etc

  • type specifies socket type. Can be SOCK_DGRAM, SOCK_STREAM, SOCK_RAW

  • protocol argument is normally left to zero except for raw sockets- need not bother about it in this elementary discussion

CDAC-Mumbai


Closing a socket

#inlcude <unistd.h>

int close(int sockfd);

Returns: 0 if OK, –1 on error

Closing a socket

The transport layer generally tries to flush out, i.e. send, any remaining data, when a close is done.

CDAC-Mumbai


Establishing a connection

Establishing a connection

#include <sys/socket.h>

int connect(int sockfd, const struct sockaddr* servaddr, socklen_t servaddrlen);

Returns: 0 if OK, -1 on error

  • This function is used by a client to establish a connection with a server

  • The socket address structure, sockaddr, contains the address information of the server to connect to

  • In general, the port number to be used on the client’s side is chosen by the kernel (ephemeral port)

CDAC-Mumbai


Background information socket address structure

Background Information:Socket Address Structure

  • This contains the protocol specific addressing information

  • The general structure is named sockaddr

  • Each of the protocols supported by a socket implementation have their own socket address structure sockaddr_suffix

    -Where ‘suffix’ represents the protocol family

  • Eg: sockaddr_in – Internet/IPv4 socket address structure

    sockaddr_ipx – IPX socket address structure

CDAC-Mumbai


The socket address structure posix

struct sockaddr {

sa_family_t sa_family; /*Address Family, AF_XXX*/

char sa_data[14]; /*14 bytes of protocol address*/

};

The Socket Address Structure(POSIX)

struct sockaddr_in {

sa_family_t sin_family; /* must be AF_INET */

in_port_t sin_port; /*16-bit TCP or UDP port number*/

___ /*network byte ordered*/

struct in_addr sin_addr; /*32-bit IPv4 address*/

----- /*network byte ordered*/

char sin_zero[8]; /* Not used, must be zero */

};

struct in_addr {

in_addr_t s_addr; /* 32-bit IPv4 address*/

/*network byte ordered */

};

CDAC-Mumbai


Associating a local address with a socket

Associating a local address with a socket

#include <sys/socket.h>

int bind(int sockfd, struct sockaddr* myaddr, socklen_t addrlen);

Returns: 0 if OK, -1 if error

  • It is used for explicitly associating a protocol specific local address to a socket

  • For Internet Protocols, bind() associates with the socket a port number and/or one of the IP addresses of the host on which the program is running (note that the host may be multi-homed)

CDAC-Mumbai


Associating a local address with a socket1

Associating a local address with a socket

  • It is necessary for a server wanting to listen for connections on some port, as also for connectionless applications (eg UDP based)

  • On unbounded sockets an implicit bind is done with IN_ADDRANY and a random port as the address and port parameters respectively

CDAC-Mumbai


Listening for a connection

#include <sys/socket.h>

int listen(int sockfd, int backlog);

Returns: 0 if OK, -1 if error

Listening for a connection

listen() performs two duties:

It specifies to the kernel that it should passively listen for connections to the address associated to sockfd

It specifies the maximum number of pending connections the kernel should queue for this socket

CDAC-Mumbai


Accepting a connection

Accepting a connection

#include <sys/socket.h>

int accept(int sockfd, struct sockaddr* cliaddr, socklen_t* addrlen);

Returns: non-negative descriptor if OK, -1 on error

  • This function is called by the server to get the next connection from the queue of completed connections

  • If no connection has been completed, the function call blocks

  • A new socket descriptor is returned for the connection

  • The structure pointed to by cliaddr is filled in with information about the protocol address of the connected peer process(client), while *addrlen gives the size of this structure

CDAC-Mumbai


Functions for sending receiving data

Functions for sending/receiving data

#include <sys/types.h>

#include <sys/socket.h>

int write(int sockfd, char* buf, int nbytes);

/*Return value: No. of bytes sent if OK, -1 on error*/

int read(int sockfd, char* buf, int nbytes);

/*Return value: No. of bytes read if OK, 0 on connection closure,

-1 on error*/

CDAC-Mumbai


Some additional functions

Some additional functions

  • In network programming, we often need the help of some additional functions for tasks like:

    • Byte ordering

    • Byte operations

    • Address Conversions

CDAC-Mumbai


Byte ordering

Byte Ordering

Increasing memory address

Address A+1

Address A

Little-endian

byte order

High-order byte

Low-order byte

MSB 16-bit value LSB

Big-endian

byte order

High-order byte

Low-order byte

Address A

Address A+1

Increasing memory address

CDAC-Mumbai


Implications of byte order

Implications of Byte Order

  • Unfortunately there is no standard byte order- some systems are big endian, the others little endian

  • We refer to the byte ordering used by a given system as host byte order

  • The sender and the receiver must agree on the order in which the bytes of multi-byte field transmitted.

  • Hence we specify the network byte order, which is big-endian byte ordering

CDAC-Mumbai


Byte order functions

Byte Order Functions

#include <netinet/in.h>

#include <sys/types.h>

uint16_t htons(uint16_t host16bitvalue)

uint32_t htonl(uint32_t host32bitvalue)

Return: value in network byte order

uint16_t ntohs(uint16_t net16bitvalue)

uint32_t ntohl(uint32_t net32bitvalue)

Return: value in host byte order

CDAC-Mumbai


Byte manipulation functions

Byte Manipulation Functions

#include <string.h>

void *memset(void *s, int c, size_t n);

/*Return: Pointer to the memory area s */

void *memcpy(void *dest, const void *src, size_t n);

/*Return: Pointer to dest */

int memcmp(const void *s1, const void *s2, size_t n);

/* Return: <0, =0, >0, if first n bytes of s1 are respectively less than, equal to, or greater than the first n bytes of s2 */

CDAC-Mumbai


Address conversion functions

Address Conversion Functions

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

int inet_aton(const char* strptr, struct in_addr* addrptr);

/* return non-zero if string was valid,0 if error */

char* inet_ntoa(struct in_addr inaddr);

/* returns: pointer to dotted-decimal string */

CDAC-Mumbai


Address conversion functions1

Address Conversion Functions

#include <sys/types.h>

#include <sys/socket.h>

#include <arpa/inet.h>

int inet_pton(int family, const char *strpr, void *addrptr);

/* returns 1 if OK, 0 if input not a valid presentation format, -1 on other errors*/

const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len);

/*returns pointer to result if OK, NULL on error*/

CDAC-Mumbai


References

References

  • Unix Network Programming :Vol 1- Stevens

  • Network Programming for Microsoft Windows –Jones, Ohlund

  • Java Network Programming – Rusty, Harold

  • TCP/IP Illustrated - Stevens

CDAC-Mumbai


  • Login