0% found this document useful (0 votes)
47 views8 pages

TCP Socket Communication in C

Uploaded by

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

TCP Socket Communication in C

Uploaded by

sri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name : Vedant S Kudalkar

Reg no : 22BAI1321
Lab 5 – Computer Networks
TCP Socket Creation

The C programs exemplify TCP socket communication, a fundamental


concept in computer networking. The server program initializes a socket,
binds it, and listens for connections, while the client program creates a
socket and connects to the server. These programs illustrate the
foundational steps for enabling robust data exchange between systems,
showcasing the core principles of networked communication in C
programming.

Server Algorithm (server.c):

1)Initialize Server:
Create and configure a server socket.
Initialize the server address structure (IP address and port).
Bind the socket to the specified address and port.
Listen for incoming connections.

2)Accept Connection:
Accept a client connection.

3)Handle Connection:
Perform server actions with the connected client.
4)Close Connection:
Close the client socket.
Close the server socket when done.

Server Program

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

#define PORT 12345

int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);

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

// Initialize server address structure


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

// Bind the socket


if (bind(server_socket, (struct sockaddr*)&server_addr,
sizeof(server_addr)) == -1) {
perror("Bind failed");
exit(EXIT_FAILURE);
}

// Listen for incoming connections


if (listen(server_socket, 5) == -1) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", PORT);

// Accept a connection
if ((client_socket = accept(server_socket, (struct
sockaddr*)&client_addr, &client_len)) == -1) {
perror("Accept failed");
exit(EXIT_FAILURE);
}

printf("Connection accepted from %s:%d\n",


inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

// Do something with the client connection...

// Close sockets
close(client_socket);
close(server_socket);

return 0;
}

Client Algorithm (client.c):

1)Initialize Client:
Create a client socket.
Configure the server address structure (server's IP address and port).

2)Connect to Server:
Connect the client socket to the server.

3)Handle Connection:
Perform client actions with the connected server.

4)Close Connection:
Close the client socket when done.

Client Program

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

#define PORT 12345

int main() {
int client_socket;
struct sockaddr_in server_addr;

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

// Initialize server address structure


server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Server IP
address
server_addr.sin_port = htons(PORT);

// Connect to the server


if (connect(client_socket, (struct sockaddr*)&server_addr,
sizeof(server_addr)) == -1) {
perror("Connection failed");
exit(EXIT_FAILURE);
}

printf("Connected to the server\n");

// Do something with the server connection...

// Close the socket


close(client_socket);

return 0;
}
Terminal Commands:

You might also like