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

CN PROGRAM 13-TCP SOCKET PROGRAMMING

The document outlines TCP socket programming in C for both iterative and concurrent servers using the Berkeley Sockets API in Linux. It details the server-client model, including steps for server and client-side socket creation, connection, communication, and closure. Example code for both server and client implementations is provided, demonstrating a simple chat application over TCP.

Uploaded by

akilanabinayaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CN PROGRAM 13-TCP SOCKET PROGRAMMING

The document outlines TCP socket programming in C for both iterative and concurrent servers using the Berkeley Sockets API in Linux. It details the server-client model, including steps for server and client-side socket creation, connection, communication, and closure. Example code for both server and client implementations is provided, demonstrating a simple chat application over TCP.

Uploaded by

akilanabinayaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

13.

TCP socket programming using C or LINUX (iterative and concurrent server)


 TCP (Transmission Control Protocol) enables reliable, connection-based
communication between a client and server.
 In Linux, socket programming uses the Berkeley Sockets API for network
communication.
TCP Server-Client Model
 Server: Listens for client connections and processes requests.
 Client: Connects to the server and exchanges data.
Types of Servers:
 Iterative Server: Handles one client at a time.
 Concurrent Server: Handles multiple clients using processes (fork) or threads.

Server-Side Steps
 Create socket → socket( )
 Bind to IP & port → bind( )
 Listen for clients → listen( )
 Accept client connection → accept( )
 Communicate (send/receive data) → send( ), recv( )
 Close connection → close( )
Client-Side Steps
 Create socket → socket( )
 Connect to server → connect( )
 Send & receive data → send(), recv( )
 Close connection → close( )

Program:

Server Code (server.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#define PORT 8080 // Server port number
#define MAX 80 // Buffer size for messages
// Function to handle chat between server and client
void chat(int connfd) {
char buff[MAX];
while (1) {
bzero(buff, MAX); // Clear the buffer
read(connfd, buff, sizeof(buff)); // Read message from client
printf("Client: %s", buff); // Display client message
// Check if client wants to exit
if (strncmp(buff, "exit", 4) == 0) {
printf("Server exiting...\n");
break;
}
printf("You: "); // Prompt server to enter response
fgets(buff, MAX, stdin); // Read input from server
write(connfd, buff, strlen(buff)); // Send response to client
}
}
int main() {
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
socklen_t len = sizeof(cli);
// Create a socket (IPv4, TCP)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Socket creation failed");
exit(1);
}
// Configure server address structure
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // Accept connections on any local IP
servaddr.sin_port = htons(PORT); // Set the port
// Bind the socket to the specified IP and port
if (bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
perror("Bind failed");
exit(1);
}
// Start listening for incoming connections
listen(sockfd, 5);
printf("Server listening on port %d...\n", PORT);
// Accept a client connection
connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
if (connfd < 0) {
perror("Accept failed");
exit(1);
}
printf("Client connected.\n");
// Start chat session
chat(connfd);
// Close the socket after chat ends
close(sockfd);
return 0;
}
Client Code (client.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define PORT 8080 // Server port number
#define MAX 80 // Buffer size for messages
// Function to handle chat between client and server
void chat(int sockfd) {
char buff[MAX];
while (1) {
printf("You: "); // Prompt client to enter message
fgets(buff, MAX, stdin); // Read input from user
write(sockfd, buff, strlen(buff)); // Send message to server
// Check if client wants to exit
if (strncmp(buff, "exit", 4) == 0) {
printf("Client exiting...\n");
break;
}
bzero(buff, MAX); // Clear the buffer
read(sockfd, buff, sizeof(buff)); // Read response from server
printf("Server: %s", buff); // Display server response
}
}
int main() {
int sockfd;
struct sockaddr_in servaddr;
// Create a socket (IPv4, TCP)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Socket creation failed");
exit(1);
}
// Configure server address structure
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT); // Set server port
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Connect to localhost
// Connect to the server
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
perror("Connection failed");
exit(1);
}
printf("Connected to server.\n");
// Start chat session
chat(sockfd);
// Close the socket after chat ends
close(sockfd);
return 0;
}

OUTPUT:

server.c client.c

You might also like