advanced sockets api-ii

23
Advanced Sockets API- II Vinayak Jagtap [email protected]

Upload: trung

Post on 15-Jan-2016

30 views

Category:

Documents


1 download

DESCRIPTION

Advanced Sockets API-II. Vinayak Jagtap [email protected]. UDP Sockets. Socket Functions for UDP client/server. Server. Client. socket(). socket(). bind(). sendto(). Data (request). recvfrom(). Process request. Data (reply). sendto(). recvfrom(). close(). Application details. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced Sockets API-II

Advanced Sockets API-II

Vinayak [email protected]

Page 2: Advanced Sockets API-II

UDP Sockets

Page 3: Advanced Sockets API-II

Socket Functions for UDP client/server

socket()

bind()socket()

sendto()

recvfrom()

close()

recvfrom()

sendto()

Data (request)

Data (reply)

Client Server

Process request

Page 4: Advanced Sockets API-II

Application details

Applications will have to do for themselves Acknowledgement of packets Flow control Sequencing

Page 5: Advanced Sockets API-II

Function for sending data

#include <sys/types.h>#include <sys/socket.h>

int sendto(int sockfd, char* buf, int nbytes, int flags,struct sockaddr* to, int addrlen);Return value: No. of bytes sent if OK, -1 on error

Flags argument is either 0 or is formed by OR’ing some constants like:MSG_DONTROUTE Bypass routingMSG_DONTWAIT Enable non-blocking operation

Page 6: Advanced Sockets API-II

Function for receiving data

#include <sys/types.h>#include <sys/socket.h>

int recvfrom(int sockfd, char* buf, int nbytes, int flags,struct sockaddr* from, int* addrlen);

Return value: No. of bytes read if OK, -1 on error

Flags argument is either 0 or is formed by OR’ing some constants like:MSG_PEEK Peek at data present on socketMSG_DONTWAIT Enable non-blocking operation

Page 7: Advanced Sockets API-II

Simple Echo client/server using UDP

UDP Client UDP Server

sendto

recvfromsendto

recvfromfgets

fputs

Page 8: Advanced Sockets API-II

Features of the UDP server

Echo function never terminates. Iterative Server

Page 9: Advanced Sockets API-II

connect() for UDP!

Connect can be done on a udp socket to specify destination address and portDoes not do any TCP like connectionsend can be used instead of sendto as destination address is known.Receive operations will accept data only from the specified source, and Send operations can send only to the specified destination.Disconnection can be done by calling connect() with family member (eg sin_family) set to AF_UNSPEC

Page 10: Advanced Sockets API-II

connect()

Advantages:

Is more efficient (due to avoidance of multiple internal connects)Asynchronous errors (e.g., ICMP errors) can be detected.

Page 11: Advanced Sockets API-II

Connected UDP Socket

Application

UDP

connected peer

UDP

Datagram from other IP

read write

Page 12: Advanced Sockets API-II

Getsockbyname()

#include <sys/socket.h> int getsockname(int s, struct sockaddr *name, socklen_t

*namelen);

Getsockname returns the current name for the specified socket.

On success, zero is returned. On error, -1 is returned, and errno is

set appropriately.

ERRORS EBADF The argument s is not a valid descriptor. ENOTSOCK The argument s is a file, not a socket. ENOBUFS Insufficient resources available in the system to

perform the operation. EFAULT The name parameter points to memory not in a valid

part of the process address space.

Page 13: Advanced Sockets API-II

UDP

When to use? Applications use broadcasting or

multicasting Cost of connection establishment is high

compared to data transferred

When not to use? Flow control is very important Packet sequence has to be maintained

E.g. DNS name query, SNMP

Page 14: Advanced Sockets API-II

Timeouts

SIGALARM

Page 15: Advanced Sockets API-II

Extended I/O Functions

int send(int sockfd, const void *msg, size_t len, int flags)

int recv(int sockfd, const void *buff, size_t len, int flags)

MSG_OOB 0x1 process out-of-band dataMSG_DONTROUTE 0x4 bypass routing, use direct interfaceMSG_DONTWAIT 0x40 don't blockMSG_NOSIGNAL 0x2000 don't raise SIGPIPE

Page 16: Advanced Sockets API-II

Extended I/O functions

#include <sys/uio.h>

int readv(int fd, const struct iovec * vector, int count);

int writev(int fd, const struct iovec * vector, int count);

They return number of bytes read/written on success, -1 on failure

struct iovec { void* iov_base; /* Starting address. */ size_t iov_len; /* Length in bytes. */ }

Page 17: Advanced Sockets API-II

Extended I/O functions#include <sys/socket.h>int recvmsg(int sockfd, struct msghdr *msg, int flags);int sendmsg(int sockfd, const struct msghdr *msg, int flags);They return number of bytes read/written on success, -1 on failure

struct msghdr { void * msg_name; /* protocol address */ socklen_t msg_namelen; /* size of address */ struct iovec * msg_iov; /* scatter/gather array */ size_t msg_iovlen; /* # elements in msg_iov */ void * msg_control; /* ancillary data */ /*aligned for struct cmsghdr*/

socklen_t msg_controllen; /* ancillary data buffer len */ int msg_flags; /* flags on received message */ };

Page 18: Advanced Sockets API-II

UNIX Domain Protocols

A way of performing client-server communication on a single host using the sockets (or XTI) API They are an alternative to IPC

Page 19: Advanced Sockets API-II

UNIX Domain Protocols

Reasons to use:It is better to use them for networked applications residing on the same host because they increase the efficiency of I/O. They are light weight in comparison with the TCP/IP stack. E.g.: X-Windows applicationsNewer implementations provide the client’s credentials (user ID and group ID) to server

Page 20: Advanced Sockets API-II

UNIX Domain Socket Address Structure

In <sys/un.h>struct sockaddr_un {

sa_family_t sun_family; /* AF_LOCAL */ char sun_path[104]; /* null

terminated path name*/

};

Page 21: Advanced Sockets API-II

UNIX Domain Protocols

Family is AF_LOCAL/AF_UNIXA pathname is chosen in the address information

Page 22: Advanced Sockets API-II

UNIX Domain Protocols

Types of sockets provided:Stream socketsDatagram socketsRaw sockets (poorly documented)

Page 23: Advanced Sockets API-II

References

Unix Network Programming, Volume I W. Richard Stevens