Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine."— Presentation transcript:

1 Sockets CIS 370 Fall 2009, UMassD

2 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.

3 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.

4 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.

5 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.

6 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.

7 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.

8 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. - uranium’s IP address –At the programming level IP addresses are stored in an in_addr_t type.

9 IP addresses  UNIX provides a routine that converts the four decimal number representation to the in_addr_t representation - inet_addr.  Usage #include 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).

10 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.

11 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 struct sockaddr_in{ sa_family_tsin_family; // internet address family in_port_tsin_port; // port number struct in_addrsin_addr; // holds the IP address unsigned charsin_zero[8]; // filling }

12 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.

13 The socket system call  Usage #include 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

14 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….




18 Binding  The bind system call associates the true network address of a machine with a socket identifier.  Usage #include 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.

19 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 int listen (int sockfd, int queue_size);  The sockfd, is as before. The server can queue up to queue_size incoming connection requests.

20 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.

21 The accept system call  Usage #include 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.

22  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.




26 Connecting to the client  To request a connection to a server process and machine, the client uses the connect system call.  Usage #include int connect(int csockfd, const struct sockaddr *address, size_t add_len);



29 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.

30 The recv and send system calls  Usage #include 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.

31  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.

32  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.

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




37 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.

38  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.









47 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.

48 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.

49  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.

50 Sending and receiving messages  The only two new system calls in the connectionless model are sendto and recvfrom.  Usage ssize_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);

51  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.

52  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.






58 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.

59  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.

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

Similar presentations

Ads by Google