DOC PREVIEW
U of I CS 241 - Lecture notes

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

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

Unformatted text preview:

UICI SocketsContentsUICI built on followingSocket StructureSlide 5Implementation of u_openSlide 7Slide 8U_IGNOREaccept:Implementation of UICI u_acceptSlide 12connect:Implementation of u_connectSlide 15Slide 16Slide 17Host Names and IP Addressesname2addr:addr2name:name or addressConverting between name and addressConversion between Name and AddressName2addr implemented using gethostbynameSlide 25Addr2name implemented using gethostbyaddrRemote Procedure CallMore RPCRPC SemanticsThreadsRobustnessSlide 32Java Remote Method InvocationRMIParameter Passing BehaviorFinding ObjectsSummary01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved1UICI SocketsCS 241 Lecture 35R: Ch 18 pp609-655Roy Campbell01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved2ContentsUICIOpenAcceptConnectSelectRPC01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved3UICI built on following#include <sys/socket.h> int socket(int domain, int type, int protocol); int bind(int s, const struct sockaddr *address, size_t address_len); int listen(int s, int backlog); int accept(int s, struct sockaddr *restrict address, int *restrict address_len); socket :domain is AF_UNIX or AF_INETtype is SOCK_STREAM for connection-oriented TCP or SOCK_DGRAM for connectionless UDPprotocol is usually 0 bind:s is the file descriptor returned by socket address contains info about the family, port and machineaddress_len is the size of the structure used for the address01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved4Socket StructureThe format of the address is determined by the address family (domain). •For AF_INET it is a struct sockaddr_in •Which is defined in netinet/in.h •And has at least the following members in network byte order:sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr;01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved5The sin_family should be AF_INET. The sin_port should be the port number in network byte order. You can convert a port number to network byte order using:uint16_t htons(uint16_t port); For a server, the sin_addr can be set to INADDR_ANY to accept connections on any interface card. For a client, it must be filled in with the IP address of the remote machine in network byte order.sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr;01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved6Implementation of u_open15. …u_ignore_sigpipe() 16. …sock = socket(AF_INET, SOCK_STREAM, 0)) 19. …setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&true, sizeof(true))21. …close(sock) 24. …server.sin_family = AF_INET;25. …htonl(INADDR_ANY);27. …bind(sock, (struct sockaddr *)&server, sizeof(server)) 28. …listen(sock, MAXBACKLOG) 15. u_ignore_sigpipe is used to ignore the SIGPIPE signal if this signal has the default action.16. socket is used to create a file descriptor. 19. setsockoption is used to allow immediate reuse of the port number after the server terminates. 27. bind is used to associate the socket with the port number. 28. Listen is used to set the socket to accept incoming requests and to set the number of unaccepted outstanding requests.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved7Implementation of u_open1. #include <errno.h>2. #include <netdb.h>3. #include <stdio.h>4. #include <unistd.h>5. #include <sys/socket.h>6. #include <sys/types.h>7. #include "uici.h"8. #define MAXBACKLOG 509. int u_ignore_sigpipe(void); 10. int u_open(u_port_t port) {11. int error; 12. struct sockaddr_in server;13. int sock;14. int true = 1;15. if ((u_ignore_sigpipe() == -1) ||16. ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1))17. return -1;01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved8Implementation of u_open18. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&true,19. sizeof(true)) == -1) {20. error = errno;21. while ((close(sock) == -1) && (errno == EINTR));22. errno = error;23. return -1; }24. server.sin_family = AF_INET;25. server.sin_addr.s_addr = htonl(INADDR_ANY);26. server.sin_port = htons((short)port);27. if ((bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1) ||28. (listen(sock, MAXBACKLOG) == -1)) {29. error = errno;30. while ((close(sock) == -1) && (errno == EINTR));31. errno = error;32. return -1; 33. }34. return sock;}01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved9U_IGNORE•u_ignore_sigpipe is used to ignore the SIGPIPE signal if this signal has the default action.When a process writes to a pipe that is not open for reading, -1 is returned and errno is set to EPIPE. In addition a SIGPIPE signal is generated. The default action of SIGPIPE is to terminate the process, so in order to handle this error, SIGPIPT must be blocked, caught, or ignored.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved10accept:#include <sys/socket.h>int accept (int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);•The accept call returns file descriptor if successul or -1 otherwise.•It fills the second parameter with information about the remote host. •You fill in the size of the buffer used for the second parameter and on return the third parameter contains the actual size needed to contain this information. •The UICI name resolution routines discussed later can convert the binary values stored in the second parameter to an ASCII host name. •The implementation of u_accept just calls accept and then calls addr2name, a UICI routine described later to convert the address of the remote host from a binary value to a readable host name.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved11Implementation of UICI u_accept13. accept(fd, (struct sockaddr *)(&netclient), &len))16. addr2name(netclient.sin_addr, hostn, hostnsize);13. Wait for a connection request from a client. Return a file descriptor that can be used to communicate with the client.16. Convert the address returned by accept to an ASCII host name.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved12Implementation of UICI u_accept1. #include <errno.h>2. #include <netdb.h>3. #include <string.h>4. #include <arpa/inet.h>5. #include <sys/socket.h>6. #include <sys/types.h>7. #include "uiciname.h"8. int u_accept(int fd, char *hostn, int hostnsize) { 9. int len = sizeof(struct sockaddr);10. struct sockaddr_in netclient;11. int retval;12. while (((retval =13. accept(fd, (struct sockaddr *)(&netclient),


View Full Document

U of I CS 241 - Lecture notes

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Lecture notes
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 Lecture notes 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 Lecture notes 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?