Presentation is loading. Please wait.

Presentation is loading. Please wait.

In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.

Similar presentations


Presentation on theme: "In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name."— Presentation transcript:

1 in unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name.

2 in netdb.h, and needing sys/socket.h : struct hostent { char * h_name; // the official name char * * h_aliases; // list of alternate names, NULL at end int h_addrtype; // what kind of address: expect AF_INET int h_length; // bytes in each address: expect 4 char * h_addr; // primary address - not a string char * * h_addr_list; // all addresses, not strings, NULL at end }; in netdb.h : hostent * gethostbyname(char * name);

3 needs sys/types.h and sys/socket.h int socket(int domain, int type, int protocol) Create a file-like object capable of internet communications domain = AF_INET if you want to use IP, there are many others. type = SOCK_STREAM if you want TCP, or SOCK_DGRAM if you want UDP, there are a few others protocol = 0, except in rare circumstances 0 means use the default protocol for this type. for SOCK_STREAM, that should be TCP, for SOCK_DGRAM, it should be UDP, but you can be explicit and say IPPROTO_TCP or IPPROTO_UDP, they are defined in netinet/in.h

4 in netinet/in.h struct sockaddr_in { char sin_len; // total size of the struct in bytes char sin_family; // usually AF_INET short int sin_port; // port number, in network format, use htons in_addr sin_addr; // IP address for the connection char sin_zero[8]; // unused. }; typedef uint32_t in_addr_t; // uint32_t is elsewhere defined as unsigned int struct in_addr { in_addr_t s_addr; }; so an in_addr is a four byte struct that contains just an int

5 so for a client struct sockaddr_in server_address; server_address.sin_len = sizeof(server_address); server_address.sin_family = AF_INET; server_address.sin_port = htons(portnumber); server_address.sin_addr.s_addr = the address from a hostent, or htonl( ipaddress ); and for a server struct sockaddr_in server_address; server_address.sin_len = sizeof(server_address); server_address.sin_family = AF_INET; server_address.sin_port = htons(portnumber); server_address.sin_addr.s_addr = htonl(INADDR_ANY);

6 Also only for servers int bind(int socket, struct sockaddr * description, int size) socket = a socket as created by socket() description = a pointer to a sockaddr_in, but typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in int listen(int socket, int queue_length) socket = the socket that was just bound to a port queue_length = keep it small These are for servers only. listen fails int bind(int socket, sockaddr * description, int size) socket = a socket as created by socket() description = a pointer to a sockaddr_in, but typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in if the desired port number is already in use.

7 Also only for servers int accept(int socket, struct sockaddr * client, int size) socket = a socket that listen() has just been performed on description = a pointer to another sockaddr_in, typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in Waits until a client makes a connection. The sockaddr_in called client tells you the IP address and port number of that client. The result is a new socket that is to be used to communicate with the client, or an error value. The error EINTR is not really an error, just wait and try again. When you are finished with the client, just close the new socket. The original remains to accept connections from other clients.

8 For clients int connect(int socket, struct sockaddr * client, int size) socket = a newly made socket description = a pointer to a sockaddr_in, typecast as a pointer to a sockaddr size = the size in bytes of a sockaddr_in A non-error result means you are connected. Use socket as a file to communicate with the server. close() it to terminate the connection.

9 from struct pollfd { int fd; // a file that you’d like to be informed about int events; // the events the you’d like to be informed of when they happen to that file int revents; // set to zero, this is where you get the results The only event that you’re really likely to care about is POLLRDNORM - It is now possible do read input from this file: some has arrived. If a connection is closed, you still get this event, but when you do the read, you receive nothing. Just like reaching the end of a normal file Other events can be combined with the | operator, you can see them all in poll.h, they include POLLWRNORM - It is now possible do write to this file:it usually is. POLLERR - An error has occurred int poll(struct pollfd Array[], int length_of_array, int milliseconds); Array contains as many pollfd objects as you want. The function waits until at least one of the events you specified occurs, or for the given number of milliseconds, whichever happens first. Result negative for error, 0 if time limit expired.

10 If you want to read from file a or file b, whichever is ready first: struct pollfd interesting[2]; interesting[0].fd = a ; interesting[0].events = POLLRDNORM | POLLERR; interesting[0].revents = 0; interesting[1].fd = b; interesting[1].events = POLLRDNORM | POLLERR; interesting[1].revents = 0; r = poll(interesting, 2, 1000); if (r < 0) { perror("poll"); sleep(1); } if (interesting[0].revents & POLLRDNORM) // you can now read from file a if (interesting[0].revents & POLLRDNORM) // you can now read from file b

11 char * inet_ntoa(struct in_addr address) converts a 4 byte IP address into readable form. If you’ve got a sockaddr_in called si, then use inet_ntoa(si.sin_addr) If you’ve got a hostent * called hep, then use inet_ntoa(* (struct in_addr *) hep->h_addr)


Download ppt "In unistd.h : int gethostname(char * name, int maxlen) Purpose : Find the computer's name."

Similar presentations


Ads by Google