InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What Is Socket Programming? |
|
Answer» Sockets are a generalized networking capability first introduced in 4.1cBSD and SUBSEQUENTLY refined into their current form with 4.2BSD. The sockets feature is available with most current UNIX system releases. (Transport Layer Interface (TLI) is the System V alternative). Sockets allow communication between TWO different processes on the same or different machines. Internet protocols are used by default for communication between machines; other protocols such as DECnet can be used if they are available. To a programmer a socket looks and behaves much like a low level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes. The differences between sockets and normal file descriptors occurs in the creation of a socket and through a variety of special OPERATIONS to control a socket. These operations are different between sockets and normal file descriptors because of the additional complexity in establishing network connections when compared with normal disk access. For most operations using sockets, the roles of client and server must be assigned. A server is a process which does some function on request from a client. As will be seen in this discussion, the roles are not symmetric and cannot be reversed without some effort. This description of the use of sockets progresses in three stages:
The connectionless or datagram mode between client and server on different hosts is not explicitly discussed here. Its use can be inferred from the presentations MADE in Stages 1 and 3. Sockets are a generalized networking capability first introduced in 4.1cBSD and subsequently refined into their current form with 4.2BSD. The sockets feature is available with most current UNIX system releases. (Transport Layer Interface (TLI) is the System V alternative). Sockets allow communication between two different processes on the same or different machines. Internet protocols are used by default for communication between machines; other protocols such as DECnet can be used if they are available. To a programmer a socket looks and behaves much like a low level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes. The differences between sockets and normal file descriptors occurs in the creation of a socket and through a variety of special operations to control a socket. These operations are different between sockets and normal file descriptors because of the additional complexity in establishing network connections when compared with normal disk access. For most operations using sockets, the roles of client and server must be assigned. A server is a process which does some function on request from a client. As will be seen in this discussion, the roles are not symmetric and cannot be reversed without some effort. This description of the use of sockets progresses in three stages: The connectionless or datagram mode between client and server on different hosts is not explicitly discussed here. Its use can be inferred from the presentations made in Stages 1 and 3. |
|
| 2. |
What This Function Socket() Does? |
|
Answer» Socket Creation Using socket() socket() is very SIMILAR to socketpair() EXCEPT that only one socket is created instead of TWO. This is most commonly used if the process you wish to communicate with is not a child process. The af, type, and protocol fields are used just as in the socketpair() system call. On success, a file descriptor to the socket is returned. On failure, -1 is returned and errno describes the PROBLEM. Socket Creation Using socket() socket() is very similar to socketpair() except that only one socket is created instead of two. This is most commonly used if the process you wish to communicate with is not a child process. The af, type, and protocol fields are used just as in the socketpair() system call. On success, a file descriptor to the socket is returned. On failure, -1 is returned and errno describes the problem. |
|
| 3. |
What This Function Bind() Does? |
|
Answer» Giving a Socket a NAME - bind() #include <ltsys/types.h> Recall that, using socketpair(), sockets could only be shared between parent and child PROCESSES or children of the same parent. With a name attached to the socket, any process on the SYSTEM can describe (and use) it. In a CALL to bind(), s is the file descriptor for the socket, obtained from the call to socket(). name is a pointer to a structure of type sockaddr. If the address family is AF_UNIX (as specified when the socket is created), the structure is defined as follows: struct sockaddr { name.sa_family should be AF_UNIX. name.sa_data should contain up to 14 bytes of a file name which will be ASSIGNED to the socket. namelen gives the actual length of name, that is, the length of the initialized contents of the data structure. A value of 0 is return on success. On failure, -1 is returned with errno describing the error. Example: struct sockaddr name;
Giving a Socket a Name - bind() #include <ltsys/types.h> Recall that, using socketpair(), sockets could only be shared between parent and child processes or children of the same parent. With a name attached to the socket, any process on the system can describe (and use) it. In a call to bind(), s is the file descriptor for the socket, obtained from the call to socket(). name is a pointer to a structure of type sockaddr. If the address family is AF_UNIX (as specified when the socket is created), the structure is defined as follows: struct sockaddr { name.sa_family should be AF_UNIX. name.sa_data should contain up to 14 bytes of a file name which will be assigned to the socket. namelen gives the actual length of name, that is, the length of the initialized contents of the data structure. A value of 0 is return on success. On failure, -1 is returned with errno describing the error. Example: struct sockaddr name;
|
|
| 4. |
What This Function Connect() Does? |
|
Answer» Specifying a Remote Socket - CONNECT() #include <ltsys/types.h>
struct sockaddr { A sample CODE fragment: Specifying a Remote Socket - connect() #include <ltsys/types.h>
struct sockaddr { A sample code fragment: |
|
| 5. |
What This Function Sendto() Does? |
|
Answer» Sending to a Named Socket - sendto() int sendto(int s, char *msg, int len, int FLAGS, STRUCT sockaddr *to, int tolen) This function allows a message msg of length len to be sent on a socket with descriptor s to the socket named by to and tolen, where tolen is the actual length of to. flags will always be zero for our purposes. The number of characters sent is the return value of the function. On error, -1 is returned and errno describes the error. An example: struct sockaddr to_name; Sending to a Named Socket - sendto() int sendto(int s, char *msg, int len, int flags, struct sockaddr *to, int tolen) This function allows a message msg of length len to be sent on a socket with descriptor s to the socket named by to and tolen, where tolen is the actual length of to. flags will always be zero for our purposes. The number of characters sent is the return value of the function. On error, -1 is returned and errno describes the error. An example: struct sockaddr to_name; |
|
| 6. |
What This Function Recvfrom() Does? |
|
Answer» Receiving on a Named Socket - recvfrom() If no message is available to be read, the process will suspend waiting for ONE UNLESS the socket is set to nonblocking mode (via an ioctl call). The system I/O call read() can ALSO be used to read data from a socket. Receiving on a Named Socket - recvfrom() If no message is available to be read, the process will suspend waiting for one unless the socket is set to nonblocking mode (via an ioctl call). The system I/O call read() can also be used to read data from a socket. |
|
| 7. |
How To Disposing Of A Socket? |
|
Answer» Code Sample: How to disposing of a Socket : #include <ltstdio.h> Example - sendto() and recvfrom() exit(1); } buf[cnt] = ''; /* assure NULL BYTE */ /* sender */ } Code Sample: How to disposing of a Socket : #include <ltstdio.h> Example - sendto() and recvfrom() exit(1); } buf[cnt] = ''; /* assure null byte */ /* sender */ } |
|
| 8. |
How Sockets Can Be Used To Write Client-server Applications Using A Connection-oriented Client-server Technique? |
|
Answer» Sockets can be used to write client-server applications using a connection-oriented client-server technique. Some characteristics of this technique include:
The client-server connection, when established, remains in existence until either the client or the server explicitly breaks it, much like a trouble-free telephone CALL. The socket used for this connection is called a connection-oriented socket. The socket type is specified as SOCK_STREAM. As a result, the process receiving a message processes that message by the following rules:
Functions listen() and accept() enable the server to listen for service requests. read() and write() may be used by client and server to send/receive messages; send() and recv() may also be used. Sockets can be used to write client-server applications using a connection-oriented client-server technique. Some characteristics of this technique include: The client-server connection, when established, remains in existence until either the client or the server explicitly breaks it, much like a trouble-free telephone call. The socket used for this connection is called a connection-oriented socket. The socket type is specified as SOCK_STREAM. As a result, the process receiving a message processes that message by the following rules: Functions listen() and accept() enable the server to listen for service requests. read() and write() may be used by client and server to send/receive messages; send() and recv() may also be used. |
|
| 9. |
How To Make A Socket A Listen-only Connection Endpoint - Listen()? |
|
Answer» /* */ */ /* */ */ |
|
| 10. |
Explain Connection Establishment By Server - Accept()? |
|
Answer» #include <ltsys/types.h> The accept() call establishes a client-server connection on the server side. (The client requests the connection using the connect() system call.) The server must have CREATED the socket using socket(), given the socket a name using bind(), and established a listen queue using listen(). sockfd is the socket file descriptor returned from the socket() system call. name is a pointer to a structure of type sockaddr as described above struct sockaddr { Upon successful return from accept(), this structure will contain the protocol address of the client's socket. The data area pointed to by namelen should be initialized to the ACTUAL LENGTH of name. Upon successful return from accept, the data area pointed to by namelen will contain the actual length of the protocol address of the client's socket. If successful, accept() creates a new socket of the same FAMILY, type, and protocol as sockfd. The file descriptor for this new socket is the return value of accept(). This new socket is used for all communications with the client. If there is no client connection request waiting, accept() will block until a client request is queued. accept() will fail mainly if sockfd is not a file descriptor for a socket or if the socket type is not SOCK_STREAM. In this case, accept() returns the value -1 and errno describes the problem. #include <ltsys/types.h> The accept() call establishes a client-server connection on the server side. (The client requests the connection using the connect() system call.) The server must have created the socket using socket(), given the socket a name using bind(), and established a listen queue using listen(). sockfd is the socket file descriptor returned from the socket() system call. name is a pointer to a structure of type sockaddr as described above struct sockaddr { Upon successful return from accept(), this structure will contain the protocol address of the client's socket. The data area pointed to by namelen should be initialized to the actual length of name. Upon successful return from accept, the data area pointed to by namelen will contain the actual length of the protocol address of the client's socket. If successful, accept() creates a new socket of the same family, type, and protocol as sockfd. The file descriptor for this new socket is the return value of accept(). This new socket is used for all communications with the client. If there is no client connection request waiting, accept() will block until a client request is queued. accept() will fail mainly if sockfd is not a file descriptor for a socket or if the socket type is not SOCK_STREAM. In this case, accept() returns the value -1 and errno describes the problem. |
|
| 11. |
Explain Data Transfer Over Connected Sockets - Send() And Recv()? |
|
Answer» Two additional DATA transfer library calls, namely send() and recv(), are available if the sockets are CONNECTED. They correspond very closely to the read() and write() functions used for I/O on ordinary file descriptors. #include <ltsys/types.h> In both cases, sd is the socket descriptor. For send(), buf points to a buffer containing the data to be sent, len is the length of the data and flags will usually be 0. The return value is the number of bytes sent if successful. If not successful, -1 is returned and errno describes the error. For recv(), buf points to a data area into which the received data is copied, len is the size of this data area in bytes, and flags is usually either 0 or set to MSG_PEEK if the received data is to be retained in the system after it is received. The return value is the number of bytes received if successful. If not successful, -1 is returned and errno describes the error. Two additional data transfer library calls, namely send() and recv(), are available if the sockets are connected. They correspond very closely to the read() and write() functions used for I/O on ordinary file descriptors. #include <ltsys/types.h> In both cases, sd is the socket descriptor. For send(), buf points to a buffer containing the data to be sent, len is the length of the data and flags will usually be 0. The return value is the number of bytes sent if successful. If not successful, -1 is returned and errno describes the error. For recv(), buf points to a data area into which the received data is copied, len is the size of this data area in bytes, and flags is usually either 0 or set to MSG_PEEK if the received data is to be retained in the system after it is received. The return value is the number of bytes received if successful. If not successful, -1 is returned and errno describes the error. |
|
| 12. |
How Do I Close Sockets? |
|
Answer» You should always CLOSE the output and input stream before you close the socket: On the client side:
You should always close the output and input stream before you close the socket: On the client side:
|
|
| 13. |
How Do I Create An Output Stream? |
|
Answer» On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream of java.io: PrintStream output; The class PrintStream has methods for DISPLAYING textual representation of Java PRIMITIVE data types. Its Write and println methods are important here. Also, you may want to USE the DataOutputStream: DataOutputStream output; The class DataOutputStream allows you to write Java primitive data types; many of its methods write a single Java primitive type to the output stream. The method writeBytes is a useful one. On the server side, you can use the class PrintStream to send information to the client. PrintStream output; Note: You can use the class DataOutputStream as mentioned above. On the client side, you can create an output stream to send information to the server socket using the class PrintStream or DataOutputStream of java.io: PrintStream output; The class PrintStream has methods for displaying textual representation of Java primitive data types. Its Write and println methods are important here. Also, you may want to use the DataOutputStream: DataOutputStream output; The class DataOutputStream allows you to write Java primitive data types; many of its methods write a single Java primitive type to the output stream. The method writeBytes is a useful one. On the server side, you can use the class PrintStream to send information to the client. PrintStream output; Note: You can use the class DataOutputStream as mentioned above. |
|
| 14. |
How Do I Create An Input Stream? |
|
Answer» On the client SIDE, you can use the DATAINPUTSTREAM class to create an INPUT stream to RECEIVE response from the server: DataInputStream input; On the server side, you can use DataInputStream to receive input from the client: DataInputStream input; On the client side, you can use the DataInputStream class to create an input stream to receive response from the server: DataInputStream input; On the server side, you can use DataInputStream to receive input from the client: DataInputStream input; |
|
| 15. |
How Do I Open A Socket? |
|
Answer» If you are programming a client, then you would open a SOCKET like this: Socket MyClient; MyClient = new Socket("Machine name", PortNumber); Where Machine name is the machine you are trying to open a connection to, and PortNumber is the port (a number) on which the SERVER you are trying to connect to is running. When selecting a port number, you should NOTE that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root). These port numbers are reserved for standard services, such as email, FTP, and HTTP. When selecting a port number for your server, select one that is greater than 1,023! In the example above, we didn't make use of exception handling, however, it is a good idea to handle exceptions. (From now on, all our code will handle exceptions!) The above can be written as: Socket MyClient; ServerSocket MyService; When implementing a server you also need to create a socket object from the ServerSocket in order to listen for and accept connections from CLIENTS. Socket clientSocket = null; If you are programming a client, then you would open a socket like this: Socket MyClient; MyClient = new Socket("Machine name", PortNumber); Where Machine name is the machine you are trying to open a connection to, and PortNumber is the port (a number) on which the server you are trying to connect to is running. When selecting a port number, you should note that port numbers between 0 and 1,023 are reserved for privileged users (that is, super user or root). These port numbers are reserved for standard services, such as email, FTP, and HTTP. When selecting a port number for your server, select one that is greater than 1,023! In the example above, we didn't make use of exception handling, however, it is a good idea to handle exceptions. (From now on, all our code will handle exceptions!) The above can be written as: Socket MyClient; ServerSocket MyService; When implementing a server you also need to create a socket object from the ServerSocket in order to listen for and accept connections from clients. Socket clientSocket = null; |
|
| 16. |
What Is Encapsulation Technique? |
|
Answer» Hiding data WITHIN the class and MAKING it available only through the methods. This technique is USED to protect your class against accidental changes to fields, which might leave the class in an INCONSISTENT state. Hiding data within the class and making it available only through the methods. This technique is used to protect your class against accidental changes to fields, which might leave the class in an inconsistent state. |
|
| 17. |
What Is The Difference Between A Null Pointer And A Void Pointer? |
|
Answer» A NULL pointer is a pointer of any type whose value is ZERO. A void pointer is a pointer to an object of an UNKNOWN type, and is guaranteed to have enough BITS to hold a pointer to any object. A void pointer is not guaranteed to have enough bits to point to a function (THOUGH in general practice it does). A NULL pointer is a pointer of any type whose value is zero. A void pointer is a pointer to an object of an unknown type, and is guaranteed to have enough bits to hold a pointer to any object. A void pointer is not guaranteed to have enough bits to point to a function (though in general practice it does). |
|
| 18. |
What Are Some Advantages And Disadvantages Of Java Sockets? |
|
Answer» Advantages of Java Sockets:
Disadvantages of Java Sockets:
Advantages of Java Sockets: Disadvantages of Java Sockets: |
|
| 19. |
What Are The Seven Layers(osi Model) Of Networking? |
Answer»
|
|
| 20. |
What Is A Javabean? |
|
Answer» JavaBeans are reusable SOFTWARE components written in the Java programming LANGUAGE, designed to be manipulated visually by a software develpoment environment, like JBuilder or VisualAge for Java. They are similar to MICROSOFT’s ActiveX components, but designed to be platform-neutral, running ANYWHERE there is a Java Virtual MACHINE (JVM). JavaBeans are reusable software components written in the Java programming language, designed to be manipulated visually by a software develpoment environment, like JBuilder or VisualAge for Java. They are similar to Microsoft’s ActiveX components, but designed to be platform-neutral, running anywhere there is a Java Virtual Machine (JVM). |
|
| 21. |
What Does A Socket Consists Of? |
|
Answer» The combination of an IP ADDRESS and a PORT number is CALLED a SOCKET. The combination of an IP address and a port number is called a socket. |
|
| 22. |
Name The Seven Layers Of The Osi Model And Describe Them Briefly? |
Answer»
|
|
| 23. |
What Is Multiprogramming? |
|
Answer» MULTIPROGRAMMING is a rapid SWITCHING of the CPU BACK and FORTH between PROCESSES. Multiprogramming is a rapid switching of the CPU back and forth between processes. |
|
| 24. |
How Does The Race Condition Occur? |
|
Answer» It occurs when two or more processes are READING or WRITING some shared DATA and the final result DEPENDS on who runs precisely when. It occurs when two or more processes are reading or writing some shared data and the final result depends on who runs precisely when. |
|