Server Code
c#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h> // For close() function
#include <stdio.h>
: Includes the standard input/output library necessary for functions likeprintf
andfgets
.#include <stdlib.h>
: Includes the standard library header for functions likeexit
.#include <string.h>
: Includes the string library header for functions likememset
andstrlen
.#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, includingclose
.
c#define PORT 8080
- Defines a constant
PORT
with the value8080
, which will be used as the port number for the server.
cint 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.
c 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 status1
.
cserver.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 to8080
, converting it to network byte order usinghtons
.
c 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 ifbind
returns-1
, indicating an error. If so, prints an error message, closes the socket, and exits the program.
c listen(sid, 5);
- Puts the socket into listening mode, ready to accept incoming connections.
5
specifies the maximum length of the queue of pending connections.
c socklen_t len = sizeof(client);
int temp = accept(sid, (struct sockaddr*)&client, &len);
socklen_t len = sizeof(client);
: Initializeslen
to the size of theclient
structure.int temp = accept(sid, (struct sockaddr*)&client, &len);
: Accepts an incoming connection. Returns a new socket descriptor (temp
) for the connection. Ifaccept
returns-1
, it indicates an error.
c 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.
c 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. Ifrecv
returns0
or a negative value, it indicates the client has disconnected.printf("Client: %s\n", buff);
: Prints the received message from the client.
c 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 intobuff
.if (send(temp, buff, strlen(buff), 0) == -1) {
: Sends the message to the client. Ifsend
returns-1
, it indicates an error.
c close(temp);
close(sid);
return 0;
}
close(temp);
: Closes the connection socket.close(sid);
: Closes the listening socket.return 0;
: Exits the program with status0
.
Client Code
c#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h> // For close() function
- Similar header inclusions as the server code for necessary functions and structures.
c#define PORT 8080
- Defines the port number
8080
to connect to the server.
cint 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.
c 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.
cclient.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 to8080
, converting it to network byte order usinghtons
.
c 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.
c 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 intobuff
.if (send(sid, buff, strlen(buff), 0) == -1) {
: Sends the message to the server. Ifsend
returns-1
, it indicates an error.
c 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. Ifrecv
returns0
or a negative value, it indicates the server has disconnected.printf("Server: %s\n", buff);
: Prints the received message from the server.
c close(sid);
return 0;
}
close(sid);
: Closes the socket.return 0;
: Exits the program with status0
.
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.