CN PROGRAM 13-TCP SOCKET PROGRAMMING
CN PROGRAM 13-TCP SOCKET PROGRAMMING
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:
#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