0% found this document useful (0 votes)
11 views

CN PROGRAMS PART-B

The document contains four Java and C programs demonstrating different networking and data handling concepts. The first program establishes a TCP client-server connection, the second implements bit stuffing for data transmission, the third checks the parity of a number, and the fourth simulates a sliding window protocol for frame transmission. Each program includes code snippets and basic explanations of their functionalities.

Uploaded by

Shreya Kamal
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)
11 views

CN PROGRAMS PART-B

The document contains four Java and C programs demonstrating different networking and data handling concepts. The first program establishes a TCP client-server connection, the second implements bit stuffing for data transmission, the third checks the parity of a number, and the fourth simulates a sliding window protocol for frame transmission. Each program includes code snippets and basic explanations of their functionalities.

Uploaded by

Shreya Kamal
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/ 3

1.

Write a program to create TCP client server connection in java


Client.java
import java.io.*;
import java.net.*;

public class Client


{
public static void main(String args[ ]) throws IOException
{
// create a socket to connect to the server running on localhost at port number 9090
Socket socket = new Socket("localhost", 9090);

// Setup output stream to send data to the server


PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// Setup input stream to receive data from the server


BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

// Send message to the server


out.println("Hello from client!");

// Receive response from the server


String response = in.readLine();
System.out.println("Server says: " + response);

// Close the socket


socket.close();
}
}
Server.java
import java.io.*;
import java.net.*;

public class Server {


public static void main(String args[]) throws IOException
{
// create a server socket on port number 9090
ServerSocket serverSocket = new ServerSocket(9090);
System.out.println("Server is running and waiting for client connection...");

// Accept incoming client connection


Socket clientSocket = serverSocket.accept();
System.out.println("Client connected!");

// Setup input and output streams for communication with the client
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

// Read message from client


String message = in.readLine();
System.out.println("Client says: " + message);

// Send response to the client


out.println("Message received by the server.");

// Close the client socket


clientSocket.close();
// Close the server socket
serverSocket.close();
}
}
2. Write a program to create Bitstuffing in java
BitStuffing.java
import java.io.*;
public class BitStuffing
{
public static void main(String[] args)
{
String input = "011111110111111";
String stuffed = bitStuffing(input);
System.out.println("Original: " + input);
System.out.println("Stuffed: " + stuffed);
}
public static String bitStuffing(String input)
{
StringBuilder result = new StringBuilder();
int counter = 0;
for (int i = 0; i < input.length(); i++)
{
char bit = input.charAt(i);
result.append(bit);
if (bit == '1')
{
counter++;
if (counter == 5)
{
result.append('0');
counter = 0;
}
} else
{
counter = 0;
}
}
return result.toString();
}
}
3. Write a program to create parity check in java
parity.java
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;

class parity
{
/* Function to get parity of number n.
It returns 1 if n has odd parity, and
returns 0 if n has even parity */
static boolean getParity(int n)
{
boolean parity = false;
while(n != 0)
{
parity = !parity;
n = n & (n-1);
}
return parity;

/* Driver program to test getParity() */


public static void main (String[] args)
{
int n = 6;
System.out.println("Parity of no " + n + " = " +
(getParity(n)? "odd": "even"));
}
}
4. Write a program to create sliding window protocol using c
#include<stdio.h>
int main()
{
int w,i,f,frames[50];
printf("enter window size:");
scanf("%d",&w);
printf("\n Enter number of frames to transmmit:");
scanf("%d",&f);
printf("\n Enter %d frames:",f);
for(i=1;i<=f;i++)
scanf("%d",&frames[i]);
printf("\n With sliding window protocol the frames will be sent in the following manner(assuming no
corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);
for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else printf("%d",frames[i]);
}
if(f%w!=0)
printf("\n Acknowledgement of above frames sent is received by sender\n");
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy