DOC PREVIEW
NU EECS 340 - Socket programming with TCP

This preview shows page 1-2-3-18-19-37-38-39 out of 39 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 39 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 39 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 39 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 39 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 39 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 39 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 39 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 39 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 39 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Some slides are in courtesy of J. Kurose and K. RossReview of Previous Lecture• Electronic Mail: SMTP, POP3, IMAP•DNS• Socket programming with TCPAnnouncement• Homework 1 due Wed. midnight• Should have completed at least part I of project 1• Recitation tomorrow on homework 1 and project 1Outline• Socket programming with TCP• Socket programming with UDP• I/O multiplexing• Web cachingsocket()bind()listen()accept()read()write()read()close()Socket()connect()write()read()close()TCP ClientTCP ServerWell-known portblocks until connection from clientprocess requestConnection establishmentData(request)Data(reply)End-of-file notificationint connect_ socket( char *hostname, int port) {int sock;struct sockaddr_in sin;struct hostent *host;sock = socket( AF_ INET, SOCK_ STREAM, 0);if (sock == -1)return sock;host = gethostbyname( hostname);if (host == NULL) {close( sock);return -1;}memset (& sin, 0, sizeof( sin));sin. sin_ family = AF_ INET;sin. sin_ port = htons( port);sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0];if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) {close (sock);return -1;}return sock;}Resolve the hoststruct hostent *gethostbyname( const char *hostname);/*Return nonnull pointer if OK, NULL on error */Setup up the structunit16_t htons(unit16_t host16bitvaule)/*Change the port number from host byte order to network byte order */Connectconnect(int socketfd, const struct sockaddr * servaddr, socket_t addrlen)/*Perform the TCP three way handshaking*/Hostent structurestruct hostent{char * h_name /*official name of host*/char ** h_aliases; /* pointer ot array of\pointers to alias name*/int h_addrtype /* host address type*/int h_length /* length of address */char ** h_addr_list /*prt to array of ptrs with \IPv4 or IPv6 address*/}Ipv4 socket address structurestruct socketaddr_in{uint8_t sin_len; /*length of the structure (16)*/sa_falimily_t sin_family /* AF_INT*/in_port_t sin_port /* 16 bit TCP or UDP port number*/struct in_addr sin_addr /* 32 bit Ipv4 address */char sin_zero(8)/* unused*/}Make the socketSocket(int family , int type, in t protocol); return nonnegative value for OK, -1 for errorServer – high level viewCreate a socketBind the socketListen for connectionsAccept new client connectionsRead/write to client connectionsShutdown connectionAssigning an address to a socket•The bind() system call is used to assign an address to an existing socket.int bind( int sockfd, const struct sockaddr *myaddr, int addrlen);• bind returns 0 if successful or -1 on error.bind()• calling bind() assigns the address specified by the sockaddr structure to the socket descriptor.•You can give bind() a sockaddr_instructure:bind( mysock, (struct sockaddr*) &myaddr,sizeof(myaddr) );bind() Exampleint mysock,err;struct sockaddr_in myaddr;mysock = socket(PF_INET,SOCK_STREAM,0);myaddr.sin_family = AF_INET;myaddr.sin_port = htons( portnum );myaddr.sin_addr = htonl( ipaddress);err=bind(mysock, (sockaddr *) &myaddr, sizeof(myaddr));Make listen socket (TCP)int make_ listen_ socket( int port) {struct sockaddr_ in sin;int sock;sock = socket( AF_ INET, SOCK_ STREAM, 0);if (sock < 0)return -1;memset(& sin, 0, sizeof( sin));sin. sin_ family = AF_ INET;sin. sin_ addr. s_ addr = htonl( INADDR_ ANY);sin. sin_ port = htons( port);if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0)return -1;return sock;}Make the socketSetup up the structBindbind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen);/* return 0 if OK, -1 on errorassigns a local protocol adress to a socket*/listen()int listen( int sockfd, int backlog);sockfd is the TCP socket (already bound to an address)backlog is the number of incoming connections the kernel should be able to keep track of (queue for us).listen() returns -1 on error (otherwise 0).Accepting an incoming connection.• Once we call listen(), the O.S. will queue incoming connections– Handles the 3-way handshake– Queues up multiple connections.• When our application is ready to handle a new connection, we need to ask the O.S. for the next connection.accept()int accept( int sockfd,struct sockaddr* cliaddr,socklen_t *addrlen);sockfd is the passive mode TCP socket.cliaddr is a pointer to allocatedspace.addrlen is a value-resultargument– must be set to the size of cliaddr– on return, will be set to be the number of used bytes in cliaddr.accept() return valueaccept() returns a new socket descriptor (small positive integer) or -1 on error.After accept returns a new socket descriptor, I/O can be done using the read() and write()system calls.read() and write() operate a little differently on sockets (vs. file operation)!Accepting a client connection (TCP)int get_ client_ socket( int listen_ socket) {struct sockaddr_ in sin;int sock;int sin_ len;memset(& sin, 0, sizeof( sin));sin_ len = sizeof( sin);sock = accept( listen_ socket, (struct sockaddr *) &sin, &sin_ len);return sock;}Setup up the structAccept the client connectionaccept(int sockefd, struct sockaddr * claddr, socklen_t * addrlen)/* return nonnegative descriptor if OK, -1 on errorreturn the next completed connection from the front of thecompleted connection queue.if the queue is empty, the process is put to sleep(assuming blocking socket)*/Reading from/writing to a TCP socketint read( int fd, char *buf, int max);int write( int fd, char *buf, int num);•By default read() will block until data is available.• reading from a TCP socket may return less than max bytes (whatever is available).• You must be prepared to read data 1 byte at a time! • write might not be able to write all num bytes (on a nonblocking socket).Terminating a TCP connection• When finished using a socket, the socket should be closed:• status = close(s);– status: 0 if successful, -1 if error– s: the file descriptor (socket being closed)• Closing a socket– closes a connection (for SOCK_STREAM)– frees up the port used by the socketsocket()bind()listen()accept()read()write()read()close()Socket()connect()write()read()close()TCP ClientTCP ServerWell-known portblocks until connection from clientprocess requestConnection establishmentData(request)Data(reply)End-of-file notificationOutline• Socket programming with TCP• Socket programming with UDP• I/O multiplexing• Web cachingSocket programming with UDPUDP: no “connection” between client and server•no handshaking• sender explicitly attaches IP address and port of destination to each


View Full Document

NU EECS 340 - Socket programming with TCP

Download Socket programming with TCP
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 Socket programming with TCP 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 Socket programming with TCP 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?