[go: up one dir, main page]

0% found this document useful (0 votes)
13 views21 pages

Unit2 1

The document provides an introduction to socket programming, detailing the socket interface as a protocol-independent means for applications to communicate over networks. It covers the typical client-server interaction using TCP, the process of creating and closing sockets, and functions for establishing connections, sending and receiving data. Additionally, it discusses byte ordering, address conversion functions, and references for further reading on network programming.

Uploaded by

sharada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

Unit2 1

The document provides an introduction to socket programming, detailing the socket interface as a protocol-independent means for applications to communicate over networks. It covers the typical client-server interaction using TCP, the process of creating and closing sockets, and functions for establishing connections, sending and receiving data. Additionally, it discusses byte ordering, address conversion functions, and references for further reading on network programming.

Uploaded by

sharada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

shri

Introduction to Sockets

1
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
2
Typical Client-Server interaction using
TCP
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

Data (reply) write()


read()

close()
read()
End-of-file notification

close() 3
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

4
Closing a socket
#inlcude <unistd.h>

int close(int sockfd);

Returns: 0 if OK, –1 on error

The transport layer generally tries to flush


out, i.e. send, any remaining data, when a
close is done.

5
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)

6
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
7
Structure(POSIX)
struct sockaddr {
sa_family_t sa_family; /*Address Family, AF_XXX*/
char sa_data[14]; /*14 bytes of protocol address*/
};

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 */
};
8
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)
9
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

10
Listening for a connection
#include <sys/socket.h>

int listen(int sockfd, int backlog);

Returns: 0 if OK, -1 if error

listen() performs two duties:


1) It specifies to the kernel that it should
passively listen for connections to the
address associated to sockfd
2) It specifies the maximum number of pending
connections the kernel should queue for this
socket
11
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

12
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*/

13
Some additional functions
 In network programming, we often
need the help of some additional
functions for tasks like:
 Byte ordering
 Byte operations
 Address Conversions

14
Byte Ordering
Increasing memory address

Address A+1 Address A


Little-endian
High-order byte Low-order byte
byte order

MSB 16-bit value LSB

Big-endian
byte order High-order byte Low-order byte
Address A Address A+1
Increasing memory address
15
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
16
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

17
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 */

18
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 */

19
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*/

20
References
 Unix Network Programming :Vol 1-
Stevens
 Network Programming for
Microsoft Windows –Jones, Ohlund
 Java Network Programming –
Rusty, Harold
 TCP/IP Illustrated - Stevens

21

You might also like