MASON CS 475 - Network Programming using sockets

Unformatted text preview:

Network Programming usingsocketsDistributed Software SystemsProf. Sanjeev SetiaAPIs for TCP/IPTCP/IP is a proto col designed to op erate in multi-vendor environmentinterface between TCP/IP and applications lo oselysp eciedapplication interfaces{BSD UNIX:so cketinterface{AT&T:TLIinterfaceTCP/IP software inside kernel invoked by systemcallsUNIX I/O facilities extended with TCP/IP sp eciccalls9The So cket Interfaceprovides functions that supp ort networkcommunication using many p ossible proto cols{PFINET is one proto col family supp orted byso ckets{TCP and UDP are proto cols in PFINET familyso cketis the abstraction for network communicationa so cket is identied by so cket descriptorsystem data structure for so cket{family (e.g., PFINET){service (e.g., SOCK STREAM){Lo cal IP address, Lo cal Port{Remote IP address, Remote Portpassiveso cket: so cket used by a server to wait forincoming connections ;activeso cket: so cket usedby client to initiate a connection10Endp oint AddressesTCP/IP proto cols dene a communication endp ointto consist of an IP address and a proto col portnumb erother proto col families have other denitionsso cket abstractions supp orts the concept ofaddressfamilywhich allows dierent proto cols to have theirown address representationsTCP/IP proto cols use a single addressrepresentation with address family denoted byAFINET11Endp oint Addresses cont'dstructure for AFINET addressesstruct sockaddr_in { /* struct to hold an address */u_char sin_len; /* total length */u_short sin_family; /* type of address */u_short sin_port; /* protocol port number */struct in_addr sin_addr; /* IP address */char sin_zero[8]; /* unused (set to zero) */};if program using mixture of proto cols, programmer must be careful sincenot all addresses have the same size12System Callsso cket{used to create new so cket{arguments: proto col family (e.g. PFINET),proto col or service (i.e., stream or datagram){returns so cket descriptorconnect:{client calls connect to establish an activeconnection to the server{argument to connect sp ecies remote endp ointwrite{servers and clients use write to send data acrossa TCP connection{arguments: so cket descriptor, address of data,length of data13System Calls cont'dread{used to receive data from a TCP connection{arguments: so cket, buer, length of buer{read blo cks if no data; if more data than ts inbuer, it only extracts enough to ll the buer;if less than buer length, it extracts all the dataand returns numb er of bytes readread and write can also be used with UDP butdierent b ehaviorclose: used to deallo cate so cket; deleted when lastpro cess that is using so cket do es a closebind{used to sp ecify a lo cal endp oint address for aso cket{uses so ckaddrin structure14System Calls cont'dlisten{used by connection-oriented servers to put so cketin passive mo de{arguments: so cket, size of queue for so cketconnection requestsaccept{creates a new so cket for each connection request{returns descriptor of new so cket to its calllerUDP calls:{send, sendto, sendmsg{recv, recvfrom, recvmsg15Integer Conversionstandard representation for binary integers used inTCP/IP proto col headers:network byte order, MSBrste.g. the proto col port eld in struct so ckaddrinuses network byte orderhost's integer representation mayb e dierentconversion routines: htons, htonl, ntohl, ntohsshould be used for portability16Client Softwareconceptually simpler than servers b ecausedo not have to handle concurrent interactions withmultiple serversusually not privileged software)don't have to beas carefulno authentication, protection, etc.17Lo cating the serverserver's IP address and port numb er neededcan be sp ecied as a constant in the programhave the user sp ecify it as an argument wheninvoking clientread from a le on diskuse a proto col to nd the server (e.g. a broadcastmessage to which servers resp ond)18Parsing address argumentaddress argument typically is a hostname likecs.gmu.edu or IP address in dotted decimal notationlike 129.174.29.34need to sp ecify address using structure so ckaddrinlibrary routines inetaddr and gethostbyname usedfor conversionsstruct hostent {char *hname;char **h_aliases;int h_addrtype;int h_length;char **h_addr_list;};#define h_addr h_addr_list[0];19EXAMPLE:struct hostent *hptr;char *name = ``cs.gmu.edu'';if ( hptr = gethostbyname(name)) {/* IP address is in hptr->h_addr */} else {/* handle error */}inetaddr converts dotted decimal IP address intobinary20Client Software cont'dlo oking up a well known port by namestruct servent dened innetdb.hin the same way as structhostentstruct servent *sptr;if (sptr = getservbyname(``smtp'',``tc p'')){/* port number is now in sptr->s_port */} else {/* handle error */}NOTE: getservbyname returns proto col port innetwork byte order21Client Software cont'dlo oking up a proto col by namestruct proto ent dened innetdb.hstruct protoent *pptr;if (pptr = getprotobyname(``udp'')){/* official protocol number is in pptr->p_proto */} else {/* handle error */}22TCP client algorithm1. Find IP address and proto col numb er of server2. allo cate a so cket3. sp ecify that the connection needs an arbitrary,unused proto col port on lo cal machine and allowTCP to select one4. Connect the so cket to the server5. Communicate with the server using application-levelproto col6. Close the connection23TCP client cont'dAllo cating a so cket#include <sys/types.h>#include <sys/socket.h>int s; /* socket descripto */s = socket(PF_INET,SOCK_STREAM, 0);Cho osing a lo cal port numb er{conicts have to be avoided{happ ens as a side-eect to connect callcho osing a lo cal IP address{a problem for hosts connected to multiplenetworks{chosen automatically by TCP/IP at time ofconnection24Connecting a TCP so cket to a serverretcode = connect(s,remaddr,re maddrlen)connect p erforms four tasks1. tests sp ecied so cket is valid and not alreadyconnected2. lls in remote address in so cket from secondargument3. cho oses a lo cal endp oint address for so cket (if itdo es not have one)4. initiates a connection and returns value to thecaller25Communicating with the server usingTCP: Example#define BLEN 120char *req = ``request of some sort'';char buf[BLEN];char *bptr;int n;int buflen;bptr = buf;buflen = BLEN;/* send request */write(s,req,strlen(req);/*


View Full Document

MASON CS 475 - Network Programming using sockets

Download Network Programming using 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 Network Programming using 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 Network Programming using 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?