0% found this document useful (0 votes)
8 views22 pages

CN Practical

The document contains multiple C programs implementing various socket communication techniques, including TCP and UDP. It features a TCP client-server model for bit-stream transmission with bit stuffing, a simple chat server using UDP, a Stop & Wait ARQ mechanism, Hamming code for error correction, and a multi-client TCP server for validating IPv4 addresses. Each program is structured to handle specific tasks related to networking and data transmission.
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)
8 views22 pages

CN Practical

The document contains multiple C programs implementing various socket communication techniques, including TCP and UDP. It features a TCP client-server model for bit-stream transmission with bit stuffing, a simple chat server using UDP, a Stop & Wait ARQ mechanism, Hamming code for error correction, and a multi-client TCP server for validating IPv4 addresses. Each program is structured to handle specific tasks related to networking and data transmission.
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/ 22

Write C programs to implement TCP Socket.

The client will take a bit-


stream from the user and send it to the server. The server will implement
bit stuffing and send the stream back to the client. The client will print it.
SERVER.C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>

int main()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);

char data[200], stuffed[200];

struct sockaddr_in serv, cli;

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = INADDR_ANY;

bind(sockfd,(struct sockaddr*)&serv, sizeof(serv));


listen(sockfd, 1);
printf("Server is waiting for clinet.................\n");

socklen_t len = sizeof(cli);

int newfd = accept(sockfd,(struct sockaddr*)&cli, &len);

printf("Client is connected......\n");

while(1)
{
int r = recv(newfd, data, sizeof(data), 0);
if(r<=0) break;

printf("Input data: %s\n", data);

data[r]= '\0';

int i=0, j=0, count =0;

while(data[i] != '\0')
{
stuffed[j] = data[i];
if(data[i] == '1') count++; else count = 0;
if(count == 5)
{
j++;
stuffed[j] = '0';
count = 0;
}

i++;j++;

}
stuffed[j] = '\0';

send(newfd,stuffed, strlen(stuffed), 0);


}
close(sockfd);
close(newfd);
return 0;
}

CLIENT. C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<unistd.h>
int main()
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);

char data[200];

struct sockaddr_in serv;

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &serv.sin_addr);

connect(sockfd,(struct sockaddr*)&serv, sizeof(serv));

printf("Server is connected......\n");

while(1)
{
printf("Enter the bit: ");
scanf("%s", data);

send(sockfd, data, strlen(data), 0);


int len = recv(sockfd, data, sizeof(data), 0);
if(len < 0) break;

data[len] = '\0';
printf("\nStufffed bit : %s\n", data);
}
close(sockfd);

return 0;
}

Write C programs to implement a simple chat server (single client, single


server) by using UDP Socket.
SERVER.C
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main() {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
char msg[500];

struct sockaddr_in serv, cli;

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = INADDR_ANY;

socklen_t len = sizeof(cli);

bind(sockfd, (struct sockaddr*)&serv, sizeof(serv));

printf("\nServer is waiting -------------\n");

while(1)
{
int n = recvfrom(sockfd, msg, sizeof(msg), 0, (struct sockaddr*)&cli,
&len);

msg[n] = '\0';
int i = 0;

printf("Client: ");
while(msg[i] != '\0')
{
printf("%c",msg[i]);
i++;
}
printf("\n");

printf("Server: ");

i=0;
char ch;
while(1)
{
scanf("%c",&ch);

if(ch == '\n')
{
msg[i] = '\0';
break;
}
msg[i] = ch;
i++;
}

sendto(sockfd, msg,i, 0, (struct sockaddr*)&cli, len);


}
close(sockfd);
return 0;
}
CLIENT.C
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

int main() {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
char msg[500];
struct sockaddr_in serv;

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = inet_addr("127.0.0.1");

socklen_t len = sizeof(serv);

while(1)
{
int i = 0;
printf("Client: ");

char ch;
while(1)
{
scanf("%c",&ch);
if(ch == '\n')
{
msg[i] = '\0';
break;
}
msg[i] = ch;
i++;
}
sendto(sockfd, msg, i, 0, (struct sockaddr*)&serv, len);
int n = recvfrom(sockfd, msg, sizeof(msg), 0, (struct sockaddr*)&serv,
&len);

msg[n] = '\0';

printf("Server: ");
i = 0;
while(msg[i] != '\0')
{
printf("%c", msg[i]);
i++;
}
printf("\n");

}
close(sockfd);

return 0;
}

Simulate Data Link Layer flow control mechanism (Stop & Wait ARQ)
using TCP Socket.
RECIEVER.C
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include<arpa/inet.h>
int main() {

int sockfd = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in serv, cli;

socklen_t len = sizeof(cli);

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = INADDR_ANY;

bind(sockfd, (struct sockaddr*)&serv, sizeof(serv));


listen(sockfd, 1);

printf("Receiver is ready.............\n");

char data;
char ack = '1';

int newfd = accept(sockfd, (struct sockaddr*)&cli, &len);

while(1)
{
recv(newfd, &data, sizeof(data), 0);

if(data == '0')
break;

printf("Recieve frame: %c\n", data);

send(newfd, &ack, sizeof(ack), 0);

close(sockfd);
close(newfd);

return 0;
}
SENDER.C
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include<arpa/inet.h>
int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);

struct sockaddr_in serv;


socklen_t len = sizeof(serv);
serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = inet_addr("127.0.0.1");
char data;
char ack;

connect(sockfd, (struct sockaddr*)&serv, len);

while(1)
{
printf("Enter frame to send: ");
scanf(" %c",&data);

if(data == '0')
break;

send(sockfd, &data, sizeof(data), 0);


recv(sockfd, &ack, sizeof(ack), 0);

if(ack == '1')
printf("Ack recieved\n");
}
close(sockfd);
return 0;
}

Write client-server programs using UDP socket. The client will


take a data word from the user and send it to the server. The server
will find the codeword (use Hamming code error correction
technique) and send it back to the client.
SERVER.C
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<unistd.h>

void hamming_code(char* data, char* codeword)


{
int d[4], p[3], c[7];

for(int i = 0; i<4; i++)


{
d[i] = data[i] - '0';
}

p[0] = d[0] ^ d[1] ^ d[3];


p[1] = d[0] ^ d[2] ^ d[3];
p[2] = d[1] ^ d[2] ^ d[3];

c[0] = p[0];
c[1] = p[1];
c[2] = d[0];
c[3] = p[2];
c[4] = d[1];
c[5] = d[2];
c[6] = d[3];

for(int i = 0; i<7; i++)


{
codeword[i] = c[i] + '0';
}
}

int main() {

int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

struct sockaddr_in serv, cli;

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = INADDR_ANY;

socklen_t len = sizeof(cli);

char data[200], codeword[10];

bind(sockfd, (struct sockaddr*)&serv, sizeof(serv));

printf("Serevr is waiting.................\n");

while(1)
{
int n = recvfrom(sockfd, data, sizeof(data), 0, (struct sockaddr*)&cli,
&len);

printf("Recived dataword: %s\n", data);


hamming_code(data, codeword);
printf("Sending codeword: %s\n", codeword);

sendto(sockfd, codeword, sizeof(codeword), 0, (struct sockaddr*)&cli,


len);
}

close(sockfd);
return 0;
}
CLIENT.C
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<unistd.h>

void hamming_code(char* data, char* codeword)


{
int d[4], p[3], c[7];

for(int i = 0; i<4; i++)


{
d[i] = data[i] - '0';
}

p[0] = d[0] ^ d[1] ^ d[3];


p[1] = d[0] ^ d[2] ^ d[3];
p[2] = d[1] ^ d[2] ^ d[3];

c[0] = p[0];
c[1] = p[1];
c[2] = d[0];
c[3] = p[2];
c[4] = d[1];
c[5] = d[2];
c[6] = d[3];

for(int i = 0; i<7; i++)


{
codeword[i] = c[i] + '0';
}
}

int main() {

int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

struct sockaddr_in serv, cli;

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = INADDR_ANY;

socklen_t len = sizeof(cli);


char data[200], codeword[10];

bind(sockfd, (struct sockaddr*)&serv, sizeof(serv));

printf("Serevr is waiting.................\n");

while(1)
{
int n = recvfrom(sockfd, data, sizeof(data), 0, (struct sockaddr*)&cli,
&len);

printf("Recived dataword: %s\n", data);

hamming_code(data, codeword);
printf("Sending codeword: %s\n", codeword);

sendto(sockfd, codeword, sizeof(codeword), 0, (struct sockaddr*)&cli,


len);
}

close(sockfd);#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<unistd.h>

int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
char data[200];

struct sockaddr_in serv;

socklen_t len = sizeof(serv);

serv.sin_family = AF_INET;
serv.sin_port = htons(8080);
serv.sin_addr.s_addr = inet_addr("127.0.0.1");

while(1)
{
printf("Enter the dataword: ");
scanf("%s", data);

sendto(sockfd, data, sizeof(data), 0, (struct sockaddr*)&serv, len);

int n = recvfrom(sockfd, data, sizeof(data), 0, NULL, NULL);

data[n] = '\0';

printf("Received codeword: %s",data);


}
close(sockfd);
return 0;
}
return 0;
}

Create a multi-client TCP server. A client will send an IPv4


address (a.b.c.d) to the server. The server will verify whether
the address is valid or not and send back 'YES' or 'NO' as a
result to the client.
SERVER.C
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>

int isValidIP(char ip[]) {


int a, b, c, d;
if (sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
return 0;
if (a < 0 || a > 255) return 0;
if (b < 0 || b > 255) return 0;
if (c < 0 || c > 255) return 0;
if (d < 0 || d > 255) return 0;
return 1;
}
int main() {
int sockfd, newsock;
struct sockaddr_in serv, cli;
socklen_t len;
char ip[50], reply[10];

sockfd = socket(AF_INET, SOCK_STREAM, 0);


serv.sin_family = AF_INET;
serv.sin_port = htons(12345);
serv.sin_addr.s_addr = INADDR_ANY;

bind(sockfd, (struct sockaddr*)&serv, sizeof(serv));


listen(sockfd, 5);

printf("Server ready. Waiting for clients...\n");

while (1) {
len = sizeof(cli);
newsock = accept(sockfd, (struct sockaddr*)&cli, &len);

if (fork() == 0) { // child process


recv(newsock, ip, sizeof(ip), 0);

if (isValidIP(ip))
strcpy(reply, "YES");
else
strcpy(reply, "NO");
send(newsock, reply, sizeof(reply), 0);
close(newsock);
exit(0);
}
}

return 0;
}

CLIENT.C
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>

int main() {
int sock;
struct sockaddr_in server;
char ip[50], result[10];

sock = socket(AF_INET, SOCK_STREAM, 0);


server.sin_family = AF_INET;
server.sin_port = htons(12345);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(sock, (struct sockaddr*)&server, sizeof(server));

while(1)
{
printf("Enter IPv4 address (e.g., 192.168.1.1): ");
scanf("%s", ip);

send(sock, ip, sizeof(ip), 0);

recv(sock, result, sizeof(result), 0);


printf("Result: %s\n", result);

close(sock);
return 0;
}

You might also like