server side socket pr explanation
server side socket pr explanation
struct sockaddr_in servaddr;- The line struct sockaddr_in servaddr; declares a structure variable
servaddr of type struct sockaddr_in.
The sockaddr_in structure is part of the sys/socket.h and netinet/in.h headers in C/C++ and is used
to specify addresses for sockets in the IPv4 family.
struct sockaddr_in {
};
1.sin_family: Specifies the address family. For IPv4, this is set to AF_INET., IPV6- AF_INET6.
2. sin_port: Specifies the port number, which needs to be in network byte order. This can be set
using the htons() function to ensure the correct byte order.
3. sin_addr: This is a structure that holds the IP address. The IP address is typically set using the
inet_addr() function or the INADDR_ANY constant if you want to accept connections from any IP
address.
4. sin_zero: This is a padding field to ensure that the sockaddr_in structure is the same size as the
generic sockaddr structure. It is typically initialized to zero and not used in most socket operations.
socket() Function: The socket() function is used to create a socket, which is an endpoint for
communication.
1. Domain (AF_INET): This specifies the address family. In this case, AF_INET is used for IPv4. If you
wanted to use IPv6, you would use AF_INET6.
2. Type (SOCK_STREAM): This defines the type of socket. SOCK_STREAM is used for a stream-
oriented socket, which is TCP (Transmission Control Protocol). TCP is a reliable, connection-oriented
protocol. If you wanted a datagram-oriented protocol like UDP, you would use SOCK_DGRAM.
3. Protocol (0): This argument specifies the protocol to be used. Since SOCK_STREAM already implies
the use of TCP, passing 0 allows the system to choose the default protocol for the given socket type,
which is TCP in this case.
listen_fd: This is an integer variable that will store the file descriptor returned by the socket()
function.
Results
1. If the socket is successfully created, listen_fd will contain a non-negative integer representing the
file descriptor of the socket.
3. htons(12345):
htons() stands for Host to Network Short. It converts the given short integer (in this case, the port
number 12345) from host byte order to network byte order.
Host Byte Order refers to the byte order used by the machine running the program (could be little-
endian or big-endian).
Network Byte Order is always big-endian, which means the most significant byte is stored first. This
is the standard for network communication protocols like TCP/IP.
4. Bind()
bind() ensures that the socket is linked to the specified IP address and port. Without binding, the
server wouldn't know which network interface or port to listen on.
5. Listen()
The listen() function puts the server socket in a passive mode, where it waits for incoming
connection requests from clients. It allows the server to queue up connection requests.
listen(listen_fd, 10);
listen_fd: This is the socket file descriptor returned by the socket() function. It represents the socket
we are binding to an IP address and port.
(struct sockaddr *)&servaddr: This is a pointer to the address structure that contains the IP address
and port to which the socket should be bound.
(struct sockaddr *): This is a typecast to convert the address of the servaddr structure into the
generic struct sockaddr * type required by the bind() function.
servaddr: This is the structure that stores the server's address and port (as previously defined, using
sockaddr_in).
sizeof(servaddr): This specifies the size of the servaddr structure, so bind() knows how many bytes of
the address structure to read.
Return Value:
Returns 0 on success.
listen_fd: This is the file descriptor of the listening socket (created by the socket() function and
bound with bind()). It remains open and can accept additional connections.
(struct sockaddr *)NULL: The second argument is typically used to store the client's address
information (IP and port) in the sockaddr structure. In this case, NULL indicates that the server is not
interested in storing the client's address, so it ignores this information.
if interested to store the client address: // Convert the client's IP address to human-readable form
and print it
NULL: The third argument is the size of the client's address structure. Since the server is not storing
the client's address, this is also set to NULL.
7.
while(1)
recv(comm_fd, str, 100, 0); // Receive data from the client into 'str' buffer
fgets(str, 100, stdin); // Read input from server's stdin (user input)
send(comm_fd, str, strlen(str), 0); // Send the input message back to the client