You are on page 1of 17

SOCKET PROGRAMMING

9/1/15

WHAT IS SOCKET?
2

An interface between an application process and transport layer.


The application creates a socket,
The socket type dictates the style of communication
connection-oriented vs. connectionless
Once configured the application can

pass data to the socket for network transmission


receive data from the socket (transmitted through the network
by some other host)

9/1/15

SOCKET PRIMITIVES
3

socket()
2. bind()
3. listen()
4. accept()
5. connect()
6. send()
7. recv()
8. sendto()
9. recvfrom()
10. close()
1.

9/1/15

TCP- Primitives
SERVER

CLIENT

SOCKET()
BIND()
LISTEN()

SOCKET()

CONNECT()

ACCEPT()
SEND()

RECV()

RECV()

SEND()

CLOSE()

CLOSE()

9/1/15

TCP- connection oriented


5

There are a few steps involved in using sockets:


Create the socket
Identify the socket
On the server, wait for an incoming connection
On the client, connect to the server's socket
Send and receive messages
Close the socket

9/1/15

Create a Socket
6

int s = socket(domain, type, protocol)


domain, or address

family: communication domain in which the socket should be created. They are
AF_INET (IP), AF_INET6 (IPv6), AF_UNIX (local channel, similar to pipes), AF_ISO (ISO protocols),
etc.

type type of services: selected according to

the properties required by the application:


SOCK_STREAM (virtual circuit service), SOCK_DGRAM (datagram service), SOCK_RAW (direct IP
service).

protocol

indicate a specific protocol to use in supporting the sockets operation. It is set to Zero ..

WHY??
The return

value is a file descriptor (a small integer).

For TCP/IP sockets, we

want to specify the IP address family (AF_INET) and datagram service


(SOCK_STREAM). Since there's only one form of stream service, there are no variations of the
protocol, so the last argument, protocol, is zero.
9/1/15

Naming a Socket
7

Assigning a transport address to the socket (a port number

in IP networking). This operation is called binding an


address and the bind system call is used.

int bind(int socket, struct sockaddr *address,


int addrlen);
socket: is the socket descriptor returned by socket()
address: pointer to struct sockaddr that contains info about
IP address and Port
addrlen: is set to sizeof (struct sockaddr)
9/1/15

Struct sockaddr *address..?


8
The transport address is defined in a socket address structure.
Sockets were designed to work with various different types of communication

interfaces, the interface is very general.


Instead of accepting, say, a port number as a parameter, it takes a sockaddr structure.

struct sockaddr_in
{
short int sin_family;
unsigned short int sin_port;
struct in_addr sin_addr;
unsigned char sin_zero[8];
}
9/1/15

Before calling bind()


9

sin_family: The address family we used when we


set up the socket. In our case, it's AF_INET.
sin_port: The port number (the transport
address). Can be assigned explicitly or set it to zero
for OS to assign it.
sin_addr: This is just your machine's IP address.
we let the OS assign the address by specifying
0.0.0.0, which is constant INADDR_ANY.

9/1/15

Connect()
10

int connect(int socket, const struct sockaddr

*address, int address_len);


socket: is the descriptor returned by socket()
*address: is pointer to struct sockaddr that contains
information on destination IP address and port
address_len: set to sizeof(struct sockaddr)

9/1/15

Listen()
11

Waits for incoming connections


int listen(int sockfd, int backlog);
sockfd is the socket file descriptor returned by socket()
backlog is the number of connections allowed on the
incoming queue
listen() returns -1 on error
Need to call bind() before you use listen()

9/1/15

Accept()
12

accept() gets the pending connection on the port you are


listening on
int accept(int sockfd, void *addr, int *addrlen);
sockfd is the listening socket descriptor
information about incoming connection is stored in addr
which is a pointer to a local struct sockaddr_in
addrlen is set to sizeof(struct sockaddr_in)

9/1/15

Sending messages
13

The function is for sending stream data over stream sockets


int send(int sockfd, const void *msg, int len, int
flags);
- sockfd: is the socket descriptor you want to send data to

(returned by socket() or got from accept())


- msg: is a pointer to the data you want to send
- len :is the length of that data in bytes
- set flags to 0 for now
- sent() returns the number of bytes actually or -1 on error

9/1/15

Receiving Messages
14

The function is for receiving stream data over stream


sockets
int recv(int sockfd, void *buf, int len, int flags);
- sockfd: is the socket descriptor to read from
- buf :is the buffer to read the information into
- len: is the maximum length of the buffer
- set flags to 0 for now
- recv() returns the number of bytes actually read into
the buffer or -1 on error
- If recv() returns 0, the remote side has closed
connection on you

9/1/15

Closing the Socket


15

int close(int socket);

Closes connection corresponding to the socket descriptor


and frees the socket descriptor
Will prevent any more sends and recvs

9/1/15

Some Number Conversions


16
These convert four-byte and two-byte numbers into network representations.
Integers are stored in memory and sent across the network as sequences of bytes.

htons : convert a number into a 16-bit network representation. Used to


store a port number into a sockaddr structure.
htonl: convert a number into a 32-bit network representation.
Used to store an IP address into a sockaddr structure.
ntohs : convert a 16-bit number from a network representation into the
local processor's format. Used to read a port number from
a sockaddr structure.
ntohl : convert a 32-bit number from a network representation into the
local processor's format. Used to read an IP address from
a sockaddr structure.
9/1/15

17

Lets have a LOOK at CODE

9/1/15