0% found this document useful (0 votes)
30 views11 pages

Name: D.Somasai REG:19BBS0009 Subject:Computer Networks Exam:Lab Fat DATE:07-06-2021

The document contains a lab exam for the subject "Computer Networks" with the following details: 1. It lists the name, registration number, subject, and date of the exam for a student named D. Somasai. 2. It provides examples of Linux and networking commands to change directories, print the computer name, and calculate checksum for a byte of data. 3. It outlines a program that develops both client and server code to demonstrate a datagram-based program where the server finds the smallest of 10 integers sent by the client and returns it.

Uploaded by

how is it is
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)
30 views11 pages

Name: D.Somasai REG:19BBS0009 Subject:Computer Networks Exam:Lab Fat DATE:07-06-2021

The document contains a lab exam for the subject "Computer Networks" with the following details: 1. It lists the name, registration number, subject, and date of the exam for a student named D. Somasai. 2. It provides examples of Linux and networking commands to change directories, print the computer name, and calculate checksum for a byte of data. 3. It outlines a program that develops both client and server code to demonstrate a datagram-based program where the server finds the smallest of 10 integers sent by the client and returns it.

Uploaded by

how is it is
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
You are on page 1/ 11

NAME: D.

SOMASAI
REG :19BBS0009
SUBJECT:COMPUTER NETWORKS
EXAM:LAB FAT
DATE:07-06-2021

a. Write a Linux command to change to new directory.


cd <file path>

b. Write a Network command to print your computer name.


Hostname
c. Write a program to show the implementation of Check sum for 1 byte of data.
Input format: data
Output format: sum, check sum
#include<stdio.h>
#include<string.h>
int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i,length;
printf("Enter first binary string\n");
scanf("%s",&a);
printf("Enter second binary string\n");
scanf("%s",&b);
if(strlen(a)==strlen(b)){
length = strlen(a);
char carry='0';
for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';
}
else
break;
}
printf("\nSum=%c%s",carry,sum);

for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}
if(carry=='1')
carry='0';
else
carry='1';
printf("\nChecksum=%c%s",carry,complement);
}
else {
printf("\nWrong input strings");
}
}
d. Write a datagram based program that responds to client messages as follows: When it
receives a message (ten integer’s values) from a client, find the smallest and sends back the same
to the client. Develop both client and server programs.

client.c
// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{ int num1;
int sock = 0, valread;
struct sockaddr_in serv_addr;

char *hello = "1, 2, 3, 4, 5, 6, 7, 8";


char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{

printf("\nInvalid address/ Address not supported \n");


return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;}
int num;
printf("Enter the number of digits you want to send to the sever for sorting\n");
scanf("%d",&num);
printf("%s",hello);
for(int i=0;i<num;i++){
int num2;
printf("enter the number from %d",i);
scanf("%d",&num2);
}
send(sock , hello , strlen(hello) , 0 );
printf("the numbers are sent into tho server as a series of string \n");
valread = read( sock , buffer, 1024);
printf("smallest number recived by the server: %s\n",buffer );
return 0;
}

SERVER.C
// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}

int partition(int array[], int low, int high) {


int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {

i++;
swap(&array[i], &array[j]);
}
}

swap(&array[i + 1], &array[high]);


return (i + 1);
}

void quickSort(int array[], int low, int high) {


if (low < high) {

int pi = partition(array, low, high);


quickSort(array, low, pi - 1);

quickSort(array, pi + 1, high);
}
}
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "demmhA si eman YM";

// Creating socket file descriptor


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}

// Forcefully attaching socket to the port 8080


if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );

if (bind(server_fd, (struct sockaddr *)&address,


sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);

}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
int str_length = buffer.length();
int arr[str_length] = { 0 };
int j = 0, i, sum = 0;
for (i = 0; buffer[i] != '\0'; i++) {

if (str[i] == ',')
continue;
if (str[i] == ' '){
j++;

}
else {

arr[j] = arr[j] * 10 + (str[i] - 48);


}
}
char *hello=arr[0];
send(new_socket , hello , strlen(hello) , 0 );
return 0;
}

OUTPUTS:
CLIENT:

SERVER:

You might also like