16,604 views
46 votes
46 votes

A client process P needs to make a TCP connection to a server process S. Consider the following situation: the server process S executes a $\text{socket()}$, a $\text{bind()}$ and a $\text{listen()}$ system call in that order, following which it is preempted. Subsequently, the client process P executes a $\text{socket()}$ system call followed by $\text{connect()}$ system call to connect to the server process S. The server process has not executed any $\text{accept()}$ system call. Which one of the following events could take place?

  1. $\text{connect()}$ system call returns successfully

  2. $\text{connect()}$ system call blocks

  3. $\text{connect()}$ system call returns an error

  4. $\text{connect()}$ system call results in a core dump

3 Answers

Best answer
111 votes
111 votes

First thing to note: All the sockets are by default in BLOCKING mode. What do we mean by blocking ??

Blocking mode means that when we make a system call, it blocks the caller for the time "when call() is made till the job is done OR an error returns ". We can set each socket to Non-blocking explicitly. Setting to Non-Blocking means we are telling the kernel that "If the system call cant be completed without putting process to sleep then DON'T put the process to sleep . Instead return with an ERROR immediately and continue the process" which can be checked for the completion by the caller in between the execution of other tasks.

Now coming to this question:

Suppose connect() is in default blocking mode then calling connect() sends SYN packet to the server. Since server has not executed any accept() call it can not acknowledge the SYN packet. Connect() in blocking mode keep sending SYN packets at fixed intervals(first after 6 sec, second after 24 sec typically until 75 sec latest). This is done until an error ETIMEDOUT is returned by the TCP.(in this case,else there are several other type of errors returned in case No port exists for that connection or server id not listening etc.)

Here, option (B) saying that connect() blocks is not entirely wrong but since we know that accept() call is not made by server, connect() WILL NOT WAIT FOREVER and SO IT CAN NOT BLOCK. It will ultimately return with an ERROR message.

So, option (C) is CORRECT.

Core dump thing I don't know about!

But once connect() returns error that socket can not be reused and must be CLOSED.

And a non-blocking connect() is never blocked and immediately returns with an error if connection is not successful although IT CONTINUES WITH TRYING TO CONNECT .Error here just means that it returns a message saying "I could not connect immediately BUT i am trying AND you can check it in between.

Hope it clears a bit.

edited by
45 votes
45 votes

Simple it is. given that the server process is preempted before client requests anything. It means that calling the connect() system call will try to establish a TCP connection (will send SYN) but since the server at the expected port is not present at the destination, a host unreachable - port unreachable error will be returned by the ICMP.

8 votes
8 votes
Everything is black.

if you will remember this proverb you will easily understand how a connection can be established in a TCP connection.

S B L A C

S STANDS FOR SOCKET.

B STANDS FOR BIND.

L STANDS FOR LISTEN.

A STANDS FOR ACKNOWLEDGE

C STANDS FOR CONNECT

Connection is always made via this series of steps.

now as you can see in the question it is given that the client process p executes a socket system call followed by directly connect now everyone knows that connect system call require bind and listen which has not occurred so this will return an error that's why this will return an error.
Answer:

Related questions

28 votes
28 votes
4 answers
1
Kathleen asked Sep 11, 2014
14,982 views
Which of the following system calls results in the sending of SYN packets?$\textsf{socket}$$\textsf{bind}$$\textsf{listen}$$\textsf{connect}$
26 votes
26 votes
4 answers
2
Kathleen asked Sep 12, 2014
10,002 views
In the slow start phase of the TCP congestion algorithm, the size of the congestion window:does not increaseincrease linearlyincreases quadraticallyincreases exponentiall...
27 votes
27 votes
4 answers
3
34 votes
34 votes
3 answers
4
go_editor asked Sep 28, 2014
11,128 views
Which of the following socket API functions converts an unconnected active TCP socket into a passive socket?$\textsf{connect}$$\textsf{bind}$$\textsf{listen}$$\textsf{acc...