Server Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>: Includes the standard input/output library necessary for functions like printf and fgets.#include <stdlib.h>: Includes the standard library header for functions like exit.#include <string.h>: Includes the string library header for functions like memset and strlen.#include <sys/socket.h>: Includes definitions for socket functions and structures.#include <netinet/in.h>: Includes constants and structures needed for internet domain addresses.#include <unistd.h>: Includes definitions for miscellaneous functions, including close.
- Defines a constant
PORT with the value 8080, which will be used as the port number for the server.
int main() {
struct sockaddr_in server, client;
char buff[100];
int sid = socket(AF_INET, SOCK_STREAM, 0);
int main() {: Entry point of the program.struct sockaddr_in server, client;: Declares structures to hold the server's and client's internet addresses.char buff[100];: Declares a buffer to store messages sent and received.int sid = socket(AF_INET, SOCK_STREAM, 0);: Creates a socket with:AF_INET: Address family for IPv4.SOCK_STREAM: Socket type for TCP.0: Default protocol for TCP.
if (sid == -1) {
printf("Error in socket creation\n");
exit(1);
}
- Checks if
sid is -1, indicating an error in socket creation. If so, prints an error message and exits the program with status 1.
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
server.sin_family = AF_INET;: Sets the address family to IPv4.server.sin_addr.s_addr = INADDR_ANY;: Binds the socket to all available network interfaces.server.sin_port = htons(PORT);: Sets the port number to 8080, converting it to network byte order using htons.
if (bind(sid, (struct sockaddr*)&server, sizeof(server)) == -1) {
printf("Error in bind\n");
close(sid);
exit(1);
}
- Binds the socket to the address and port specified in the
server structure. Checks if bind returns -1, indicating an error. If so, prints an error message, closes the socket, and exits the program.
- Puts the socket into listening mode, ready to accept incoming connections.
5 specifies the maximum length of the queue of pending connections.
socklen_t len = sizeof(client);
int temp = accept(sid, (struct sockaddr*)&client, &len);
socklen_t len = sizeof(client);: Initializes len to the size of the client structure.int temp = accept(sid, (struct sockaddr*)&client, &len);: Accepts an incoming connection. Returns a new socket descriptor (temp) for the connection. If accept returns -1, it indicates an error.
if (temp == -1) {
printf("Error in accept\n");
close(sid);
exit(1);
}
- Checks if
temp is -1, indicating an error in accepting the connection. If so, prints an error message, closes the listening socket, and exits the program.
while (1) {
memset(buff, 0, sizeof(buff));
if (recv(temp, buff, sizeof(buff), 0) <= 0) {
printf("Client disconnected\n");
break;
}
printf("Client: %s\n", buff);
while (1) {: Infinite loop for continuous communication.memset(buff, 0, sizeof(buff));: Clears the buffer.if (recv(temp, buff, sizeof(buff), 0) <= 0) {: Receives data from the client. If recv returns 0 or a negative value, it indicates the client has disconnected.printf("Client: %s\n", buff);: Prints the received message from the client.
printf("Enter message to send: ");
fgets(buff, sizeof(buff), stdin);
if (send(temp, buff, strlen(buff), 0) == -1) {
printf("Error in send\n");
break;
}
}
- Prompts the user to enter a message to send to the client.
fgets(buff, sizeof(buff), stdin);: Reads a line of input from the user into buff.if (send(temp, buff, strlen(buff), 0) == -1) {: Sends the message to the client. If send returns -1, it indicates an error.
close(temp);
close(sid);
return 0;
}
close(temp);: Closes the connection socket.close(sid);: Closes the listening socket.return 0;: Exits the program with status 0.
Client Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
- Similar header inclusions as the server code for necessary functions and structures.
- Defines the port number
8080 to connect to the server.
int main() {
struct sockaddr_in client;
char buff[100];
int sid = socket(AF_INET, SOCK_STREAM, 0);
int main() {: Entry point of the program.struct sockaddr_in client;: Declares a structure to hold the client's internet address.char buff[100];: Declares a buffer to store messages sent and received.int sid = socket(AF_INET, SOCK_STREAM, 0);: Creates a socket with similar parameters as the server.
if (sid == -1) {
printf("Error in socket creation\n");
exit(1);
}
- Checks if
sid is -1, indicating an error in socket creation. If so, prints an error message and exits the program.
client.sin_family = AF_INET;
client.sin_addr.s_addr = INADDR_ANY;
client.sin_port = htons(PORT);
client.sin_family = AF_INET;: Sets the address family to IPv4.client.sin_addr.s_addr = INADDR_ANY;: Specifies that the client can connect to any available network interface.client.sin_port = htons(PORT);: Sets the port number to 8080, converting it to network byte order using htons.
if (connect(sid, (struct sockaddr*)&client, sizeof(client)) == -1) {
printf("Error in connect\n");
close(sid);
exit(1);
}
- Attempts to connect to the server. Checks if
connect returns -1, indicating an error. If so, prints an error message, closes the socket, and exits the program.
while (1) {
printf("Enter message to send: ");
fgets(buff, sizeof(buff), stdin);
if (send(sid, buff, strlen(buff), 0) == -1) {
printf("Error in send\n");
break;
}
while (1) {: Infinite loop for continuous communication.- Prompts the user to enter a message to send to the server.
fgets(buff, sizeof(buff), stdin);: Reads a line of input from the user into buff.if (send(sid, buff, strlen(buff), 0) == -1) {: Sends the message to the server. If send returns -1, it indicates an error.
memset(buff, 0, sizeof(buff));
if (recv(sid, buff, sizeof(buff), 0) <= 0) {
printf("Server disconnected\n");
break;
}
printf("Server: %s\n", buff);
}
memset(buff, 0, sizeof(buff));: Clears the buffer.if (recv(sid, buff, sizeof(buff), 0) <= 0) {: Receives data from the server. If recv returns 0 or a negative value, it indicates the server has disconnected.printf("Server: %s\n", buff);: Prints the received message from the server.
close(sid);: Closes the socket.return 0;: Exits the program with status 0.
Summary
- The server sets up a listening socket and waits for client connections.
- Upon accepting a connection, it enters a loop to receive and send messages.
- The client creates a socket, connects to the server, and enters a loop to send and receive messages.
- Both the client and server handle errors and clean up resources by closing sockets before exiting.