Tutorial 1: Sockets Flashcards

1
Q

Assume the standard socket API providing the following functionality that has also been introduced in the lecture:

socket(), bind(), listen(), accept(), connect(), send(), receive(), close()

Design two separate finite state machines (FSMs) that describe a communication session between a client and a server. The first FSM should represent the server and the second FSM represents the client. The communication session should remain opened and messages should be exchanged until one of the parties decides to close it. Hint: First think of a set of states to represent a connection. Then use the API functions to transition between states.

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

When exchanging messages via network, peers usually comply with a common protocol, e.g Http for the World Wide Web. To develop a protocol, message types and the order of actions have to be specified. In this task, you are to develop a protocol for a network service, which performs calculations on complex numbers. A pair of complex numbers and an operator is sent from a client to a server. Depending on the operator, the server performs an add, subtract, multiply or divide operation on the pair and the result is returned to the client.

  1. Define a string serialization for a complex number.
  2. Define a message format m1, which is used by the clients to send computation requests to the server and construct an example message using the string serialization for complex numbers.
  3. Define a message format m2, which is used by the server to send back results to the client. The response should include a status string. Think about status messages, that could be provided by the server, in order to indicate different problems during the processing of the request.
  4. The network service should also be able to convert a complex number from standard to polar notation. Extend your protocol in order to support the new operation.
  5. Draw a sequence diagram showing a valid request-response cycle. Draw a sequence diagram of an errornous request-response cycle.
  6. Considerthecomplexnumbersc1=3.0+4.0iandc2=1.0+2.0i.Showthemessageexchangeforallfiveoperations (add,subtract,multiply,divide, polar)
A
  1. Solution: a + bi → (a, b)
    • operator: op ∈ {add,subtract,multiply,divide}
    • operand1:c1=a1+b1i→(a1,b1)
    • operand2:c2=a2+b2i→(a2,b2)
    • message format: m1 : 〈c1;c2;op〉
    • example: 〈(1.0,2.0);(2.0,3.0);add〉
    • status: st ∈ {ok, msgIncomplete, msgFormatUnknown, unknownOperator, c1NotAComplexNumber, c2NotAComplexNumber, serverError}
    • result: cr = ar +bri → (ar,br)
    • message format: m2〈cr;st〉
    • example: 〈(2.0,3.0);ok〉
    • operator: op ∈ {add, subtract, multiply, divide, polar} •
    • notation: n ∈ {(s)tandard,(p)olar}
    • complex:c→(a,b,n)
    • standard: c = a+bi → (a,b,s)
    • polar:c=r∗eφi→(r,φ,p)
  2. see picture
  3. .
    • m1〈(3.0,4.0,s);(1.0,2.0,s);add〉,m2〈(4.0,6.0,s);ok
    • m1〈(3.0,4.0,s);(1.0,2.0,s);subtract〉,m2〈(2.0,2.0,s);ok〉
    • m1〈(3.0,4.0,s);(1.0,2.0,s);multiply〉,m2〈(−5.0,10.0,s);ok〉 •
    • m1〈(3.0,4.0,s);(1.0,2.0,s);divide〉,m2〈(2.2,−0.4,s);ok〉
    • m1〈(3.0,4.0,s);(0.0,0.0,s);polar〉,m2〈(5.0,53.1,p);ok〉
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Remote Procedure Calls (RPCs) are used to execute procedures/methods/functions on remote machines over a communi- cation network. RPCs can be realized with the help of sockets. Consider the following simplified socket API shown in Listing 3.8. Using the Socket class a client can create a connection to the server. Then, commands can be sent by invoking the send(message) method. The ServerSocket class is used by the server side to wait for connections on a particular port.

  • (Use the socket API to write a skeleton for a RPC in pseudocode. At the serverside implement a method provideService (port: int) that establishes a service and answers client requests by returning a result string. At the client implement a method doRPC(command : string,ip : string, port : int). First, initialize a socket connection, then, send a string message rep- resenting the command to the server and wait for the answer of the server.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

(b) Remember Exercise 2. Define a class Complex and implement a procedure marshal(), which converts an instance of Complex to the string format. Define a procedure unmarshal(), which converts the string format back to an object. As library methods you can call the following procedures on a string: parseDouble() : double which takes the string and produces a double value; split(regex : string) which splits the string into an array of substrings at positions matching the given regular expression; and remove(regex : string) which removes substrings matching the given regular expressions from the string.

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

(c) Define a class for the message formats of Exercise 2: M1(request) and M2(response). Also implement the functions marshal() and unmarshal for the message formats. Use delimiters to separate the message parts. (Hint: You can assume that for Operators there exists a class Operator and for messages status there exists a class Status, which both contain the functions marshal() and unmarshal)

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

Use the methods from (a) and the classes Complex, M1 and M2 to implement the message protocol designed in Exercise 2. At the client side define a procedure callRemoteService(c1,c2,op), which takes the arguments, marshals them, and executes the RPC over the socket connection. On server side unmarshal the arguments and dispatch them to a procedure callLocalService(c1,c2,op). Implementing the dispatching and calculation of the complex numbers in the local service is an optional task. Finally, marshal the results and send them back to the client.

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

(e) What are the problems of using delimiters to build/parse object properties? What are alternative ways of doing the marshaling/unmarshaling?

A
  • Problems:
    • The values of object properties should never include one of the delimiters.
    • If a class is changed, parsing the old classes will probably fail
  • Alternatives:
    • Object meta information can be stored into the message → XML
    • Object properties can be serialized in bytes, fixed length intervals can be used → Protocol Buffers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List advantages or disadvantages of asynchronous socket handling compared to synchronous socket handling.

A
  • Advantages:
    • Scalability
    • Slow consumers cannot block the server for a long time
    • One thread can handle multiple sockets
  • Disadvantages:
    • Complex handling code
    • Requires different kind of architecture, Eventloops
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Assume the following simplified API of Java NIO for this exercise, see Listing 5.9. You do not need to include error handling and you do not need to close the sockets. The api is simplified, so all the data can be sent or received in a single write call, no intermediate buffering and sending in multiple send or receives is needed. The methods have the same blocking behavior as in the Java NIO API.

Complete the source code Listing 5.10 with a serverside eventloop in pseudocode which has to following characteristics:

  • multiple clients are able to connected to the server at the same time
  • the server is single threaded, no forking or additional threads should be needed • no additional library function then the simplified below should be needed

public byte[] handleRequest(byte[] input) { //creates a response based on an input

} public void startServer() {

ServerSocketChannel ssc = new ServerSocketChannel() ssc. …

}

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