0% found this document useful (0 votes)
20 views6 pages

Lab 4 CN

Uploaded by

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

Lab 4 CN

Uploaded by

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

Lab 4: Week-4: Connection Oriented Socket Programming

Details of Connection Oriented Socket programming APIs for TCP/IP stack

Connection-Oriented Socket Programming (TCP/IP)


Connection-oriented socket programming uses TCP (Transmission Control Protocol),
which ensures:
 Reliable data transfer
 Data arrives in order
 Error-free communication
 Connection is established before data is sent

TCP Server Steps


1. socket() → Create socket
2. bind() → Bind IP & port
3. listen() → Listen for clients
4. accept() → Accept connection
5. send()/recv() → Communicate
6. close() → Close socket

TCP Client Steps


1. socket() → Create socket
2. connect() → Connect to server
3. send()/recv() → Communicate
4. close() → Close socket

TCP Socket APIs


1. socket()
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
2. bind() (Server only)
bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
3. listen() (Server only)
listen(sockfd, backlog);
4. accept() (Server only)
int clientfd = accept(sockfd, (struct sockaddr*)&clientAddr, &addr_size);
5. connect() (Client only)
connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
6. send()
send(sockfd, buffer, strlen(buffer), 0);
7. recv()
int n = recv(sockfd, buffer, BUFFER_SIZE, 0);
8. close()
close(sockfd);
Differences of UDP and TCP
Feature TCP (SOCK_STREAM) UDP (SOCK_DGRAM)
Connection Required? Yes (connect, accept) No
Reliability Reliable Not reliable
Data Flow Stream Datagram (message)
Functions listen(), accept() sendto(), recvfrom()
Use Case Web servers, FTP Streaming, VoIP, games

Assignments
Write a connection-oriented client and server program in C using command
line
arguments. At the server side, pass the port number (to whom the server will
bind to) in the command line. At the client side, pass the IP address and the
port number of the server (to whom the client will connect to) as command
line
argument and carry out the following tasks.
◦ After establishment of connection print the IP Address and port number of
the
client to whom the server is connected now.
◦ Then exchange messages.
◦ After message exchange is over then the client sends a “close” message to
the
server to tear down the connection.

Server Program (server.c)

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

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {


if (argc != 2) {
printf("Usage: %s <Port>\n", argv[0]);
return 1;
}

int port = atoi(argv[1]);


int server_fd, client_fd;
char buffer[BUFFER_SIZE];
struct sockaddr_in serverAddr, clientAddr;
socklen_t addr_size = sizeof(clientAddr);

// 1. Create TCP Socket


server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("Socket creation failed");
exit(1);
}

// 2. Configure server address


serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(port);

// 3. Bind socket to port


if (bind(server_fd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
perror("Bind failed");
exit(1);
}

// 4. Listen for incoming connections


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

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

// 5. Accept a client connection


client_fd = accept(server_fd, (struct sockaddr*)&clientAddr, &addr_size);
if (client_fd < 0) {
perror("Accept failed");
exit(1);
}

// Print client IP and Port


printf("Connected to Client: %s:%d\n",
inet_ntoa(clientAddr.sin_addr),
ntohs(clientAddr.sin_port));

// 6. Message exchange loop


while (1) {
memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0);
buffer[bytes_received] = '\0';
printf("Client: %s\n", buffer);

// Check for close message


if (strcmp(buffer, "close") == 0) {
printf("Client requested to close the connection.\n");
break;
}

printf("You: ");
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "\n")] = '\0';
send(client_fd, buffer, strlen(buffer), 0);
}

// 7. Close sockets
close(client_fd);
close(server_fd);
return 0;
}

Client Program (client.c)

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

#define BUFFER_SIZE 1024

int main(int argc, char *argv[]) {


if (argc != 3) {
printf("Usage: %s <Server_IP> <Port>\n", argv[0]);
return 1;
}

char *server_ip = argv[1];


int port = atoi(argv[2]);
int sockfd;
char buffer[BUFFER_SIZE];
struct sockaddr_in serverAddr;

// 1. Create TCP Socket


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(1);
}

// 2. Configure server address


serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = inet_addr(server_ip);

// 3. Connect to server
if (connect(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
perror("Connection failed");
exit(1);
}

printf("Connected to Server: %s:%d\n", server_ip, port);

// 4. Message exchange loop


while (1) {
printf("You: ");
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "\n")] = '\0';
send(sockfd, buffer, strlen(buffer), 0);

if (strcmp(buffer, "close") == 0) {
printf("Closing connection...\n");
break;
}

memset(buffer, 0, BUFFER_SIZE);
int bytes_received = recv(sockfd, buffer, BUFFER_SIZE, 0);
buffer[bytes_received] = '\0';
printf("Server: %s\n", buffer);
}

// 5. Close socket
close(sockfd);
return 0;
}

Steps to Execute
1. Compile Programs
gcc server.c -o server
gcc client.c -o client
2. Run Server First
./server 8080
3. Run Client
./client 127.0.0.1 8080
4. Message Exchange
 Type messages in the client and server terminal to chat.
 Client sends close to end the connection.

You might also like