0% found this document useful (0 votes)
7 views

server program

This document is a C program that implements a simple TCP server. It creates a socket, binds it to a specified port, listens for incoming connections, and handles client communication by echoing received messages back to the client. The server runs indefinitely, accepting and processing multiple client connections.

Uploaded by

Eshan Jinabade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

server program

This document is a C program that implements a simple TCP server. It creates a socket, binds it to a specified port, listens for incoming connections, and handles client communication by echoing received messages back to the client. The server runs indefinitely, accepting and processing multiple client connections.

Uploaded by

Eshan Jinabade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 12345


#define BUFFER_SIZE 1024

int main() {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];

// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}

// Bind socket to IP/Port


server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);

if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)


{
perror("Bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}

// Listen for incoming connections


if (listen(server_fd, 5) < 0) {
perror("Listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}

printf("Server is listening on port %d...\n", PORT);

// Accept client connections and handle communication


while (1) {
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr,
&addr_len)) < 0) {
perror("Accept failed");
close(server_fd);
exit(EXIT_FAILURE);
}

printf("Client connected.\n");

// Echo loop
while (1) {
memset(buffer, 0, BUFFER_SIZE);
int bytes_read = read(client_fd, buffer, BUFFER_SIZE);
if (bytes_read <= 0) {
printf("Client disconnected.\n");
close(client_fd);
break;
}

printf("Received: %s", buffer);


send(client_fd, buffer, bytes_read, 0);
}
}

close(server_fd);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy