Socket Programming Flashcards

1
Q

What is socket programming about?

A

It refers to the programming of processes that communicate with eachother using an API known as sockets

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

Describe the data route of a segment through the OSI model from client to server..

A
  1. Web client (app layer) 2. TCP (transport layer) 3. IP (network layer) 4. Ethernet driver (datalink layer) <–> 5. ethernet driver 6. IP 7. TCP 8. webserver.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What transport layer protocols can be used for communication between client and server?

A

UDP (Datagrams, SOCK_DGRAM)
TCP (Connection, SOCK_STREAM)

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

What is the difference between IP and UDP

A

IP:
Operates at the network layer (Layer 3).
Connectionless and handles packet routing and addressing.
Small header with source and destination IP addresses.
Does not provide reliability, error checking, or flow control.
Fundamental to the internet.

UDP:
Operates at the transport layer (Layer 4).
Connectionless and provides a minimal transport layer.
Small header with source and destination port numbers.
Does not guarantee reliability, order, or error handling.
Used for real-time applications with low latency requirements (e.g., streaming, gaming, VoIP).

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

Explain what is taking place in this code:
int main(int argc, char **argv) {
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;

if (argc != 2)
    err_quit("usage: a.out <IPaddress>");

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    err_sys("socket error");

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13); /* daytime server */

if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    err_quit("inet_pton error for %s", argv[1]);

if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
    err_sys("connect error");

while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
    recvline[n] = 0; /* null terminate */
    if (fputs(recvline, stdout) == EOF)
        err_sys("fputs error");
}

if (n < 0)
    err_sys("read error");

exit(0); }
A

This C code initializes a simple network client for retrieving the current time from a daytime server.
0. Parses an IP address from the command line.
1. Creates a TCP socket.
2. Sets up the server’s address (IP and port).
3. Establishes a connection with the server.
4. Reads and prints the time from the server.

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

Explain this code:
int main(int argc, char **argv) {
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;

listenfd = Socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13); /* daytime server */

Bind(listenfd, (SA *)&servaddr, sizeof(servaddr));
Listen(listenfd, LISTENQ);

for (;;) {
    connfd = Accept(listenfd, (SA *)NULL, NULL);
    ticks = time(NULL);
    snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
    Write(connfd, buff, strlen(buff));
    Close(connfd);
} }
A

Simple Daytime Server

  • Creates a TCP server socket.
  • Initializes the server address.
  • Binds the socket to a specific port.
  • Listens for incoming connections.
  • Accepts client connections.
  • Retrieves the current time.
  • Sends the time to connected clients.
  • Closes the client connections in a loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain how a iterative server works(initiates connection)

A

The server process waits for connections in the accept call.
TCP connections are established using a three-way handshake.
accept returns a new descriptor (connfd) after the handshake.
The server handles one client at a time.
Multiple client connections are queued and processed one at a time.

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

What are connect, accept and close, bind, functions in relation to socket programming?

A

connect: Used to establish a connection to a remote server in client code. It initiates the three-way handshake for TCP communication.

accept: Used in server code to accept incoming client connections. It blocks until a client connects and returns a new socket descriptor for communication with the client.

close: Closes a socket when communication is complete. It releases resources associated with the socket and terminates the connection.
socket: creates a socket and defines its properties.
bind: associates the socket with a local IP address and port.
listen: prepares the socket to accept incoming client connections, specifying the maximum backlog of pending connections.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When during establishing a client-server connection is the TCP handshake?

A

After server has created “socket”, “bind” and “listen” and when the client sends a “connect”. Continues hanshake by server sending syn ack and also “accept”

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

Defien three way hanskahe in terms of syn, ack but also in terms of programing functions(accept,bind, listen, socket, connect)

A
  1. SERVER: socket bind listen 1. CLIENT: Syn (Connect), 2. SERVER: Syn-Ack (accept) 3. CLIENT: connect return, ACK . 4. accept returns, read
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
A

testing

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

Describe TCP connection termination with ACK, FIN, SYN and socket programming functions

A
  1. CLIENT: close, FIN 2. SERVER: read returns 0, close, ACK, SEND FIN 3. CLIENT: Receive close, ACK.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Look at the TCP state Transition diagram an EXPLAIN THAT SHIT MF

A

look at the diagram yo

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

What is the state that an active-close-end goes through in TCP’s full-duplex connection termination, and how long does it typically remain in this state?

A

An active-close-end goes through the “TIME_WAIT” state and typically remains in this state for a duration of 2 times the maximum segment lifetime (MSL). The MSL is the maximum time that any IP datagram can live in a network. The recommended value for MSL, as per RFC 1122, is 2 minutes, but BSD systems commonly use 30 seconds, which means the TIME_WAIT duration typically falls between 1 and 4 minutes.

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

What is the purpose of TIME_WAIT state in TCP connection temrination?

A

This waiting period ensures that all potential duplicates from previous incarnations of the connection have been cleared from the network. It ensures that any delayed or duplicate segments related to the recently closed connection have time to expire in the network.

16
Q

What could happen if an endpoint is not in a TIME_WAIT state when a deleyd segment arrives?

A

In such a situation, the delayed segment can be mistakenly considered part of a different connection if it has the appropriate sequence numbers.

17
Q

Why is the TIME_WAIT twice the length of the MSL?

A

Guaranteed that when establishing a connection, all old duplicates from previous incarnations of the connection have expired in the network. Enables reuse of closed connections

18
Q

What does MSL stand for

A

Maximum Segment Lifetime

19
Q

What is IANA?

A

IANA (internet Assigned Numbers Authority) list uses of 10124(?) most common ports.

20
Q

What is ephemeral ports?

A
  • Temporary ports assigned by OS to a connection within a dynamic range. - Enhances security as is becomes hard to predict to which port an application is connected to.
  • Often used on client-side of a client, server connection
21
Q

How does a concurrent server identify which socket to direct (demultiplex) an incoming datagram to?

A

By the 4-tuple that defines the socket. It contains (senderIP,SenderPORT: receiverIP, receiverPORT)

22
Q

What TCP states are there! Do go trhough how they are reached in a client server simulation :)

A

Closed
Listen
Syn_sent
Syn_received
Established
Fin_wait_1
Close_wait
Fin_wait_2
Closing
Last_ack
Time_wait

23
Q

Dow the client have to call bind before calling connect?

A

No, kernel chooses an ephemeral port and source IP if necesary.

24
Q

When does Connect() return for TCP sockets?

A

AFTER the full handshake is completed.

25
Q

What is RST (TCP)?

A

A type of segment that is sent by TCP when something went wrong,

26
Q

Sometimes connect call result in a ICMP error, what does that mean?

A

It could be because an intermediate router is unavailable

27
Q

In relation to TCP states, and server-client connections. What two queues does the kernel keep?

A

SYN_RCVD: contains an entry for every SYN received that are waiting to compelte the handshake
ESTABLISHED: containes an entry for each client with an established connection

28
Q

What is the difference between connected vs listening descriptors?

A

Listening D. : endpoint for client connection REQUESTS, created once and exists for the lifetime of server
Connected D. : endpoint of connection between client and server, exists as long as it takes to service a client

29
Q

Echo server and client…? What do you know about it?

A

Tells us something important about value of I/O multiplexing…?

30
Q

what do you know about byte ordering issues?

A
  • Byte ordering issues can arise when data is transferred between systems with different endianness.
  • To avoid issues, network protocols, and file formats often specify a specific byte order, or include markers to indicate endianness.
  • When reading/writing multibyte data, programmers must account for endianness differences using functions like htons, htonl, ntohs, and ntohl
31
Q

How does the blcoking I/O model work

A

In the Blocking I/O model, also known as synchronous or traditional I/O, operations wait until they complete before returning control to the program.
When an I/O operation is initiated, it blocks (pauses) the execution of the program until the operation is finished.

32
Q

How does non-blocking I/O model work?

A

In the Non-blocking I/O model, I/O operations are initiated without waiting for their completion, allowing the program to continue executing other tasks.
When a non-blocking I/O operation is requested, it returns immediately, even if the operation has not yet finished.
The program can periodically check the status of the I/O operation or use callback functions to be notified when the operation is complete.

33
Q

How does the I/O multiplexing model work?

A

In summary, the I/O Multiplexing model allows a program to efficiently manage multiple I/O sources by monitoring their readiness using mechanisms like select, poll, or epoll. This approach is effective for applications requiring concurrency and responsiveness when handling multiple I/O operations.

34
Q

Describe how functions select, poll and epoll works..

A
  • select and poll use arrays to specify which file descriptors to watch and block until at least one is ready.
  • epoll is more efficient, scales better, and uses an event-driven structure for monitoring file descriptors. It’s a preferred choice for high-performance applications, especially on Linux.
35
Q

What is Batch Input and buffering?

A

Batch Processing:
- Processing data in groups or batches.
- Efficient for large data volumes.
- Common for tasks like payroll processing.

Input Buffering:
- Temporarily storing data before processing.
- Optimizes I/O operations.
- Reduces delays in reading data.

36
Q

What is the difference between blocking, non-blocking and multiplexing I/O models?

A

Blocking I/O:
- Blocks program until I/O operation completes.
- Simple, but can lead to unresponsiveness.

Non-blocking I/O:
- I/O operations return immediately, allowing program to continue.
- Requires polling, suitable for limited I/O.

Multiplexing I/O:
- Uses system calls (e.g., select, poll, epoll) to monitor multiple I/O sources.
- Blocks program until at least one source is ready, efficient for managing many I/O sources concurrently.

37
Q
A
38
Q
A