Sockets, HTTP and addressing (w1-w2) Flashcards

1
Q

iPv4 is a __ bit number, written in which format?

Also, what is the range of numbers between each dot? Why?

A

32 bit number, written as dotted quad, range is up to 255 (including 0) , as 2^8 = 256.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how many addresses available in IPv4?

A

2^32

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

IPv6 has how many addresses available?

A

340 282 366 920 938 463 463 374 607 431 768 211 456

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how are IPV6 addresses formatted?

A

8 Groups of 16 bits each, each group written in hex with a colon separating the groups

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

getByName is a method part of which class?

A

InetAddress class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

getByName() accomplishes what?

A

resolves address with DNS name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

getHostAddress() does what?

A

returns string containing IP address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

if given the address www.google.com, write a two line program which returns the correct IP address using InetAddress class

A

InetAddress IP = InetAddress.getByName(www.google.com);
System.out.println(name + “ : “ +
IP.getHostAddress());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

write a two line program which returns the host name, when given an IP Address

A

InetAddress IP =
InetAddress.getByName(addressIP);
System.out.println(address + “ : “ +
IP.getHostName());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

write a two line program which connects to a website, given the port and website name`

A

InetAddress IP =
InetAddress.getByName(“www.wand.net.nz”);
Socket mysocket = new Socket(IP, 443);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Initialize a printwriter, write a line, then close the writer

A
PrintWriter out = new
PrintWriter(mysocket.getOutputStream(), true);
out.println("hello world");
out.close();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Initialize a Buffered Reader given a socket object “socket”

A
InputStreamReader isr = new InputStreamReader(mysocket.getInputStream());
BufferedReader reader = new
BufferedReader(isr);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

initialize a serversocket on port 1234 and accept a socket connection

A
ServerSocket server = new ServerSocket(1234);
Socket client = server.accept();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the InetAddress class deal with

A

deals with resolving names and addresses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

Threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would we start a thread running in the main code?

A
ServerThread thread = new ServerThread(clientSocket);
thread.start();
17
Q

When thread.start() is called, which method begins executing code in the thread class?

A

the run() method will begin executing code

18
Q

how can we make class server{…} implement threads?

A

by extending it with threads: class server extends Thread{…}

19
Q

What is the main problem with threads? what is a partial solution to this?

A

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

20
Q

what is the problem with multiple clients accessing a server variable simultaneously? give an example

A

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.

21
Q

how does the synchronized keyword work

A

when changing a variable accessed by multiple users, wrap the code with the synchronize keyword in all processes to ensure the variables integrity

22
Q

what is the https:// mean at the start of urls?

A

it is the protocol used. in this case, https protocol

23
Q

break http request/response headers down into three sections

A

ASCII text header
Empty Line
Binary Data

24
Q

what is the end of the header signalled by, in the request and response

A

an empty line

25
Q

what is the end of line sequence for http, and some popular operating systems

A

\r\n same as windows
\r for mac
\n for unix

26
Q

while((rc = fis.read(buf)) != -1){
bos.write(buf, 0, rc);
}
explain every single thing going on in this while loop

A

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)

27
Q

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?

A

byte[] array = message.getBytes();

bos.write(array, 0, array.length);