DOC PREVIEW
GSU CSC 3320 - sockets

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11UNIX SocketsBeej’s Guide to Socket Programminghttp://beej.us/guide/bgnetR. Stevens, “UNIX Network Programming Vol. 1”2BSD Sockets• Networking protocols are implemented as part of the operating system– The exported networking API is the socket interface– Originally provided by BSD 4.1c ~1982.• The principal abstraction is a socket– Point at which an application attaches to the network– Defines operations for creating connections, attaching to network, sending/receiving data, closing.3Client-Server ParadigmServer1. Create a TCP socket2. Assign a port to socket3. Set socket to listen4. Repeatedly:a. Accept new connectionb. Communicatec. Close the connectionClient1. Create a TCP socket2. Establish connection3. Communicate4. Close the connection4Connection-oriented example (TCP)Serversocket()bind()Clientsocket()listen()accept()recv()send()connect()send()recv()Block untilconnectionProcessrequestConnection establishmentData (request)Data (reply)close()close()25Connectionless example (UDP)Serversocket()bind()Clientsocket()recvfrom()sendto()sendto()recvfrom()Data (request)Data (reply)close()close()6struct sockaddr {unsigned short sa_family; /* Address family (e.g., AF_INET) */char sa_data[14]; /* Protocol-specific address info */};struct sockaddr_in {unsigned short sin_family; /* IP (AF_INET) */unsigned short sin_port; /* Port (16-bits) */struct in_addr sin_addr; /* Internet address (32-bits)*/char sin_zero[8]; /* Not used */}; struct in_addr {unsigned long s_addr; /* Internet address (32-bits) */};GenericIP Specificsockaddrsockaddr_inFamilyFamilyPortBlobInternet addressNot used2 bytes2 bytes4 bytes8 bytesPreparation step: Address Setup7Network vs. Host Byte Ordering– Store 32-bit integer value in memory, for example 4A3B2C1D– Native (host) representation (Little-Endian, MSB last)• sin_family can be in Host Byte Order – Network byte order (Big-Endian, MSB first)• Use for multi-byte, binary data exchange• htonl(), htons(), ntohl(), ntohs()• sin_port and sin_addr must be in Network Byte Order4A 3B 2C 1DBig-Endian1D 2C 3B 4ALittle-Endian8socket#include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, int protocol) domain is either AF_UNIX, AF_INET, or AF_OSI, or .. AF_UNIX is the Unix domain, it is used for communication within a single computer system. [AF_LOCAL is the Posix name for AF_UNIX.] AF_INET is for communication on the internet to IPv4 addresses. type is either SOCK_STREAM (TCP, connection oriented, reliable), or SOCK_DGRAM (UDP, datagram, unreliable), or SOCK_RAW (IP level).protocol specifies the protocol used. It is usually 0 to say we want to use the default protocol for the chosen domain and type. Returns, if successful, a socket descriptor which is an int. It returns -1 in case of failure.39bind#include <sys/types.h> #include <sys/socket.h> int bind(int sd, const struct sockaddr *addr, int addrlen)sd: File descriptor of local socket, as created by the socket function. addr: Pointer to protocol address structure of this socket (e.g. sockaddr_in or sockaddr_un)addrlen: Length in bytes of structure referenced by addr. Returns an integer, the return code (0=success, -1=failure) 10connect#include <sys/types.h> #include <sys/socket.h> int connect(int sd, const struct sockaddr *addr, int addrlen)sd file descriptor of local socket addr pointer to protocol address of other socket (i.e. the one you want to connect to)addrlen length in bytes of address structure.Returns an integer (0=success, -1=failure) 11listenint listen(int fd, int qlen) fd file descriptor of a socket that has already been bound qlen specifies the maximum number of connection requests that can wait to be processed by the server while the server is busy servicing another connection request. Returns an integer (0=success, -1=failure) 12accept#include <sys/types.h> #include <sys/socket.h> int accept(int fd, struct sockaddr *addressp, int *addrlen)fd is an int, the file descriptor of the socket the server was listening on [in fact it is called the listening socket], i.e. on which the server has successfully completed socket, bind, and listen. addressp points to an address. It will be filled with address of the calling client. We can use this address to determine the IP address and port of the client. addrlen is an integer that will contain the actual length of address structure of client. Returns an integer brand new socket file descriptor (-1 in case of failure). It is the socket that the server will use from now on to communicate with the client that requested connection [in fact it is called the connected socket]. Different calls to accept will result in different connected sockets. Remember that the default behaviour for this function is to block the calling process until a connection is actually accepted (you can change that with fcntl)413sendint send(int sockfd, const void *msg, int len, int flags)sockfd is the socket descriptor you want to send data to (whether it's the one returned by socket() or the one you got with accept().)msg is a pointer to the data you want to send. It can be any sort of structure.flags. I am told you should leave this as 0 but see the man page for more info.Returns the number of bytes actually sent. If less than lenthen you must resend the rest.14sendto (for sending over UDP)#include <sys/types.h> #include <sys/socket.h> int sendto(int sd, char *buff, int len, int flags, struct sockaddr *addressp, int addrlen)sd, socket file descriptor buff, address of buffer with the information to be sent len, size of the message flags, usually 0; could be used for priority messages, etc. addressp, address of process we are sending message to addrlen, length of message Returns number of bytes sent. It is -1 in case of failure. 15recvint recv(int sockfd, void *buf, int len, unsigned int flags)sockfd is the socket descriptor to read frombuf is the buffer to read the information intolen is the maximum length of the buffer, and flags can again be set to 0. (See the recv() man page for flag information.)Returns the number of bytes actually read into the buffer, or -1 on error.16recvfrom (recv for UDP)#include <sys/types.h> #include <sys/socket.h> int recvfrom (int sd, char *buff, int len, int flags, struct sockaddr *addressp, int *addrlen) sd, socket file descriptor buff, address of buffer where message will be stored len, size of buffer flags, usually 0; used for priority messages, peeking etc. addressp, buffer that will


View Full Document

GSU CSC 3320 - sockets

Download sockets
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view sockets and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view sockets 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?