Sockets, HTTP and addressing (w1-w2) Flashcards
iPv4 is a __ bit number, written in which format?
Also, what is the range of numbers between each dot? Why?
32 bit number, written as dotted quad, range is up to 255 (including 0) , as 2^8 = 256.
how many addresses available in IPv4?
2^32
IPv6 has how many addresses available?
340 282 366 920 938 463 463 374 607 431 768 211 456
how are IPV6 addresses formatted?
8 Groups of 16 bits each, each group written in hex with a colon separating the groups
getByName is a method part of which class?
InetAddress class
getByName() accomplishes what?
resolves address with DNS name
getHostAddress() does what?
returns string containing IP address
if given the address www.google.com, write a two line program which returns the correct IP address using InetAddress class
InetAddress IP = InetAddress.getByName(www.google.com);
System.out.println(name + “ : “ +
IP.getHostAddress());
write a two line program which returns the host name, when given an IP Address
InetAddress IP =
InetAddress.getByName(addressIP);
System.out.println(address + “ : “ +
IP.getHostName());
write a two line program which connects to a website, given the port and website name`
InetAddress IP =
InetAddress.getByName(“www.wand.net.nz”);
Socket mysocket = new Socket(IP, 443);
Initialize a printwriter, write a line, then close the writer
PrintWriter out = new PrintWriter(mysocket.getOutputStream(), true); out.println("hello world"); out.close();
Initialize a Buffered Reader given a socket object “socket”
InputStreamReader isr = new InputStreamReader(mysocket.getInputStream()); BufferedReader reader = new BufferedReader(isr);
initialize a serversocket on port 1234 and accept a socket connection
ServerSocket server = new ServerSocket(1234); Socket client = server.accept();
What does the InetAddress class deal with
deals with resolving names and addresses
How can we write a server, so that it can accept more than one connection at a time, allowing two clients to simultaniously use the server?
Threads.
How would we start a thread running in the main code?
ServerThread thread = new ServerThread(clientSocket); thread.start();
When thread.start() is called, which method begins executing code in the thread class?
the run() method will begin executing code
how can we make class server{…} implement threads?
by extending it with threads: class server extends Thread{…}
What is the main problem with threads? what is a partial solution to this?
threads do not scale, partial solution is thread pools. If a pool of size N threads gets N+1 threads, the thread number N+1 will then block until another thread is let go
what is the problem with multiple clients accessing a server variable simultaneously? give an example
the server cpu might stop one process just as it is about to save some changes to a variable. then a second process starts and changes the variable. when the first process is then resumed, it will save the variable according to the first processes state, this ovverriding the second process’s changes.
how does the synchronized keyword work
when changing a variable accessed by multiple users, wrap the code with the synchronize keyword in all processes to ensure the variables integrity
what is the https:// mean at the start of urls?
it is the protocol used. in this case, https protocol
break http request/response headers down into three sections
ASCII text header
Empty Line
Binary Data
what is the end of the header signalled by, in the request and response
an empty line
what is the end of line sequence for http, and some popular operating systems
\r\n same as windows
\r for mac
\n for unix
while((rc = fis.read(buf)) != -1){
bos.write(buf, 0, rc);
}
explain every single thing going on in this while loop
while statement = fill up buf with as much input stream as possible, and store the length of data into rc. if no more data, this will equal -1 and exit the loop.
bos statement = writes according to bos the array, from start of the array, to end (rc)
with a header like : String message = “HTTP/1.1 200 OK\r\n\r\n”; how would you send this header, given a buffered output stream object?
byte[] array = message.getBytes();
bos.write(array, 0, array.length);