Network Programming With Sockets
What are the 4 helper functions for conversion (in C) and what do they do?
#include <arpa/inet.h> unit32_t htonl(uint32_t hostlong); unit16_t htons(uint16_t hostshort); unit32_t ntohl(uint32_t netlong); unit16_t ntohs(uint16_t netshort); these functions convert from host(h) to network (n) representation or vice versa. they exist for 16 and 32 bit numbers.
What are the helper functions for address manipulation (this is insane as **** but I couldn't break it up) and what do they do?
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int inet_aton(const char *cp, struct in_addr *inp); in_addr_t inet_addr(const char *cp); in_addr_t inet_network(const char *cp); char *inet_ntoa(struct in_addr in); struct in_addr inet_makeaddr(int net, int host); in_addr_t inet_lnaof(struct in_addr in); in_addr_t inet_netof(struct in_addr in); It is often useful to convert from string representations of addresses like "130.149.49.77" to 32-bit address values. The above helpers support this type of conversion.
How would we write the accept() function in C and what does it do?
#include <sys/types.h> #include <sys/sockets.h> int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); accept() waits for incoming connection requests of stream type. For incoming connection a new socket descriptor is created and returned as return value. Only ever used on a stream TCP server. Accept() is blocking.
How would we write the bind() function in C and what does it do?
#include <sys/types.h> #include <sys/sockets.h> int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); links a socket to a particular IP address/port number/protocol combination
How would we write the connect() function in C and what does it do?
#include <sys/types.h> #include <sys/sockets.h> int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); for stream TCP sockets a client uses connect() to request establishment of a connection with a server. for datagram UDP sockets a client uses connect() to specify a default receiver for datagrams. connect() is blocking
How would we write the listen() function in C and what does it do?
#include <sys/types.h> #include <sys/sockets.h> int listen(int sockfd, int backlog); declares your willingness to accept incoming connections of type SOCK_STREAM or SOCK_SEQPACKET and allocates resources like a queue for incoming connection requests It is only used on the server side. listen() is non blocking
In C how would we create a new socket, and what features would it have?
#include <sys/types.h> #include <sys/sockets.h> int socket(int domain, int type, int protocol); creates a new socket structure, which includes allocation of resources like the socket buffer, and returns a file descriptor representing the socket to the caller. socket() is non blocking
How would we write the read() and resulting functions in C?
#include <sys/types/h> #include <sys/socket.h> #include <unistd.h> ssize_t recv(int sockfd, void *buf, size_t len, int flags); ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); ssize_t read(int fd, void *buf, size_t count);
How would we write the write() and resulting functions in C?
#include <sys/types/h> #include <sys/socket.h> #include <unistd.h> ssize_t send(int sockfd, const void *buf, size_t len, int flags); ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t *addrlen); ssize_t write(int fd, const void *buf, size_t count);
What is the close() function, what does it do and how do we write it?
#include <unistd.h> int close(int fd); needs to be called whenever you do not need a socket anymore. it gives back the socket resources to the operating system for use by other applications. as argument you supply the socket descriptor sockfd returned by socket
What do the following do in regards to the TCP server? 1. bind() 2. listen() 3. accept()
1. bind a socket to a particular port number and IP address 2. declare willingness to accept incoming connections, allocate resources for incoming connection requests 3. accept an incoming connection request and creates a new socket for this connection
What do the following do in regards to the UDP client with connect()? 1. connect() 2. write()
1. calls supplies default receiver address. Can be called multiple times to change the default receiver. 2. sends the last receiver specified by connect()
What do the following do in regards to the UDP client? 1. connect() 2 rcvfrom() sendto()
1. compared to TCP client no connect() system call is necessary. tricked ya. 2. receiver address then has to be supplied to sendto()
What do the following do in regards to the TCP client? 1. socket() 2. connect() 3. read()/write() 4. close()
1. creates a socket, which then allows resources. 2. establish connection with server, use any free local port number 3. reading/writing from/to a socket 4. close.. it
The socket API calls: 1. write() 2. read() 3. select() do what exactly?
1. sending over a socket can be achieved by calling write() 2. for reading from a socket you can call read() 3. sockets can be used in the same way as filehandles e.g. in select()
What is a socket?
A network socket is one endpoint in a communication flow between two programs running over a network. It is bound to one port. it is associated with underlying protocol such as TCP.
The basic IP service is packet delivery. What are the four main bad features of this service?
Connection less: no connection or shared state is set u before datagram delivery starts. Unacknowledged: IP does not recognize acknowledgments Unreliable: on IP level no retransmissions are carried out Unordered: IP does not guarantee in-sequence delivery
This isn't a question. It's a request. Feel free to disregard it but you might be sorry chappy! Look at TCP client and server in the sockets lecture (slide 60 - end) and try to understand them.. its like 12 pages of code.. enjoy!
Do it for me Pree?
What is DNS?
Domain name service. A special service/protocol which translates human readable host names to IPv4 addresses, the internet itself only works with IPv4
What is an IP address?
Internet protocol. There are two versions 4 and 6 with different address formats, but mostly we use IPv4. It identifies the host and helps the network to find a path to this host.
What are the significant features of the IPv4 address?
It has a width of 32 bits. They are worldwide unique written with decimal notation such as 130.149.49.77 they have an internal structure <network-id> <host-id> where network-id denotes a network and host-id a host within the network
How do we write to a socket using UDP and TCP?
UDP: the data encapsulated in UDP datagram and immediately transferred - each write() leads to separate diagram. TCP: the data is buffered and possibly combined with data from further write()'s before transmitting
What do the parameters addr and addrlen do in terms of connect()?
You have to prepare an address structure containing the address/port number/protocol of the server. format of this depends on address family. Fir IPv4 a sockaddr_in structure can be used
How does the socket interface help server applications and what is the local host?
client and server applications can even run on the same machine and communicate using the socket interface. Client uses special IP address 127.0.0.1 known as the local host
UDP offers very similar services to that of IPv4. However TCP is quite different. How so?
connection oriented, reliable and in sequence, byte stream oriented, flow controlled and congestion controlled, full duplex. Using TCP allows for safe and reliable data transfer over the internet where data is transferred reliably and in the right sequence
Processors can use different ways to store data in memory. For certain types relevant for networking purposes one does two things. What are they? Give reference to network byte order and host byte order.
defines a canonical representation that is actually being transmitted called the network byte order. asks hosts to convert between their own representation (host byte order) and network representation as needed
What does the parameter sockfd do in terms of accept()?
denotes the socket on which you attempt connections. Is just the value returned by socket()
What does the parameter sockfd do in terms of connect()?
denotes the socket over which you want to communicate, it is just the value returned by socket()
what is a datagram socket?
does not guarantee reliable or in sequence delivery. is built on the UDP protocol. if delivery fails the sender is not informed
Given the write() and resulting functions from the previous card in C, what does the function do?
for all these functions the data that is to be sent is supplied in the buf and len parameters pointing to a buffer. with send() and write() the addressee is either implicitly known or has been specified before by connect(). with sendto() the addressee has to be explicitly specified in the dest_addr and addrlen parameters, which in case of the IPv4 address family can be of type sockaddr_in. these are non blocking only if there is enough space to accommodate data.
given the write() and resulting functions, what is its return value? What issues does this return function have?
if it is >= 0 then it denotes the actual number of bytes written. if it is -1 then an error occurred and errno contains information about it. when these functions return successfully you do not known more than the data has been placed into the socket buffer. you cannot make any inference on whether or not the data has actually been transmitted or successfully received
what is the return value of read() and resulting functions?
if it is >=0 then it denotes the actual number of bytes read if it is -1 then an error occurred, the reason of which is denoted in errno.
what does the return value do for socket()?
if successful a file descriptor >= 0 is returned else -1 is returned and errno is set
given a 8 bit integer number b7b6b5b4b3b2b1b0 stored somewhere in memory as an ordered sequence of bytes, In what way would intel and motorola processors display this and what is the format known as? What would happen if we sent the integer from intel to motorola?
intel uses the low-endian format. b3b2b1b0 b7b6b5b4 where the gaps denote a byte boundary. motorola uses big endian format. b7b6b5b4 b3b2b1b0. If intel sent to motorola a port number, the network byte order is defined to be big endian.
What is IP addressing?
it allows us to distinguish network interfaces. to address different applications/processes/tasks in a host, an additional addressing layer is needed
what is the errno variable?
it indicates the cause of an error, and the errors that can be raised by a particular socket call are often described in its man page
Operating systems create an abstraction for processes that:
makes them believe they have their own processor, allocates memory exclusively and are the units that can request other OS resources like sockets, pipes, files, etc
What is a stream socket?
offers reliable in sequence delivery of a byte stream. is built on the TCP protocol. if delivery fails for some reason, the sender is notified.
What is the return value for connect()?
on success it will be 0 and otherwise it will be -1 and errno is set
What are the most well known ports? all 4 of them please.
port 80: HTTP the web! port 22: SSH secure shell port 25: SMP emails port 53: DNS
what are raw and sequenced-packet sockets?
raw: bypass UDP to TCP and directly send IP packets sequenced: a variant of a stream socket
How does a server application run in regards to IP and ports.
run on an end host. needs an address/port number that is known to clients. are ready to accept service requests from client and respond to it. usually run all the time and support several clients
How does a client application run in regards to IP and ports?
run on an end host. request service from a server and proceed when response is there. to do this a client needs to know the servers IP address and port number, and then initiates contact with server. after wards a client application can end
Processes can be in several states. What might they be?
running: actually running on the processes runnable: ready to run on the processor but not actually doing it blocked: process is waiting for some input from a hard disk or the like. when this input arrives the process becomes runable or running. a blocked process does not use CPU resources
What does the parameter domain do?
selects the protocol family to be used for the socket options include AF_INET for IPv4 AF_INET6 for IPv6
What is a byte stream service? This is.. insane.
smallest unit of transmission is a byte. different write()s do not necessarily lead to different packets. receiver cannot easily detect which was the last byte supplied at the transmitter with one write() command. If I send 10 bytes then 20 bytes, you receive 30 only and cannot tell how many writes() contributed to it nor where boundaries are.
What is the parameter sockfd and the return function do in terms of listen()?
socfd denotes the socket and is the return value of socket() it returns 0 if successful or -1 and errno is set.
Outline how the TCP server works.
socket() to bind() to listen() to accept() to read()/write() which can happen multiple times, then finally close()
Outline how the UDP server operates.
socket() to bind() to read()/write() which can happen multiple times
Outline how the UDP client with connect operates.
socket() to connect() to read()/write() which can happen multiple times, before finally close()
Outline how the TCP client operates.
socket() to connect() to read()/write() which can happen multiple times, then finally close()
Outline how the UDP client operates.
socket() to rcvfrom()/sendto() which can happen multiple times, before finally close()
What is the parameter sockfd and the return value of bind()?
sockfd denotes the socket and is just the value returned by socket() the return value is 0 when successful others -1 and we get errno
What is the parameter backlog do in terms of listen()?
there is a queue for non processed incoming connection requests and backlog specifies how long this queue can be. any excess connection request is declined
Given the read() and resulting functions from the previous card in C, what does the function do?
these functions can read received data from a socket. caller provides a buffer buf og given length len into which received data will be written. in recvfrom() the caller also provides memory for an address structure in which the receiving protocol provides the address/port/protocol of the host from which the data was received. read() is used for reading from a file indicated by the fd argument, you can supply the socket descriptor sockfd here you got from socket(). All these functions are usually blocking.
Define ports well. Really well.
they are a 16 bit number. a process on a host can allocate one or more ports. one port is allocated to at most one application/process
What are buffers in regards to sockets?
they decouple the application from the underlying protocol. receiving UDP/TCP protocol entity places incoming data into recieve buffer, applications read() data from buffer at their leisure. application at transmitter write() data into transmit buffer, TCP/UDP entity sends it at its discretion
What do the parameters addr and addrlen do in terms of listen()?
they receive address information about the requesting client such as its IP, and the precise type of the address info depends on the address family; for IPv4 you can type cast that into type sockaddr_in
What is blocking in regards to socket calls?
this refers to the concept of processes in operating systems. A process is a running instance of a program/application.
What do the parameters type and protocol do?
type: indicates socket type. options include SOCK_STREAM, SOCK_DGRAM and SOCK_RAW for stream datagram and raw sockets protocol: selects the protocol used for the given socket type, often only one option sensible then protocol=0 is a good choice
What are UDP and TCP?
user datagram protocol and transmission control protocol. Both protocols operate on top of IPv4, they generate packets and stuff these as payload data into IPv4 packets. through these port numbers there can be many simultaneous conversations going on between the same two hosts.
What does a call like select() do?
waits for input on a socket (e.g. incoming packets) if no input is available at the time of call, the calling process will be put into blocked state hence does not use the CPU
what is a datagram service?
when write()ing x bytes to a datagram socket the written data is encapsulated into a UDP packet and sent out. the receiver will read() a block of x bytes from its socket