You are on page 1of 18

Socket Interface

Basic Sockets API Review


DHCP, Mail, WWW, TELNET, FTP... Socket Library TCP ARP RARP IP PPP Com UDP ICMP
Layer 4 / Transport

Application

Layer 3 / Network Layer 2 / Data Link Layer 1 / Physical


2

Ethernet Network card

Sockets

process sends/receives messages to/from its socket socket analogous to door

host or server controlled by app developer

host or server

process socket TCP with buffers, variables

process socket

sending process shoves message out door sending process relies on transport infrastructure on other side of door which brings message to socket at receiving process

Internet

TCP with buffers, variables

Basic socket calls for a client


Socket() sockaddr_in{ } bind() Local addr sockaddr_in{ } connect() peer addr

recv()

send()

close()

Socket( ) system call


#include <sys/socket.h> /*UNIX*/ #include <winsock2.h> /*Windows*/ SOCKET socket(int domain, int type, int protocol) Returns: socket descriptor on success, INVALID_SOCKET on failure

The socket API is protocol independent It can support several different communication domains domain parameter

AF_INET (internet) AF_LOCAL (or AF_UNIX) domain SOCK_STREAM SOCK_DGRAM SOCK_RAW (access IP packet) In the TCP/IP, the parameter is set to zero
5

type parameter indicates the type of socket to be created


protocol field indicates which protocol should be used with the socket

Demultiplexing
application application application application

ICMP

IGMP

TCP

UDP

ARP

IP Ethernet

RARP

incoming frame
6

bind( ) system call


#include <sys/socket.h> /*UNIX*/ #include <winsock2.h> /*Windows*/ int bind(SOCKET s, const struct sockaddr *name, int namelen); Return: 0 on success, SOCKET_ERROR on error

Specify local IP address and local port for a socket Can use INADDR_ANY for any IP address when the host is multi-home host s parameter

Socket descriptor

*name and namelen parameters are used to supply the port and IP address of the local AP

*name points to the socket address data structure namelen indicate the length of socket address data structure
7

Sockaddr data abstraction

The sockaddr interface uses data abstraction. Thus, while the protocol domain may change, the interface remain the same
Struct sockaddr_in{ sa_family_t unsigned short int structin_addr unsigned char }; sin_family; sin_port; sin_addr; _pad[ ]

Struct sockaddr{ unsigned short int sa_family; unsigned char sa_data[14]; };

Sa_family and sin_family are common between the two structures The domain type in the socket() function must be the same value as the family.
8

Connect( ) system call


#include <sys/socket.h> /*UNIX*/ #include <winsock2.h> /*Windows*/ int connect(SOCKET s, const struct sockaddr *peer, int peer_len); Return: 0 on success, nonzero on failure

Used to establish the connection *peer parameter specifies servers address and port number Used by client

Used in connection-oriented TCP: Forms a TCP connection, server uses accept to receive the call Used in connectionless UDP: record the servers address in the socket.
9

send(), sendto(), sendmsg() system call


#include <sys/socket.h> /*UNIX*/ #include <winsock2.h> /*Windows*/ int send(SOCKET s, void *buf, size_t len, int flags); Int sendto (SOCKET s, const void *buf, size_t len, int flags, const struct sockaddr *to, int tolen) Return: 0 on success, nonzero on failure

Send, sendto, and sendmsg


Transfer outgoing data from application send () is used in the socket which is connected sendto() and sendmsg() use to send a message using an unconnected socket

Sendmsg (socket, msgstruct, flags)

Perform the same operation as sendto, but abbreviates the arguments by defining a structure

Flags

MSG_OOB

Cause urgent data to be sent or read Peek at incoming data without removing it from the receive buffer Cause the kernel to bypass the normal routing function
10

MSG_PEEK MSG_DONTROUTE

recv(), recvfrom(), recvmsg() system call


#include <sys/socket.h> /*UNIX*/ #include <winsock2.h> /*Windows*/ int recv (SOCKET s, void *buf, size_t len, int flags); Int recvfrom (SOCKET s, void *buf, size_t len, int flags, struct sockaddr *from, int fromlen) Return: number of bytes transferred on success, -1 on failure

Recv, recvfrom, and recvmsg Transfer incoming data to application recv() uses to receive data from a connected socket recvfrom() and recvmsg() is used to receive data from unconnected socket, receive data from arbitrary set of clients Read and write with sockets such as read and write for I/O

Used with connected sockets Read (descriptor, buffer, length)

Descriptor may correspond to a file or a socket (remote)

11

Figure 13 a simple TCP client


#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> int main( void ) { struct sockaddr_in peer; int s; int rc; char buf[ 1 ]; peer.sin_family = AF_INET; peer.sin_port = htons( 7500 ); peer.sin_addr.s_addr = inet_addr( "127.0.0.1" ); s = socket( AF_INET, SOCK_STREAM, 0 ); if ( s < 0 ) { perror( "socket call failed" ); exit( 1 ); }

12

Figure 13 a simple TCP client (cont)


rc = connect( s, ( struct sockaddr * )&peer, sizeof( peer ) ); if ( rc ) { perror( "connect call failed" ); exit( 1 ); } rc = send( s, "1", 1, 0 ); if ( rc <= 0 ) { perror( "send call failed" ); exit( 1 ); } rc = recv( s, buf, 1, 0 ); if ( rc <= 0 ) perror( "recv call failed" ); else printf( "%c\n", buf[ 0 ] ); exit( 0 ); }
13

Basic socket calls in a server


Socket() sockaddr_in{ } bind() Local addr

listen() sockaddr_in{ } accept() peer addr

recv()

send()

close()
14

listen() system call


#include <sys/socket.h> /*UNIX*/ #include <winsock2.h> /*Windows*/ int listen(SOCKET s, int backlog); Return: 0 on success, SOCKET_ERROR on error

Used by server, TCP is in passive mode, UDP server dont require Prepares socket to accept incoming connections backlog parameter is the length of the server request queue (connection request queue)

15

accept() system call


#include <sys/socket.h> /*UNIX*/ #include <winsock2.h> /*Windows*/ SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen); Return: A connected socket if OK, INVALID_SOCKET on failure

Used by TCP server Waits for next connection establish and returns new socket Use the newsock to communication with this client Returns the address of the new connections peer in the sockaddr_in structure pointed to by *addr
16

A simpler TCP server


#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <stdio.h> int main( void ) { struct sockaddr_in local; int s; int s1; int rc; char buf[ 1 ]; local.sin_family = AF_INET; local.sin_port = htons( 7500 ); local.sin_addr.s_addr = htonl( INADDR_ANY ); s = socket( AF_INET, SOCK_STREAM, 0 ); if ( s < 0 ) { perror( "socket call failed" ); exit( 1 ); }

17

A simpler TCP server (cont)


rc = bind( s, ( struct sockaddr * )&local, sizeof( local ) ); if ( rc < 0 ) { perror( "bind call failure" ); exit( 1 ); } rc = listen( s, 5 ); if ( rc ) { perror( "listen call failed" ); exit( 1 ); } s1 = accept( s, NULL, NULL ); if ( s1 < 0 ) { perror( "accept call failed" ); exit( 1 ); } rc = recv( s1, buf, 1, 0 ); if ( rc <= 0 ) { perror( "recv call failed" ); exit( 1 ); } printf( "%c\n", buf[ 0 ] ); rc = send( s1, "2", 1, 0 ); if ( rc <= 0 ) perror( "send call failed" ); exit( 0 ); }

18

You might also like