Coffe
Coffe
Coffe
USN:4VM20IS024
Instructor :Dr Reshma Bhanu
Course/sem : BE/ 5th sem
Date:05/12/2022
Target:
Sockets are commonly used for client and server interaction. Typical
system configuration places the server on one machine, with the clients
on other machines. The clients connect to the server, exchange
information, and then disconnect.
• The CRC is used to detect errors in digital data and is used primarily in
data transmission system.
• Major internet Applications such as world wide web, remote
administration and file transfer rely on TCP, which is the part of
Transport Layer of the TCP/IP suite.
• The UDP connection is majorly used in the Voice over IP (VoIP),
online multiplayer games, streaming media applications such as
movies and DNS.
Outcome :
We get the knowledge of the working of the Socket
programming and understand the following concepts,
1. Client-server using TCP/IP sockets
2. Client-server using TCP/IP sockets
3. Client-Server Communication using UDP
1.Client-server using TCP/IP sockets
Aim:
Using TCP/IP Sockets, write a client-server program to make client sending the file
name and the server to send back the contents of the requested file if present.
Implement the above program using as message queues or FIFOs as IPC channels.
Source Code:
TCP Server
The server program has three responsibilities which must be fulfilled in the
code. First job is to read the file name coming from client. For this, it uses input
stream. Second one is to open the file, using some input stream, and read the
contents. Third one is, as the reading is going on, to send the contents each
line separately.
import java.net.*;
import java.io.*;
public class ContentsServer
{ public static void main(String args[]) throws Exception
{ // establishing the connection with the server
ServerSocket sersock = new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket sock = sersock.accept(); // binding with port: 4000
System.out.println("Connection is successful and waiting for chatting");
// reading the file name from client
InputStream istream = sock.getInputStream( );
BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));
String fname = fileRead.readLine( );
// reading file contents
BufferedReader contentRead = new BufferedReader(new
FileReader(fname) ); // keeping output stream ready to
send the contents
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
String str;
// reading line-by-line from file while((str
= contentRead.readLine()) != null)
{
pwrite.println(str); // sending each line to client
}
sock.close();
TCP Client:
To read filename as input from keyboard, remember, this file should exist on
server. For this, it uses input stream. The file read from keyboard should be
sent to the server. For this client uses output stream. The file contents sent by
the server, the client should receive and print on the console.
BufferedReader:
import java.net.*;
import java.io.*; public
class ContentsClient
{ public static void main( String args[ ] ) throws Exception
{
Socket sock = new Socket( "127.0.0.1", 4000);
// reading the file name from keyboard. Uses input stream
System.out.print("Enter the file name");
BufferedReader keyRead = new BufferedReader(new
InputStreamReader(System.in));
String fname = keyRead.readLine();
// sending the file name to server. Uses PrintWriter
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(fname);
// receiving the contents from server. Uses input stream
InputStream istream = sock.getInputStream();
BufferedReader socketRead = new BufferedReader(new
InputStreamReader(istream));
String str; while((str = socketRead.readLine()) != null) //
reading line-by-line
{ System.out.println(str);
} pwrite.close();
socketRead.close();
keyRead.close();
}
}
Output:
At server side:
[root@localhost]# javac ContentsServer.java
[root@localhost]# Java ContentsServer
Server ready for connection
Connection is successful and waiting for chatting
At Client Side:
[root@localhost]# javac ContentsClient.java
[root@localhost]# java ContentsClient
Enter the file name
aa.txt
Welcome to Network Lab
Whenever digital data is stored or interfaced, data corruption might occur. Since the
beginning of computer science, developers have been thinking of ways to deal with
this type of problem. For serial data they came up with the solution to attach a parity
bit to each sent byte. This simple detection mechanism works if an odd number of
bits in a byte changes, but an even number of false bits in one byte will not be
detected by the parity check. To overcome this problem developers have searched
for mathematical sound mechanisms to detect multiple false bits. The CRC
calculation or cyclic redundancy check was the result of this. Nowadays CRC
calculations are used in all types of communications. All packets sent over a network
connection are checked with a CRC. Also each data block on your hard disk has a
CRC value attached to it. Modern computer world cannot do without these CRC
calculations. So let's see why they are so widely used. The answer is simple; they
are powerful, detect many types of errors and are extremely fast to calculate
especially when dedicated hardware chips are used.
Implementation Algorithm:
send my routing table to all my neighbors whenever my link The idea behind CRC
calculation is to look at the data as one large binary number. This number is divided by a
certain value and the remainder of the calculation is called the CRC. Dividing in the CRC
calculation at first looks to cost a lot of computing power, but it can be performed very
quickly if we use a method similar to the one learned at school. We will as an example
calculate the remainder for the character 'm'—which is 1101101 in binary notation—by
dividing it by 19 or 10011. Please note that 19 is an odd number. This is necessary as we
will see further on. Please refer to your schoolbooks as the binary calculation method here is
not very different from the decimal method you learned when you were young. It might only
look a little bit strange. Also notations differ between countries, but the method is similar.
With decimal calculations you can quickly check that 109 divided by 19 gives a
quotient of 5 with 14 as the remainder. But what we also see in the scheme is that
every bit extra to check only costs one binary comparison and in 50% of the cases
one binary subtraction. You can easily increase the number of bits of the test data
string—for example to 56 bits if we use our example value "Lammert"—and the
result can be calculated with 56 binary comparisons and an average of 28 binary
subtractions. This can be implemented in hardware directly with only very few
transistors involved. Also software algorithms can be very efficient.
All of the CRC formulas you will encounter are simply checksum algorithms based
on modulo-2 binary division where we ignore carry bits and in effect the subtraction
will be equal to an exclusive or operation. Though some differences exist in the
specifics across different CRC formulas, the basic mathematical process is always
the same:
• The message bits are appended with c zero bits; this augmented message is
the dividend
• A predetermined c+1-bit binary sequence, called the generator polynomial, is
the divisor
• The checksum is the c-bit remainder that results from the division operation
Table 1 lists some of the most commonly used generator polynomials for 16-
and 32-bit
CRCs. Remember that the width of the divisor is always one bit wider than the
remainder.
So, for example, you’d use a 17-bit generator polynomial whenever a 16-bit
checksum is required.
Source Code:
import java.util.*;
class crc
{ void div(int a[],int k)
{ int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int count=0;
for(int i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(int j=i;j<17+i;j++)
{
a[j]=a[j]^gp[count++];
}
}
}
count =0 ;
}
public static void main(String args[])
{
int a[]=new int[100]; int b[]=new int[100]; int len,k;
crc ob=new crc();
System.out.println("Enter the length of Data Frame:");
Scanner sc=new Scanner(System.in); len=sc.nextInt();
int flag=0;
System.out.println("Enter the Message:"); for(int
i=0;i<len;i++)
{ a[i]=sc.nextInt();
}
for(int i=0;i<16;i++)
{ a[len++]=0;
}
k=len-16;
for(int i=0;i<len;i++)
{ b[i]=a[i];
}
ob.div(a,k);
for(int i=0;i<len;i++)
a[i]=a[i]^b[i];
System.out.println("Data to be transmitted: "); for(int
i=0;i<len;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Enter the Reveived Data: ");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
ob.div(a, k);
for(int i=0;i<len;i++)
{
if(a[i]!=0)
{
System.out.println("ERROR in Received data");
return;
}
System.out.println("no error");
}
}
}
Output:
Aim: Write a program on datagram socket for client/server to display the messages
on client side, typed at the server side.
A datagram socket is the one for sending or receiving point for a packet
delivery service. Each packet sent or received on a datagram socket is individually
addressed and routed. Multiple packets sent from one machine to another may be
routed differently, and may arrive in any order.
Here, the connectionServer.java program creates a ServerSocket object
bound to port 3456 and waits for an incoming client connection request. When a
client contacts the server program, the accept( ) method is unblocked and returns a
Socket object for the server to communicate with the particular client that contacted.
The server program then creates a PrintStream object through the output stream
extracted from this socket and uses it to send a welcome message to the contacting
client. The client program runs as follows: The client creates a Socket object to
connect to the server running at the specified IP address or hostname and at the
port number 3456. The client creates a BufferedReader object through the input
stream extracted from this socket and waits for an incoming line of message from
the other end. The readLine( ) method of the BufferedReader object blocks the
client from proceeding further unless a line of message is received. The purpose of
the flush( ) method of the PrintStream class is to write any buffered output bytes to
the underlying output stream and then flush that stream to send out the bytes. Note
that the server program in our example sends a welcome message to an incoming
client request and then stops.
Source Code:
UDP Client
import java.net.*;
import java.io.*;
class
connectionClient
{ public static void main(String[ ] args)
{
try {
InetAddres
s
acceptorH
ost =
InetAddres
s.getByNa
me(args[0]
);
int
serverPort
Num =
Integer.par
seInt(args[
1]);
Socket clientSocket = new Socket(acceptorHost, serverPortNum);
BufferedReader br = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream( )));
System.out.println(br.readLine( )); clientSocket.close();
}
catch(Exception e)
{ e.printStackTrace( );
}
}
}
UDP Server
import java.net.*;
import java.io.*;
class
connectionServer
{ public static void main(String[ ] args)
{ try {
Server Side
[root@localhost]# Javac ConnectionServer.java
[root@localhost]# Java ConnectionServer “Welcome to Computer Network Lab”
3956
Client Side: