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

Net Lab Original

This document describes algorithms for TCP and UDP communication between a client and server. For TCP communication, the client connects to the server, sends a request, receives data from the server, and closes the connection. The server accepts the client connection, reads data from the client, sends data to the client, and closes the connection. For TCP echo, the client sends a message to the server which echoes it back in uppercase. The server reads the message, converts it to uppercase, and sends it back. For UDP, the client converts data to packets and sends them to the server listening on a port. The server receives packets from the client and sends packets back to the client.

Uploaded by

anu_anumohan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Net Lab Original

This document describes algorithms for TCP and UDP communication between a client and server. For TCP communication, the client connects to the server, sends a request, receives data from the server, and closes the connection. The server accepts the client connection, reads data from the client, sends data to the client, and closes the connection. For TCP echo, the client sends a message to the server which echoes it back in uppercase. The server reads the message, converts it to uppercase, and sends it back. For UDP, the client converts data to packets and sends them to the server listening on a port. The server receives packets from the client and sends packets back to the client.

Uploaded by

anu_anumohan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 36

Computer Networks Lab

1. TCP COMMUNICATION
ALGORITHM

CLIENT
STEP 01 : START CLIENT
STEP 02 : SEND REQUEST FOR CONNECTION TO SERVER.
STEP 03 : CONNECT TO SERVER WHEN SERVER ACCEPT CONNECTION
STEP 04 : WRITE DATA TO BUFFER THAT RECEIVED FROM CONSOLE.
STEP 05 : CLOSE ALL CONNECTION
STEP 06 : STOP.
SERVER
STEP 01 : START SERVER
STEP 02 : CREATE A SERVER SOCKET.
STEP 03 : ESTABLISH CONNECTION WHEN CLIENT REQUEST FOR
CONNECTION
STEP 04 : IF SOCKET IS NOT CONNECTED DISPLAY ERROR
STEP 05 : READ DATA FROM BUFFER AND DISPLAY ON CONSOLE.
STEP 06 : CLOSE ALL CONNECTION AFTER DISPLAYING
STEP 07 : STOP.

Department Of Computer Applications, T.K.M College of Engineering


1|Page
Computer Networks Lab

/* Program to implement TCP communication */

Client Program

package lab.tcp;
import java.io.*;
import java.net.*;
public class Tcpclient {
public static void main(String[] args) {
try
{
System.out.println("This is client and ready to
start communication");
InputStreamReader isr=new InputStreamReader (
System.in);
BufferedReader br=new BufferedReader(isr);
String x;
Socket s=new Socket("localhost",78);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
while(true)
{
System.out.println("Server:"+dis.readUTF());
System.out.println("Enter message to
server:");
x=br.readLine();
dos.writeUTF(x);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


2|Page
Computer Networks Lab

Server Program

package lab.tcp;
import java.io.*;
import java.net.*;
public class Tcpserver {
public static void main(String[] args) {
try
{
System.out.println("This is server and ready to
start communication");
InputStreamReader isr=new InputStreamReader
(System.in);
BufferedReader br=new BufferedReader(isr);
String x;
ServerSocket ss=new ServerSocket(78);
Socket s=ss.accept();
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
while(true)
{
System.out.println("Enter message to
client:");
x=br.readLine();
dos.writeUTF(x);
System.out.println("Client:
"+dis.readUTF());
System.out.println();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


3|Page
Computer Networks Lab

OUTPUT:

Server

This is server and ready to start communication


Enter message to client:
hai
Client:hello
Enter message to client:

Client

This is client and ready to start communication


Server:hai
Enter message to server:hello

Department Of Computer Applications, T.K.M College of Engineering


4|Page
Computer Networks Lab

2. TCP ECHO SERVER


ALGORITHM

CLIENT
STEP 01 : START CLIENT
STEP 02 : SEND REQUEST FOR CONNECTION TO SERVER.
STEP 03 : CONNECT TO SERVER WHEN SERVER ACCEPT CONNECTION
STEP 04 : WRITE DATA TO BUFFER THAT RECEIVED FROM CONSOLE.
STEP 05 : READ DATA FROM BUFFER.
STEP 06 : CLOSE ALL CONNECTION
STEP 07 : STOP.
SERVER
STEP 01 : START SERVER
STEP 02 : CREATE A SERVER SOCKET.
STEP 03 : ESTABLISH CONNECTION WHEN CLIENT REQUEST FOR
CONNECTION
STEP 04 : IF SOCKET IS NOT CONNECTED DISPLAY ERROR
STEP 05 : READ DATA FROM BUFFER AND DISPLAY ON CONSOLE.
STEP 06 : WRITE DATA TO BUFFER FOR SENDING TO THE CLIENT
STEP 07 : CLOSE ALL CONNECTION
STEP 08 : STOP.

Department Of Computer Applications, T.K.M College of Engineering


5|Page
Computer Networks Lab

/* Program to implement TCP echo server */

Client Program

package lab.echo;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try
{
System.out.println("This is client and ready to
start communication");
InputStreamReader isr=new InputStreamReader
(System.in);
BufferedReader br=new BufferedReader(isr);
String x;
Socket s=new Socket("localhost",78);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
while(true)
{
System.out.println("Enter a message to
server:");
x=br.readLine();
dos.writeUTF(x);
System.out.println("Server:"+dis.readUTF());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


6|Page
Computer Networks Lab

Server Program

package lab.echo;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try
{
System.out.println("This is server and ready to
start communication");
InputStreamReader isr=new InputStreamReader
(System.in);
BufferedReader br=new BufferedReader(isr);
String x,y;
ServerSocket ss=new ServerSocket(78);
Socket s=ss.accept();
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
while(true)
{
x=dis.readUTF();
y=x.toUpperCase();
dos.writeUTF(y);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


7|Page
Computer Networks Lab

OUTPUT:

Server

This is server and ready to start communication

Client

This is client and ready to start communication


Enter a message to server:
hai i am client
Server:HAI I AM CLIENT
Enter message to server:

Department Of Computer Applications, T.K.M College of Engineering


8|Page
Computer Networks Lab

3. UDP COMMUNICATION
ALGORITHM

CLIENT
STEP 01 : START CLIENT
STEP 02 : CONVERT DATA INTO UDP PACKETS FOR SENDING TO SERVER.
STEP 03 : SENDING PACKET TO SERVER WHICH LISTENING IN ANY FIXED
NON PORT
STEP 04 : RECEIVE INFORMATION SEND FROM SERVER.
STEP 05 : DISPLAY IT TO CONSOLE.
STEP 06 : STOP.
SERVER
STEP 01 : START SERVER
STEP 02 : LISTEN TO PERTICULAR PORT NUMBER
STEP 03 : RECEIVING PACKET FROM CLIENT AND DISPLAY IT ON CONSOLE
STEP 04 : CONVERT THE RECEIVED DATA INTO PACKET FOR SENDING.
STEP 05 : SEND PACKET TO CLIENT.
STEP 06 : STOP.

Department Of Computer Applications, T.K.M College of Engineering


9|Page
Computer Networks Lab

/* Program to implement communication using UDP */

Client Program

package lab.udp;
import java.io.*;
import java.net.*;
public class Datagramclient {
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
InetAddress local=InetAddress.getLocalHost();
InetAddress r=local;
DatagramSocket ds=new DatagramSocket(4000);
int bsize=2000;
byte[] obuffer=new byte[bsize];
DatagramPacket outgram;
System.out.println("This is client and ready to start
communication");
boolean more = true;
while(more)
{
byte[] ibuffer=new byte[bsize];
DatagramPacket dp=new DatagramPacket
(ibuffer,bsize);
String send=br.readLine();
obuffer=send.getBytes();
outgram=new DatagramPacket
(obuffer,obuffer.length,r,3456);
ds.send(outgram);
if((send.trim().equals("exit")))
break;
ds.receive(dp);
String data=new String(dp.getData());
data=data.trim();
System.out.println("From server : "+data);
if(data.equals("exit"))
break;
}
ds.close();
}
}

Department Of Computer Applications, T.K.M College of Engineering


10 | P a g e
Computer Networks Lab

Server Program

package lab.udp;
import java.io.*;
import java.net.*;
public class Datagramserver {
public static void main(String[] args) throws Exception
{
BufferedReader k=new BufferedReader (new
InputStreamReader(System.in));
InetAddress i=InetAddress.getLocalHost();
InetAddress r=null;
DatagramSocket ds=new DatagramSocket(3456);
int bsize=2000;
int rport;
DatagramPacket dp;
System.out.println("This is server and type replies
and type exit to quit");
boolean more=true;
while(more)
{
byte[]inbuffer=new byte[bsize];
DatagramPacket ingram=new
DatagramPacket(inbuffer,bsize);
byte[]outbuffer=new byte[bsize];
ds.receive(ingram);
r=ingram.getAddress();
rport=ingram.getPort();
String data=new String(ingram.getData());
data=data.trim();
if(data.equals("exit"))
break;
System.out.println("From client : "+data);
String rply=k.readLine();
outbuffer=rply.getBytes();
dp=new DatagramPacket
(outbuffer,outbuffer.length,r,rport);
ds.send(dp);
if(rply.equals("exit"))
break;
}
ds.close();
}
}

Department Of Computer Applications, T.K.M College of Engineering


11 | P a g e
Computer Networks Lab

OUTPUT:

Server

This is server and type replies and type exit to quit


From client : hai
hai
From client : h r u

Client

This client and ready to start communication


hai
From server : hai
h r u

Department Of Computer Applications, T.K.M College of Engineering


12 | P a g e
Computer Networks Lab

4. LARGEST NUMBER USING TCP


ALGORITHM

CLIENT
STEP 01 : START
STEP 02 : SEND REQUEST FOR CONNECTION TO SERVER
STEP 03 : CONNECT TO SERVER WHEN SERVER ACCEPT CONNECTION
STEP 04 : READ THREE NUMBERS.
STEP 05 : WRITE THE ABOVE NUMBERS TO BUFFER THAT ARE RECEIVED FROM
CONSOLE
STEP 06 : IF ERROR OCCURS DISPLAY THE ERROR DURING WRITE OPERATION
STEP 07 : READ DATA FROM BUFFER AND DISPLAY IT
STEP 08 : CLOSE THE CONNECTION AFTER DATA SEND
STEP 09 : STOP
SERVER
STEP 01 : START
STEP 02 : CREATE A SERVER SOCKET
STEP 03 : ESTABLISH CONNECTION WHEN CLIENT REQUEST FOR CONNECTION
STEP 04 : IF SOCKET IS NOT CONNECTED PROPERLY DISPLAY ERROR
STEP 05 : READ NUMBERS FROM BUFFER AND DISPLAY IT ON CONSOLE
STEP 06 : FIND THE LARGEST NUMBER.
STEP 07 : WRITE THE LARGEST NUMBER TO BUFFER FOR SENDING TO CLIENT
STEP 08 : CLOSE CONNECTION AFTER SENDING DATA
STEP 09 : STOP

Department Of Computer Applications, T.K.M College of Engineering


13 | P a g e
Computer Networks Lab

/* Program to implement TCP where client will send three


numbers to the server and the server will return the
largest number */

Client Program

package lab.big;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try
{
System.out.println("This is client and ready to
start two way communication");
InputStreamReader isr=new InputStreamReader
(System.in);
BufferedReader br=new BufferedReader(isr);
String x,y,z;
Socket s=new Socket("localhost",2000);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
System.out.println("Enter 3 numbers");
x=br.readLine();
y=br.readLine();
z=br.readLine();
dos.writeUTF(x);
dos.writeUTF(y);
dos.writeUTF(z);
System.out.println("Largest number is"
+dis.readByte());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


14 | P a g e
Computer Networks Lab

Server Program

package lab.big;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) {
try
{
System.out.println("This is server and ready to
start communication");
InputStreamReader isr=new InputStreamReader
(System.in);
BufferedReader br=new BufferedReader(isr);
String x,y,z;
int a,b,c;
ServerSocket s=new ServerSocket(2000);
Socket ss=s.accept();
InputStream is=ss.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=ss.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
x=dis.readUTF();
y=dis.readUTF();
z=dis.readUTF();
a=Integer.parseInt(x);
b=Integer.parseInt(y);
c=Integer.parseInt(z);
if(a>b)
{
if(a>c)
{
dos.writeByte(a);
}
else
{
dos.writeByte(c);
}
}
else if(b>c)
{
dos.writeByte(b);
}
else
{
dos.writeByte(c);
}

Department Of Computer Applications, T.K.M College of Engineering


15 | P a g e
Computer Networks Lab

}
catch(IOException e)
{
e.printStackTrace();
}
}
}

OUTPUT:

Server

This is server and ready to start communication

Client

This is client and ready to start two way communications


Enter 3 numbers
87
65
90
Largest number is 90

Department Of Computer Applications, T.K.M College of Engineering


16 | P a g e
Computer Networks Lab

5. SORTING OF NUMBERS USING TCP


ALGORITHM

CLIENT
STEP 01 : START
STEP 02 : SEND REQUEST FOR CONNECTION TO SERVER
STEP 03 : CONNECT TO SERVER WHEN SERVER ACCEPT CONNECTION
STEP 04 : READ NUMBERS.
STEP 05 : WRITE THE ABOVE NUMBERS TO BUFFER THAT ARE RECEIVED FROM
CONSOLE
STEP 06 : IF ERROR OCCURS DISPLAY THE ERROR DURING WRITE OPERATION
STEP 07 : READ DATA FROM BUFFER AND DISPLAY IT
STEP 08 : CLOSE THE CONNECTION AFTER DATA SEND
STEP 09 : STOP
SERVER
STEP 01 : START
STEP 02 : CREATE A SERVER SOCKET
STEP 03 : ESTABLISH CONNECTION WHEN CLIENT REQUEST FOR CONNECTION
STEP 04 : IF SOCKET IS NOT CONNECTED PROPERLY DISPLAY ERROR
STEP 05 : READ NUMBERS FROM BUFFER AND DISPLAY IT ON CONSOLE
STEP 06 : SORT THE NUMBERS.
STEP 07 : WRITE THE SORTED NUMBERS TO BUFFER FOR SENDING TO CLIENT
STEP 08 : CLOSE CONNECTION AFTER SENDING DATA
STEP 09 : STOP

Department Of Computer Applications, T.K.M College of Engineering


17 | P a g e
Computer Networks Lab

/* Program to implement TCP where client will send numbers


to the server and the server will return them in sorted
order */

Client Program

package lab.sort;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try
{
System.out.println("This is client and ready to
start communication");
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String a[]=new String[10];
String l;
byte n;
Socket s=new Socket("localhost",81);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
System.out.println("enter the limit of
numbers");
l=br.readLine();
dos.writeUTF(l);
n=Byte.parseByte(l);
System.out.println("enter numbers");
for(int i=0;i<n;i++)
{
a[i]=br.readLine();
dos.writeUTF(a[i]);
}
System.out.println("the array to be sorted is");
for(int i=0;i<n;i++)
{
System.out.println(dis.readByte());
}

Department Of Computer Applications, T.K.M College of Engineering


18 | P a g e
Computer Networks Lab

System.out.println("sorted numbers are");


for(int i=0;i<n;i++)
{
System.out.println(dis.readByte());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


19 | P a g e
Computer Networks Lab

Server Program

package lab.sort;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try
{
System.out.println("This is server and ready to
start communication");
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String a[]=new String[10];
byte b[]=new byte[10];
String l;
byte temp,n;
ServerSocket ss=new ServerSocket(81);
Socket s=ss.accept();
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
l=dis.readUTF();
n=Byte.parseByte(l);
for(int i=0;i<n;i++)
{
a[i]=dis.readUTF();
}
for(int i=0;i<n;i++)
{
b[i]=Byte.parseByte(a[i]);
}
System.out.println("the array to be sorted is");
for(int i=0;i<n;i++)
{
System.out.println(b[i]);
dos.writeByte(b[i]);
}
for(int i=0;i<n;i++)

Department Of Computer Applications, T.K.M College of Engineering


20 | P a g e
Computer Networks Lab

{
for(int j=0;j<n;j++)
{
if(b[i]<b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
System.out.println("the sorted array is");
for(int i=0;i<n;i++)
{
System.out.println(b[i]);
dos.writeByte(b[i]);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


21 | P a g e
Computer Networks Lab

OUTPUT:

Client

This is client and ready to start communication


enter the limit of numbers
6
enter numbers
1
90
0
12
4
6
the array to be sorted is
1
90
0
12
4
6
sorted numbers are
0
1
4
6
12
90

Server

This is server and ready to start communication


the array to be sorted is
1
90
0
12
4
6
the sorted array is
0
1
4
6
12
90

Department Of Computer Applications, T.K.M College of Engineering


22 | P a g e
Computer Networks Lab

6. MATRIX OPERATIONS USING TCP


ALGORITHM

CLIENT
STEP 01 : START
STEP 02 : SEND REQUEST FOR CONNECTION TO SERVER
STEP 03 : CONNECT TO SERVER WHEN SERVER ACCEPT CONNECTION
STEP 04 : READ THE ROWS AND COLUMNS OF FIRST MATRIX
STEP 05 : READ THE ELEMENTS OF FIRST MATRIX.
STEP 06 : WRITE THE ABOVE NUMBERS TO BUFFER THAT ARE RECEIVED FROM
CONSOLE
STEP 07 : IF ERROR OCCURS DISPLAY THE ERROR DURING WRITE OPERATION
STEP 08: READ THE ROWS AND COLUMNS OF SECOND MATRIX
STEP 09 : READ THE ELEMENTS OF SECOND MATRIX.
STEP 10 : WRITE THE ABOVE NUMBERS TO BUFFER THAT ARE RECEIVED FROM
CONSOLE
STEP 11 : IF ERROR OCCURS DISPLAY THE ERROR DURING WRITEOPERATION
STEP 12 : READ ADDED MATRIX AND MULTIPLIED MATRIX FROM BUFFER AND
DISPLAY IT
STEP 08 : CLOSE THE CONNECTION AFTER DATA SEND
STEP 09 : STOP
SERVER
STEP 01 : START
STEP 02 : CREATE A SERVER SOCKET
STEP 03 : ESTABLISH CONNECTION WHEN CLIENT REQUEST FOR CONNECTION
STEP 04 : IF SOCKET IS NOT CONNECTED PROPERLY DISPLAY ERROR
STEP 05 : READ MATRICES FROM BUFFER AND DISPLAY IT ON CONSOLE
STEP 06 : ADD AND MULTIPLY THE MATRICES.
STEP 07 : WRITE THE ADDED AND MULTIPLIED MATRICES TO BUFFER FOR
SENDING TO CLIENT AND DISPLAY IT ON CONSOLE

Department Of Computer Applications, T.K.M College of Engineering


23 | P a g e
Computer Networks Lab

STEP 08 : CLOSE CONNECTION AFTER SENDING DATA


STEP 09 : STOP

Department Of Computer Applications, T.K.M College of Engineering


24 | P a g e
Computer Networks Lab

/* Program to implement TCP where client will send two


matrices to the server and the server will return the
added and multiplied matrix to the client */

Client Program

package lab.matrix;
import java.io.*;
import java.net.*;

public class Matrixclient {

public static void main(String[] args) {


try
{
System.out.println("this is client");
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String a[][]=new String[10][10];
String b[][]=new String[10][10];
String m,n,p,q;
int m1,n1,p1,q1;
Socket s=new Socket("localhost",80);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
System.out.println("enter the rows and columns of
first matrix");
m=br.readLine();
n=br.readLine();
m1=Integer.parseInt(m);
n1=Integer.parseInt(n);
dos.writeUTF(m);
dos.writeUTF(n);
System.out.println("enter the elements of first
matrix");
for(int i=0;i<m1;i++)
{
for(int j=0;j<n1;j++)
{
a[i][j]=br.readLine();
dos.writeUTF(a[i][j]);
}
}

Department Of Computer Applications, T.K.M College of Engineering


25 | P a g e
Computer Networks Lab

System.out.println("enter the rows and columns of


second matrix");
p=br.readLine();
q=br.readLine();
p1=Integer.parseInt(p);
q1=Integer.parseInt(q);
dos.writeUTF(p);
dos.writeUTF(q);
System.out.println("enter the elements of second
matrix");
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
b[i][j]=br.readLine();
dos.writeUTF(b[i][j]);
}
}
if(m1==p1 && n1==q1)
{
System.out.println("the matrices can be added
and the added matrix is");
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
System.out.print(dis.readByte());
System.out.print(" ");
}
System.out.println();
}
}
else
{
System.out.println("the matrices can not be
added ");
}
if(m1==p1 && n1==q1)
{
System.out.println("the matrices can be subtracted
and the subtracted matrix is");
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
System.out.print(dis.readByte());

Department Of Computer Applications, T.K.M College of Engineering


26 | P a g e
Computer Networks Lab

System.out.print(" ");
}
System.out.println();
}
}
else
{
System.out.println("the matrices can not
Be subtracted ");
}
if(n1==p1)
{
System.out.println("the matrices can be
multiplied and the multiplied matrix is");
for(int i=0;i<m1;i++)
{
for(int j=0;j<q1;j++)
{
System.out.print(dis.readByte());
System.out.print(" ");
}
System.out.println();
}
}
else
{
System.out.println("the matrices can not be
multiplied ");
}

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


27 | P a g e
Computer Networks Lab

Server Program

package lab.matrix;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Matrixserver {

public static void main(String[] args) {


try
{
System.out.println("This is server and ready to
communication");
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String a[][]=new String[10][10];
String b[][]=new String[10][10];
int c[][]=new int[10][10];
int d[][]=new int[10][10];
int e[][]=new int[10][10];
int f[][]=new int[10][10];
int x[][]=new int[10][10];
String m,n,p,q;
int m1,n1,p1,q1;
ServerSocket s=new ServerSocket(80);
Socket ss=s.accept();
InputStream is=ss.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=ss.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
m=dis.readUTF();
n=dis.readUTF();

m1=Integer.parseInt(m);
n1=Integer.parseInt(n);

for(int i=0;i<m1;i++)
{
for(int j=0;j<n1;j++)
{

Department Of Computer Applications, T.K.M College of Engineering


28 | P a g e
Computer Networks Lab

a[i][j]=dis.readUTF();
}
}
for(int i=0;i<m1;i++)
{
for(int j=0;j<n1;j++)
{
c[i][j]=Integer.parseInt(a[i][j]);

}
System.out.println();
}
p=dis.readUTF();
q=dis.readUTF();
p1=Integer.parseInt(p);
q1=Integer.parseInt(q);
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
b[i][j]=dis.readUTF();
}
}
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
d[i][j]=Integer.parseInt(b[i][j]);

}
System.out.println();
}
if(m1==p1 && n1==q1)
{
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
e[i][j]=c[i][j]+d[i][j];
dos.writeByte(e[i][j]);
}
}
System.out.println("the added matrix is");
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{

Department Of Computer Applications, T.K.M College of Engineering


29 | P a g e
Computer Networks Lab

System.out.print(e[i][j]);
System.out.print(" ");

}
System.out.println();
}

}
else
{
System.out.print(" matrices can not be added ");
}
if(m1==p1 && n1==q1)
{
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
f[i][j]=c[i][j]-d[i][j];
dos.writeByte(f[i][j]);
}
}
System.out.println("the subtracted matrix is");
for(int i=0;i<p1;i++)
{
for(int j=0;j<q1;j++)
{
System.out.print(f[i][j]);
System.out.print(" ");

}
System.out.println();
}

}
else
{
System.out.println(" matrices can not be
subtracted ");
}
if(n1==p1)
{
for(int i=0;i<m1;i++)
{
for(int j=0;j<q1;j++)
{
x[i][j]=0;

Department Of Computer Applications, T.K.M College of Engineering


30 | P a g e
Computer Networks Lab

for(int k=0;k<p1;k++)
{
x[i][j]=(x[i][j]+(c[i][k]*d[k][j]));
}
}
}
System.out.println("the multiplied matrix is");
for(int i=0;i<m1;i++)
{
for(int j=0;j<q1;j++)
{
dos.writeByte(x[i][j]);
System.out.print(x[i][j]);
System.out.print(" ");

}
System.out.println();
}

}
else
{
System.out.print(" matrices can not be
multiplied ");
}

catch(Exception e)
{
e.printStackTrace();
}
}
}

OUTPUT:

Client

this is client
enter the rows and columns of first matrix
2
2
enter the elements of first matrix
3
3
3

Department Of Computer Applications, T.K.M College of Engineering


31 | P a g e
Computer Networks Lab

3
enter the rows and columns of second matrix
2
2
enter the elements of second matrix
3
3
3
3
the matrices can be added and the added matrix is

6 6
6 6

the matrices can be subtracted and the subtracted matrix is

0 0
0 0

the matrices can be multiplied and the multiplied matrix is

18 18
18 18

Server

This is server and ready to communication

the added matrix is

6 6
6 6

the subtracted matrix is

0 0
0 0
1
the multiplied matrix is

18 18
18 18

Department Of Computer Applications, T.K.M College of Engineering


32 | P a g e
Computer Networks Lab

7. ACCESS A FILE FROM A SERVER AND DISPLAY THE CONTENTS INO


THE SERVER
ALGORITHM
CLIENT

STEP 01: START CLIENT


STEP 02: SEND REQUEST FOR CONNECTION TO SERVER.
STEP 03: CONNECT TO SERVER WHEN SERVER ACCEPT CONNECTION.
STEP 04 :READ THE DATA FROM BUFFER AND DISPLAY ON CONSOLE
STEP 05: READ THE DATA FROM THE CONSOLE.
STEP 06: WRITE THE DATA TO BUFFER FOR SENDING TO THE SERVER.
STEP 07: READ THE DATA FROM BUFFER AND DISPLAY ON CONSOLE.
STEP 08: CLOSE THE CONNECTION.
STEP 09: STOP CLIENT.

SERVER

STEP 01: START SERVER


STEP 02: CREATE A SERVER SOCKET.
STEP 03: ESTABLISH THE CONNECTION WHEN CLIENT IS REQUESTED
STEP 04: IF SOCKET IS NOT CONNECTED DISPLAY ERROR
STEP 05: READ THE MESSAGE FOR CLIENT FROM THE CONSOLE.
STEP 06: WRITE THE DATA TO BUFFER FOR SENDING TO THE CLIENT.
STEP 07: READ THE DATA FROM BUFFER AND DISPLAY ON CONSOLE.
STEP 08: SEARCH FOR THE FILE AND READ THE CONTENTS OF IT AND WRITE
IT TO THE BUFFER.
STEP 09: CLOSE THE CONNECTION.
STEP 10: STOP SERVER

Department Of Computer Applications, T.K.M College of Engineering


33 | P a g e
Computer Networks Lab

/* Program to access a file from a server and display the


contents into the client*/

Client Program

package lab.file;
import java.io.*;
import java.net.*;
public class Fileclient {
public static void main(String[] args) {
try
{
System.out.println("this is client and ready to
communication");
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String filename;
Socket s=new Socket("localhost",3000);
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
System.out.println("enter the file name");
filename=br.readLine();
dos.writeUTF(filename);
BufferedReader b=new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println(“contents of the file are”);
while(true)
{
System.out.println(dis.readUTF());
}

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


34 | P a g e
Computer Networks Lab

Server Program

package lab.file;
import java.io.*;
import java.net.*;
public class Fileserver {
public static void main(String[] args) {
try
{
System.out.println("this is server and ready to
communication");
InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String filename;
int x=0;
ServerSocket ss=new ServerSocket(3000);
Socket s=ss.accept();
//FileInputStream fin;
InputStream is=s.getInputStream();
DataInputStream dis=new DataInputStream(is);
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
filename=dis.readUTF();
File f=new File(filename);
FileInputStream fin=new FileInputStream(filename);
DataInputStream in=new DataInputStream(fin);
BufferedReader b=new BufferedReader(new
InputStreamReader(in));
String str;
/*while((x=fin.read())!=-1)
{
dos.write(x);
}*/
while((str=b.readLine())!=null)
{
dos.writeUTF(str);
}

}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Department Of Computer Applications, T.K.M College of Engineering


35 | P a g e
Computer Networks Lab

OUTPUT:

Client
this is client and ready to communication
enter the file name
hello.txt
contents of the file are
heloooooooooo
haiiiiiiiiii

Server

this is server and ready to communication

Department Of Computer Applications, T.K.M College of Engineering


36 | P a g e

You might also like