Sockets for servers Flashcards
How do berkeley sockets work for servers?
client actively initiates the connection
Server is passive, it waits and listens for an incoming connection from the
client
* Waits on a specific port (and possibly IP address)
* Start off as before by creating a socket
What happens next?
next they bind that socket to the port and IP address we want to use
how does binding work?
can either specify a specific IP address or ask it to listen on all interface using INADDR_ANY
what does bind() function do?
socket is bound to the address and port
How do we setup port for listening?
API provides a queue to allow multiple connections to queue up
* Once queue is full connections are refuse
* Can set the queue size when we set the socket to listen using the
listen() function
what do we do when a connection happens?
use accept() function
what does accept() function do?
blocks until a connection is made
returns new socket descriptor for connection
returns client address of the connection
What haooebs after contact has been made?
client and server are connected
server can speak to client
use recv() send()
once finsihed call close()
how does server speak to client?
via the returned socket descriptor
What abt if theyre are multiple connections?
they arent processed until accept() is called again
so heart of our serve is normally an infinite loop
what does this infinite loop do?
waits to accept connection
handles transaction
close connection
How are multiple connections handled?
one by one
second connection wont open till first is closed
accept() has to be called immediatley after it returns
program needs to fork in 2