Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sockets.

Similar presentations


Presentation on theme: "Sockets."— Presentation transcript:

1 Sockets

2 Sockets API Open APIs for communication Assumes any transport
Much broader than Java sockets

3 Sockets API C APIs, but need to vary data structures
Provide ‘base’ structures that are place holders for ‘sub’ structures holding data Example – sockaddr, sockaddr_in, sockaddr_un, etc. struct sockaddr { sa_family_t sa_family; /* address family */ char sa_data[14]; }; struct sockaddr_in { sa_family_t sin_family; /* Address family */ in_port_t sin_port; /* Port number */ struct in_addr sin_addr; /* Internet address */ /* Pad to size of `struct sockaddr'. */ unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) - sizeof(unsigned short int) - sizeof(struct in_addr)];

4 Using Sockets Client Server socket() creates the socket
connect() connects socket to specified location (file, port, etc.) send()/recv() communicate with bytes close() close the socket connection Server bind() bind the socket to a name (file, port, etc.) listen() wait for requests

5 Socket Functions socket(int domain, int type, int protocol)
Domains, types, and protocols defined in sys/socket.h Domain specifies the protocol family which will be used AF_UNIX local to host (e.g. pipe) AF_INET inter-network – UDP/TCP Others that are not so relevant today Type specifies the semantics of the communication SOCK_STREAM, SOCK_DGRAM, SOCK_RAW Protocol specifies the particular protocol used with the socket IPPROTO_TCP (if AF_INET) Returns socket index or –1 on error Errors - out of table space, permissions, out of buffer space

6 Socket Functions send(int socketIndex, const void *msg, size_t len, int flags) recv(int socketIndex, const void *buff, size_t len, int flags) Send/receive bytes between socket sendto() and sendmsg() also send to non-connect()ed sockets close(int socketIndex)

7 Socket Functions (client)
connect(int socketIndex struct sockaddr *addr, socklen_t *addrlen) Initiates a connection to a socket addr identical to bind() and varies based on Blocks if no pending connections in the queue struct sockaddr_in server; memset(&server, 0, sizeof(echoserver)); /* Clear struct */ server.sin_family = AF_INET; /* Internet/IP */ server.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */ server.sin_port = htons(atoi(argv[3])); /* server port */ /* Establish connection */ connect(sock, (struct sockaddr *) &server, sizeof(server))

8 Socket Functions (server)
bind (int socketindex, const struct *sockaddr *name, socklen_t namelen) Assigns a name to an unnamed socket For Unix sockets (on file system), file must be deleted with unlink() Socket index returned by call to socket() Sockaddr types vary based on domains Sockadd_in has port/address, sockaddr_un for file path listen (int socketindex, in backlog) Errors – invalid desctriptor index, wrong type of socket for listen

9 Socket Functions (server)
int socket = accept(int socketIndex struct sockaddr *addr, socklen_t *addrlen) Socket index must be created (socket()), bind()ed, and listen()ing Takes first connection, and allocates new descriptor Blocks if no pending connections in the queue addr contains address of connecting entity, format determined by socket domain


Download ppt "Sockets."

Similar presentations


Ads by Google