Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Sockets Introduction-Network Programming-Lecture Slides, Slides of Network Programming

This lecture was delivered by Dr. Mstan Veer at Gautam Buddha University for Network Programming course. It includes: SOckets, Introduction, Address, Structure, Argument, Pointer, Protocol, Value-result, Byte, Ordering

Typology: Slides

2011/2012

Uploaded on 07/06/2012

umi
umi 🇮🇳

4.5

(13)

63 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Socket Introduction 1
Sockets Introduction
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Sockets Introduction-Network Programming-Lecture Slides and more Slides Network Programming in PDF only on Docsity!

Socket Introduction 1

Sockets Introduction

Socket Address Structure

• Socket = IP address + TCP or UDP port

number

• Used in a socket function as a argument

(as pointer).

• IP address, TCP or UDP port number,

length of structure .....

• Each protocol define its own Socket

Address Structure(IPv4, IPv6....)

Generic Socket address structure

  • <sys/socket.h> : Generic Socket address structure

struct sockaddr { uint8_t sa_len; sa_family_t sa_family; /address family: AF_xxx value/ char sa_data[14]; /protocol specific address/ }**

  • int bind(int , struct sockaddr * , socklen_t);

struct sockaddr_in serv; /IPv4 socket address structure/ /* fill in serv{} */ bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));

Generic Socket address structure

Comparison of socket address structure

IPv4 Header

IPv

Address

Value-Result Argument

• socket address structure is always passed by

reference to socket functions.

• The length of the structure is also passed.

• The way the length is passed depends on

which direction the structure is being passed

Socket address structure pass.

Accept, recvfrom, getsockname, getpeername

length Socket Address structure

Kernel

int *

User

process

value result

Protocol address

  • Process to kernel

struct sockaddr_in serv

/* fill in serv{} */

connect(sockfd, (SA *)&serv,

sizeof(serv));

  • Kernel to process

struct sockaddr_un cli

socklen_t len;

len = sizeof(cli);

getpeername(unixfd,

(SA*)&cli,&len);

/* len is filled in by the kernel. */

Figure3.9 determine host byte order

**int main(int argc, char argv) { union {short s; char c[sizeof(short)]; } un; un.s = 0x0102; printf("%s: ", CPU_VENDOR_OS); if (sizeof(short) == 2) { if (un.c[0] == 1 && un.c[1] == 2) printf("big-endian\n"); else if (un.c[0] == 2 && un.c[1] == 1) printf("little-endian\n"); else printf("unknown\n"); } else printf("sizeof(short) = %d\n", sizeof(short)); exit(0); }

Byte Manipulation Functions

  • #include <strings.h>

void bzero(void dest, size_t nbytes); / sets the specified no of bytes to zero */

void bcopy(const void *src, void dest, size_t nbytes); / byte by byte copy from source to destination */

int bcmp(const void *ptr1, const void ptr2, size_t nbytes); / return 0 if equal, nonzero if unequal */

inet_aton,inet_addr, inet_ntoa

functions

  • Convert internet address between ASCII string

and network byte ordered binary values(as stored

in socket address structure)

  • Used for IPv4 addresses conversion
  • For both IPv4 and IPv6 we have : inet_pton ,

inet_ntop

  • #include<arpa/inet.h>

For ASCII to network binary:

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

/* return : 1 if string was valid,0 on error */

For ASCII to network binary:

in_addr_t inet_addr(const char *strptr);

/* deprecated. return : 32bit binary network byte ordered IPv4 address; INADDR_NONE if error */

For network binary to ASCII:

char *inet_ntoa(struct in_addr inaddr);

/return pointer to dotted-decimal string/