Docsity

Sockets - Lecture Notes - Computer Networks | ECS 152B, Study notes of Computer Systems Networking and Telecommunications

Partial preview of the text

Download Sockets - Lecture Notes - Computer Networks | ECS 152B and more Study notes Computer Systems Networking and Telecommunications in PDF only on Docsity!

Lecture 11: Sockets

n Prev. summary

n Link

n Network

n Transport

Transport

Network

Link

Application

n Today’s lecture

n Sockets

(in between Application

and Transport)

What is a Socket?

Socket: door between application layer

process & end-end

transport layer protocol

process

Transport layer

socket

controlled by application developer controlled by operating system host

process

Transport layer

socket

controlled by application developer controlled by operating system host

internet

process

kernel

Application Layer Creates/Uses

Sockets

two processes (local or

remote) communicate by

sending data into socket &

Transport reading data out of socket

Network

Link

Application

socket

IP address port number

“port number” - allows receiving host to determine to which local process the message should be delivered

user space

kernel space

process =

Socket-programming

Half-associations:

<protocol, local_addr, local_port>

<protocol, foreign_addr, foreign_port>

Complete Socket Specification :

<protocol, local_addr, local_port, foreign_addr, foreign_port>

<..., 128.56.37.5, 1490, 128.56.86.73, 2302>

process: IP address port number

Socket programming with TCP

Client

n creates local TCP socket

n specifies IP address, port number of server process

n client initiates messaging

Server n server must be up & running n server must have created the socket that welcomes client’s contact n When contacted by client, server TCP creates new socket for server process to communicate with client (accept) n allows server to talk with multiple clients

TCP provides reliable, in-order transfer of bytes between client and server

application viewpoint

Socket programming with TCP

Example:

n client reads line from standard input ( inFromUser stream) , sends to server via socket ( outToServer stream)

n server reads line from socket

n server sends back a welcome message to client

n client reads, prints line from socket ( inFromServer stream)

Input stream: sequence of bytes into process Output stream: sequence of bytes out of process

client socket

inFromUser outToServerinFromServer

Socket Functions

socket

connect

bind

listen

accept

close

socket socket

client server

communication end-point

communication end-point

The socket interface

provides generalized

functions that support

network communication

using many possible

protocols

e.g.

Socket Function

socket() : Create a descriptor in the socket

descriptor table for use in

network communication

returns an integer (socket descriptor),- if error

socket descriptor table

pointers to other socket structures

0 : 1 :2 : 3 :4 :

socket data structure

socket(..) close(..)

Socket Function Parameters

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

family description AF_INET AF_INET

IPv4 protocols IPv6 protocols AF_LOCAL Unix domain protocols

AF_KEY

AF_ROUTE Routing sockets Key socket

type description SOCK_STREAM stream socket SOCK_DGRAM datagram socket SOCK_RAW raw socket

  • AF_xxx vs PF_xxx : AF (Address Family) and PF (Protocol Family) values of a protocol are the same; (historic intention)

AF_KEY

UDP

TCP

IPv

UDP

TCP

IPv

yes

yes

yes yes

SOCK_STREAM SOCK_DGRAM SOCK_RAW

possible combinations AF_INET^ AF_INET6 AF_LOCALAF_ROUTE of family and type :

Bind Function

Bind function is used to assign a local protocol address to a socket

  • uses a socket descriptor (sockfd) created by the socket function
  • uses protocol-specific address and its size (myaddr, addrlen)

#include <sys/socket.h>

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

Returns 0 if OK,-1 if error

Bind Function

IP address Port wildcard* wildcard*

0 nonzero local IP addr 0 process specifies IP address, kernel chooses port

Result

kernel chooses IP address, process specifies port

kernel chooses IP address and port

Process specifies

  • binding IP address to a socket:
    • TCP server: bind restricts the socket to receive connections destined only to that IP address (i.e., has multiple IP addresses).
    • TCP client: assigns source IP address for sending datagrams

local IP addr nonzero process specifies IP address and port

*wildcard: INADDR_ANY (IPv4)

Listen Function

socket

active socket passive socket

a socket used for initiating a connection

a socket used to wait for an incoming connection

Listen function:

  • converts an unconnected socket into a passive socket indicating that the kernel should accept incoming connection requests to this socket (a created socket is assumed to be active: i.e., a connect will follow)
  • is called only by a TCP server. Why?

(default)

Accept Function

Accept function returns the next completed connection from the front of

the completed connection queue.

  • If the completed connection queue is empty, process is put to sleep
  • if accept is succesful, it returns a new socket descriptor (newsockfd)

socket socket

communication end-point

communication end-point

#include <sys/socket.h>

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

Returns nonnegative descriptor if OK,-1 if error

Connect Function

Connect function is used by a TCP client to establish a connection

with a TCP server (connection-oriented TCP):

  • uses a socket descriptor (sockfd) created by the socket function
  • uses IP address and port number of the server (servaddr)

Connect function:

#include <sys/socket.h>

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

Returns 0 if OK,-1 if error