Top Banner
Sockets CIS 370 Fall 2009, UMassD
59

Sockets CIS 370 Fall 2009, UMassD. Introduction Sockets provide a simple programming interface which is consistent for processes on the same machine.

Dec 25, 2015

Download

Documents

Emily Reed
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Sockets

CIS 370

Fall 2009, UMassD

Page 2: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Introduction

Sockets provide a simple programming interface which is consistent for processes on the same machine or across different machines.

The aim of UNIX sockets is to provide a means of IPC that is sufficiently generic to allow bi-directional messages between two processes irrespective of where they reside.

You need to add -lxnet when compiling.

Page 3: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Types of connection

Processes that send messages across a network can choose one of two ways to communicate.– The connection oriented model or virtual circuit– The connectionless oriented model.

Page 4: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The connection oriented model

The connection oriented model can be used by a process which needs to send an unformatted, uninterrupted stream of characters to the same constant destination.

For example - a remote login connection where a client system has a virtual connection to a server.

Like a phone network.

Page 5: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The connectionless oriented model

Suppose a servers wants to send a broadcast message to all its clients (and is not necessarily concerned that the clients get the message), the server can use a connectionless oriented model.

The process sends the message to a specified network address, it then send the next message to a different address.

Like USPS.

Page 6: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

General information

For any communication to take place between processes, the client and the server need to be connected: at the hardware level by network equipment such as cables, cards and devices such as routers, and at the software level by a standard set of network protocols.

UNIX uses TCP for connection and UDP for connectionless oriented models.

Page 7: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Addressing

When processes are communicating across a network they must know the network address of the machine that the processes are residing on.

The address essentially gives the physical location of a machine on a network.

Addresses are generally layered, representing the different levels of a network.

Page 8: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Internet addressing

– Across the world’s networks there is an almost universally accepted addressing standard - IP internet addressing.

– An IP address consists of four decimals separated by periods.

• 134.88.14.211 - uranium’s IP address

– At the programming level IP addresses are stored in an in_addr_t type.

Page 9: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

IP addresses

UNIX provides a routine that converts the four decimal number representation to the in_addr_t representation - inet_addr.

Usage#include <arpa/inet.h>

in_addr_t inet_addr(const char *ip_address);

*ip_address is in the form discussed earlier, the routine returns an address in the form of in_addr_t (a -1 if in error).

Page 10: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Ports

Once the address of the machine to access is known, the client process also needs to know the correct server processes to connect to.

To this end the server process sets and listens for connections on a specified port number.

Therefore a client process will ask for a connection to a specific machine and port.

Page 11: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Socket interface

Different types of sockets are used for different types of IPCs within the same machine or on different machines.

A specific form of socket for network communication is shown below:#include <netinet/in.h>struct sockaddr_in{

sa_family_t sin_family; // internet address family

in_port_t sin_port; // port number

struct in_addr sin_addr; // holds the IP address

unsigned char sin_zero[8]; // filling

}

Page 12: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Transport end point

In all forms of communication both the client and server must establish their own transport end points.

These are handles used to establish a link between processes across a network.

Creation of these are achieved using the socket system call.

Page 13: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The socket system call

Usage#include <sys/socket.h>

int socket(int domain, int type, int protocol);

domain tells the call where the socket is to be used. AF_INET - internet domain, AF_UNIX - same UNIX machine.

The type specifies connection (SOCK_STREAM) or connectionless (SOCK_DGRAM).

Protocol - SOCK_STREAM - TCP, SOCK_DGRAM - UDP

Page 14: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

A simple example

We will use all the system calls we have seen so far.

The client sends the server a stream of lower-case characters.

The server converts them to upper case and sends them back to the client.

The outline is presented with the gaps filled in….

Page 15: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 16: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 17: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 18: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Binding

The bind system call associates the true network address of a machine with a socket identifier.

Usage#include < sys/types.h>

#include <sys/socket.h>

int bind(int sockfd, const struct sockaddr *address, size_t add_len);

The first parameter, sockid, is the socket file descriptor, returned by the socket system call.

The second parameter is a pointer to a sockaddr structure. The last parameter holds the size of the actual socket

structure used.

Page 19: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Listening

After binding and before any client system can connect to the newly created server endpoint, the server must set itself up to wait for connections.

Usage#include <sys/sockets.h>

int listen (int sockfd, int queue_size);

The sockfd, is as before. The server can queue up to queue_size incoming connection requests.

Page 20: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Accepting

When the server receives a connect request from a client it has to create an entirely new socket to handle the specific communication.

The first socket is used only to establish communication.

Creation of a second socket is done the accept system call.

Page 21: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The accept system call

Usage#include <sys/types.h>

#include <sys/socket.h>

int accept(int sockfd, struct sockaddr *address, size_t *add_len);

The first parameter is the original sockfd. On completion, the return value is the new

socket id to be used for communication.

Page 22: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The address parameter is filled out with the information about the client. Since this is connection oriented model, it can NULL.

If it is not NULL, then the variable pointed to by the add_len should initially contain the length of the address structure described by address. On the return of the accept call, *add_len will hold the number of bytes actually copied.

Page 23: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 24: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 25: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 26: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Connecting to the client

To request a connection to a server process and machine, the client uses the connect system call.

Usage#include <sys/types.h>

#include <sys/socket.h>

int connect(int csockfd, const struct sockaddr *address, size_t add_len);

Page 27: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 28: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 29: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Sending and receiving data

As the sockets are set up as SOCK_STREAMS

both have different file descriptors that can be used for reading and writing.

In fact the read and write system calls can be used.

If extra options are needed, two new system calls can be used - send and recv.

Page 30: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The recv and send system calls Usage

#include <sys/types.h>

#include <sys/socket.h>

ssize_t recv(int sockfd, void *buffer, size_t length, int flags);

ssize_t send(int sockfd, const void *buffer, size_t length, int flags);

The recv call specifies the file descriptor to read the data from the buffer and the length of the buffer.

Page 31: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

As with read, recv returns the amount of data read.

The flags parameter affects the way in which the data can be received. They are:– MSG_PEEK - the process can look at the data

without actually ‘receiving’ it.– MSG_OOB - normal data is by passed and the

process only receives ‘out of bound data’, like a signal (SIGINT)

– MSG_WAITALL - the recv call will only return when the full amount of data is available.

Page 32: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

send behaves just like write if the flags parameter is set to 0. It sends the message contained in buffer to sockfd. The length parameter specifies the length of buffer.

As with recv, the flags, parameter affect the way that message is sent. They are:– MSG_OOB - send ‘out of bound’ data– MSG_DONTROUTE - the message will be sent

ignoring any routing conditions of the underlying protocol. Normally this means that the message will be sent via the most direct route rather than the quickest.

Page 33: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Continuing our example - the server process will be ...

Page 34: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 35: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 36: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 37: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Closing the connection

It is important to deal with closing the socket connection.

Since a socket is a two-way communication mechanism, it is impossible to predict whether a process will be trying to read or write when a break in the communication occurs.

Page 38: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

If a process attempts to write or send data to a socket which has become disconnected it will receive the SIGPIPE signal, which should be dealt with through a signal handler.

If read or recv returns zero this indicated end of file and therefore the connection, hence must be checked and handled accordingly.

The close system call can be used on socket. If the SOCK_STREAM type is being used the kernel will see to it that the data is sent before closing. I.E. the close is blocked till transfer is complete.

If the type SOCK_DGRAM is used then the socket is closed immediately.

Page 39: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 40: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 41: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 42: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 43: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 44: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 45: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 46: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 47: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Assignment 7

The server will provide the following services:

– sin_of_x(int)

– cos_of_x(int)

– tan_of_x(int)

– log_of_x(int)

– exp_of_x(int) Server will run on Uranium, client on any machine.

Connection-oriented model.

Page 48: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Programming the connectionless model

The main difference will be that in connectionless mode the packets transmitted between the client and server will arrive at their destination in an indeterminate order.

A process wishing to send or receive messages across a network must create its own local socket and bind its own network address to that socket.

Page 49: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The process can now effectively use this socket as a gateway out on to the network.

To send a message the process must know the destination address; this could be a ‘broadcast address’ covering many machine.

Page 50: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

Sending and receiving messages The only two new system calls in the

connectionless model are sendto and recvfrom.

Usagessize_t recvfrom(int sockfd, void *message,

size_t length, int flags, struct sockaddr *send_addr, size_t *add_len);

ssize_t sendto(int sockfd, const void *message, size_t length, int flags, struct sockaddr *send_addr, size_t *add_len);

Page 51: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The sockfd parameter in both calls specifies the locally bound socket.

The recvfrom call works exactly the same way as recv if send_addr is set to NULL.

The message pointer is the buffer into which the received message will be placed and the length is the number of bytes to be read into the message.

The last two parameters are those which help with the connectionless form of communiation.

Page 52: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The send_addr structure will be filled with the address information of the machine which sent the message. This means that the receiving process can send a reply if it wishes.

The final parameter is a pointer to a size_t integer which, on completion of the call, will be filled out with the length of the address.

In the sendto call dest_addr specifies the address of the peer for the message to be sent to and dest_len specifies the length of the address.

Page 53: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 54: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 55: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 56: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 57: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Page 58: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

The difference?

In both models the server has to create a socket and bind its local address to that socket.

In the connection model the server must then start to listen for the incoming connection. This step is not necessary in the connectionless model as the client will do more of the work.

Page 59: Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.

From a client perspective, in the connection model, the client just connects to the server. In the connectionless model, the client must create a socket and bind its local address to that socket.

Different system calls are normally used to transmit data.

The send and recv system calls can be used in both models.

The sendto and recvfrom calls are normally used in the connectionless model so that the server can retrieve information about the sender of the message and reply accordingly.