Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following questions refer to two socket descriptors which were created succe

ID: 3837260 • Letter: T

Question

The following questions refer to two socket descriptors which were created successfully with the following code and have not been set to be non-blocking in any way: int udp _sock = socket (PF_INET SOCK_DGRAM, IPPROTO_UDP); int tcp_ sock = socket (PF_INET SOCK_STREAM, IPPROTO_TCP); What does a successful call to connect () do if the first parameter is udp_ sock? tcp_ sock? What does a successful call to bind () do if the first parameter is udp_ sock? tcp_ sock? On which of the following sockets should close be called when your program is finished using it? udp_ sock. tcp_ sock both

Explanation / Answer

1. (a) when we call connect() to udp_sock as first parameter, then the datagram may only call send() and recv() if it has first called connect(). The datagram socket is open when sending and receiving the data else it will be closed all the time. This is ensured by SOCK_DGRAM. The datagram can send messages via sendto() and the datagram receiver can receive them via recvfrom(), but the receiver can't send message back to the sender.

(b) when we call connect() to tcp_sock as first parameter , it attempts to form a connection to the socket that's specified by remote_interface. A client socket can only be connected to one listener at a time. It will remains open until it is told to close. That is maintained by SOCK_STREAM.

2.

(a) UDP sockets donot connect, however you have to bind them to a specific port if you want to be able to receive data on that port. UDP is connectionless protocol. A UDP socket cannot receive on multiple ports simultaneously. After binding the socket to a port, it's ready to receive data on that port.

(b) Same as UDP, TCP sockets require bind() to bind a socket to a address.This is, typically, a socket of type SOCK_STREAM. The TCP are bound to the ports and addresses using bind() so that receiver can receive the data because it has complete address.

3. For both udp_sock and tcp_sock, the sockets should close().