in unistd.h : int gethostname(char * name, int maxlen) purpose : find the computer's name

11

Click here to load reader

Upload: james-farmer

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

in unistd.h:

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

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

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);

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

needs sys/types.hand 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 sayIPPROTO_TCP or IPPROTO_UDP, they are defined in netinet/in.h

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

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

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

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);

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

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 sockaddrsize = the size in bytes of a sockaddr_in

int listen(int socket, int queue_length) socket = the socket that was just bound to a portqueue_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 sockaddrsize = the size in bytes of a sockaddr_in

if the desired port number is already in use.

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

Also only for servers

int accept(int socket, struct sockaddr * client, int size) socket = a socket that listen() has just been performed ondescription = a pointer to another sockaddr_in, typecast as a pointer to a sockaddrsize = 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.

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

For clients

int connect(int socket, struct sockaddr * client, int size) socket = a newly made socketdescription = a pointer to a sockaddr_in, typecast as a pointer to a sockaddrsize = 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.

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

from <poll.h>

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 fileOther 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 oneof the events you specified occurs, or for the given number of milliseconds, whicheverhappens first.

Result negative for error, 0 if time limit expired.

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

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

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

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 useinet_ntoa(si.sin_addr)

If you’ve got a hostent * called hep, then useinet_ntoa(* (struct in_addr *) hep->h_addr)