Informatica

Páginas: 5 (1160 palabras) Publicado: 20 de septiembre de 2012
Socket Programming

Kameswari Chebrolu
Dept. of Electrical Engineering, IIT Kanpur

What is a socket?
Socket: An interface between an application process
and transport layer
The application process can send/receive messages to/from
another application process (local or remote)via a socket

In Unix jargon, a socket is a file descriptor – an integer
associated with an open file
Typesof Sockets: Internet Sockets, unix sockets,
X.25 sockets etc
Internet sockets characterized by IP Address (4 bytes) and
port number (2 bytes)

Socket Description

Types of Internet Sockets
Stream Sockets (SOCK_STREAM)
Connection oriented
Rely on TCP to provide reliable two-way connected
communication

Datagram Sockets (SOCK_DGRAM)
Rely on UDP
Connection is unreliable

BackgroundTwo types of “Byte ordering”

Connection Oriented Protocol
Server

Network Byte Order: High-order byte of the number is stored
in memory at the lowest address
Host Byte Order: Low-order byte of the number is stored in
memory at the lowest address
Network stack (TCP/IP) expects Network Byte Order

Client

socket()
bind()

socket()

listen()

connect()

accept()

Conversions:htons() - Host to Network Short

send()

recv()

htonl() - Host to Network Long

recv()

send()

ntohs() - Network to Host Short

close()

close()

ntohl() - Network to Host Long

Connectionless Protocol
Server

Client

socket()
bind()

int socket(int domain, int type, int protocol);
domain should be set to AF_INET

socket()
bind()

recvfrom()

sendto()sendto()

recvfrom()

close()

socket() -- Get the file descriptor

close()

type can be SOCK_STREAM or SOCK_DGRAM
set protocol to 0 to have socket choose the correct protocol
based on type
socket() returns a socket descriptor for use in later system
calls or -1 on error

socket structures
struct sockaddr: Holds socket address information for
many types of sockets
struct sockaddr{
unsigned short sa_family; //address family AF_xxx
unsigned short sa_data[14]; //14 bytes of protocol addr
}

struct sockaddr_in: A parallel structure that makes it
easy to reference elements of the socket address
struct sockaddr_in {
short int
unsigned short int
struct in_addr
unsigned char
}

sin_family;
sin_port;
sin_addr;
sin_zero[8];

// set to AF_INET
// Port number
//Internet address
//set to all zeros

bind() - what port am I on?
Used to associate a socket with a port on the local machine
The port number is used by the kernel to match an incoming
packet to a process

int bind(int sockfd, struct sockaddr *my_addr, int addrlen)

Dealing with IP Addresses
int inet_aton(const char *cp, struct in_addr *inp);
Example usage:
struct sockaddr_in my_addr;my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
inet_aton(“10.0.0.5”,&(my_addr.sin_addr));
memset(&(my_addr.sin_zero),'\0',8);
inet_aton() gives non-zero on success and zero on failure
To convert binary IP to string: inet_noa()
printf(“%s”,inet_ntoa(my_addr.sin_addr));

connect() - Hello!
Connects to a remote host
int connect(int sockfd, struct sockaddr *serv_addr, intaddrlen)

sockfd is the socket descriptor returned by socket()

sockfd is the socket descriptor returned by socket()

my_addr is pointer to struct sockaddr that contains information
about your IP address and port

serv_addr is pointer to struct sockaddr that contains
information on destination IP address and port

addrlen is set to sizeof(struct sockaddr)

addrlen is set tosizeof(struct sockaddr)

returns -1 on error

returns -1 on error

my_addr.sin_port = 0; //choose an unused port at random
my_addr.sin_addr.s_addr = INADDR_ANY; //use my IP addr

At times, you don't have to bind() when you are using
connect()

listen() - Call me please!
Waits for incoming connections
int listen(int sockfd, int backlog);
sockfd is the socket file descriptor returned by...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Informatica
  • Informatica
  • Informatica
  • Informatica
  • Informatica
  • Informática
  • Informatica
  • Informatica

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS