Beruflich Dokumente
Kultur Dokumente
Aim:
To create a client server program for message transfer using Winsock control.
Procedures:
Creating the client:
1. Start a new EXE project
2. Add a Winsock control to your application.
3. Add all the control to the form which required for the program.
Creating the server:
1. Start a new standard EXE in VB
2. Add the Winsock control to your application.
3. Add the control to the form as shown in the accompanying code.
Get click client request from the database
strdata= item=&sitemdata&
rs.open select*from prices,str connect,
addopenkeyset
Source Code:
Client side:
Private Sub cmdConnect_Click()
sockMain.RemoteHost = txtHost.Text
sockMain.RemotePort = txtPort.Text
sockMain.Connect
End Sub
Private Sub cmdSend_Click()
sockMain.SendData txtSend.Text
End Sub
Private Sub Form_Load()
Server.Show
txtSend.Text = ""
End Sub
Private Sub sockMain_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
sockMain.GetData strData, vbString
txtStatus.Text = txtStatus.Text & _
strData & vbCrLf
End Sub
Server Side:
Private Sub cmdListen_Click()
sockMain.LocalPort = txtPort.Text
sockMain.Listen
End Sub
Private Sub cmdSend_Click()
sockMain.SendData txtSend.Text
End Sub
Private Sub sockMain_ConnectionRequest(ByVal requestID As Long)
If sockMain.State <> sckClosed Then
sockMain.Close
End If
sockMain.Accept requestID
txtStatus.Text = txtStatus.Text & _
"Accept Connection From : " & _
sockMain.RemoteHostIP & vbCrLf
End Sub
Private Sub Form_Load()
txtStatus.Text = ""
txtSend.Text = ""
End Sub
Private Sub sockMain_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
sockMain.GetData strData, vbString
txtStatus.Text = txtStatus.Text & _
strData & vbCrLf
End Sub
RESULT:
Thus the VB program is executed and output is verified successfully.
IMPLEMENTATION OF FTP
AIM:
To write a java program to perform File Transfer Protocol.
ALGORITHM
SERVER SIDE
1. Import the java packages and create class fileserver.
2. String of argument is passed to the args[].
3. Create a new server socket and bind it to the port.
4. Accept the client connection at the requested port.
5. Get the filename and stored into the BufferedReader.
6. Create a new object class file and readline.
7. If File is exists then FileReader read the content until EOF is reached.
8. Else Print FileNamedoest exits.
9. End of main.
10. End of FileServer class.
CLIENT SIDE
1.
2.
3.
4.
IMPLEMENTATION OF FTP
SOURCE CODE
SERVER
import java.io.*;
import java.net.*;
public class fileserver1
{
public static void main(String args[])throws IOException
{
ServerSocket s1=null;
try
{
s1=new ServerSocket(1187);
}
catch(IOException u1)
{
System.out.println("could not found port 1187");
System.exit(1);
}
Socket c=null;
try
{
c=s1.accept();
System.out.println("connection frame" +c);
}
catch(IOException e)
{
System.out.println("accept failed");
System.exit(1);
}
PrintWriter out=new PrintWriter(c.getOutputStream(),true);
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the text file name");
String s=sin.readLine();
File f=new File(s);
if(f.exists())
{
BufferedReader in=new BufferedReader(new FileReader(s));
String v;
while((v=in.readLine())!=null)
{
out.write(v);
out.flush();
}
System.out.println("the file send successfully");
in.close();
c.close();
s1.close();
}
}
}
CLIENT
import java.io.*;
import java.net.*;
public class fileclient1
{
public static void main(String args[])throws IOException
{
Socket s=null;
BufferedReader b=null;
try
{
s=new Socket(InetAddress.getLocalHost(),1187);
b=new BufferedReader(new InputStreamReader(s.getInputStream()));
}
catch(Exception u)
{
System.out.println("the file is received");
System.out.println("don't know host");
System.exit(1);
}
String inp;
while((inp=b.readLine())!=null)
{
System.out.println("the content of the file is");
System.out.println(inp);
System.out.println("the file is received successfully");
}
b.close();
s.close();
}
}
OUTPUT:
SERVER
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac fileserver1.java
C:\Program Files\Java\jdk1.6.0\bin>java fileserver1
connectionframeSocket[addr=/127.0.0.1,port=1056,localport=1187]
enter the text file name
HAI.txt
the file send successfully
C:\Program Files\Java\jdk1.6.0\bin>
CLIENT
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac fileclient1.java
C:\Program Files\Java\jdk1.6.0\bin>java fileclient1
the content of the file is
GOD LOVE'S EVERY ONE IN THE WORLD.
the file is received successfully
C:\Program Files\Java\jdk1.6.0\bin>
RESULT:
ECHO
AIM
To write a program in java to demonstrate the ECHO command.
ALGORITHM
ECHOSERVER
1. Start the program.
2. Import java.net and other necessary packages.
3. Create objects for DataInputStream, Socket and PrintWriter to receive client
message and send it back.
4. Store the message in a string and print the message using print() method.
5. Send the same received message to the client using PrintWriter and Socket.
6. When the received character is ., then stop sending the data back.
7. Stop the program.
ECHOCLIENT
1. Start the program.
2. Import java.net and other necessary packages.
3. Create objects for ServerSocket and Socket to send the message.
4. Create objects for PrintStream to write message from client to server.
5. Get the user input and store it in a string.
6. Print the string in the Socket using PrintStream to be received by the server.
7. Using the Print() method, receive the client echo message and print it.
8. If the user input is ., then stop sending the data.
9. Stop the program.
ECHO
SOURCE CODE
ECHO SERVER
import java.io.*;
import java.net.*;
class echoserver
{
public static void main(String args[])
{
try
{
Socket s=null;
ServerSocket ss=new ServerSocket(8000);
s=ss.accept();
System.out.println(s);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter print=new PrintWriter(s.getOutputStream());
int i=1;
while(i>0)
{
String str=br.readLine();
if(str.equals("."))
break;
System.out.println("msg received by client:"+str);
print.println(str);
print.flush();
}}
catch(IOException e)
{
System.out.println("\n error:"+e);
}
}
}
ECHO CLIENT
import java.io.*;
import java.net.*;
class echoclient
{
public static void main(String a[])
{
try
{
Socket s=new Socket("LocalHost",8000);
DataInputStream in=new DataInputStream(System.in);
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br2=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter print=new PrintWriter(s.getOutputStream());
System.out.println("\n msg to be echo:");
String str=br1.readLine();
print.println(str);
print.flush();
System.out.println(br2.readLine());
}
catch(UnknownHostException e)
{}
catch(IOException e)
{
System.out.println("\n error:"+e);
}
}
}
OUTPUT
ECHOCLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac echoclient.java
D:\Chitra\Java>java echoclient
msg to be echo:
GOD IS GREAT
GOD IS GREAT
D:\Chitra\Java>
ECHOSERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac echoserver.java
D:\Chitra\Java>java echoserver
Socket[addr=/127.0.0.1,port=1623,localport=8000]
RESULT
Thus the java program is executed and output is verified successfully.
dp=new DatagramPacket(buff,buff.length,InetAddress.getLocalHost(),8000);
ds.send(dp);
System.out.println("do u want to continue:yes or no");
str1=in.readLine();
if(str1.equals("yes"))
{
i=true;
}
else
{
i=false;
}
}
}
public static void main(String args[])throws IOException
{
udp1sender se=new udp1sender();
se.send();
}
}
RECEIVER
import java.io.*;
import java.net.*;
class udp1receiver
{
DatagramSocket ds;
DatagramPacket dp;
byte buff[]=new byte[1024];
String str;
public void receive() throws IOException
{
ds=new DatagramSocket(8000);
while(true)
{
dp=new DatagramPacket(buff,buff.length);
ds.receive(dp);
str=new String (dp.getData(),0,0,dp.getLength());
System.out.println(str);
System.out.println("InetAddress:"+dp.getAddress());
}
}
OUTPUT
SERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp1sender.java
D:\Chitra\Java>java udp1sender
Enter the msg:
IT KINGS
do u want to continue:yes or no
no
D:\Chitra\Java>
CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp1receiver.java
D:\Chitra\Java>java udp1receiver
IT KINGS
InetAddress:/172.16.11.99
D:\Chitra\Java>
RESULT
Thus the java program is executed and output is verified successfully.
ALGORITHM
SERVER
1. Start the program.
2. Import java.net and other necessary packages.
3. Create objects for DatagramSocket and DatagramPacket to receive packet
data from client.
CLIENT
1. Start the program.
2. Import java.net and other necessary packages.
3. Create objects for DatagramSocket and DatagramPacket to receive packet
data from server.
4. Create an object for InetAddress of the LocalHost.
5. Receive the server data using receive() method and store it in a string.
6. Run a loop and store the data in the string until the received message is end.
7. Print the received servers message.
8. Get user input in the same loop and send data until the user input is end.
9. Convert the user input into bytes and send the byte using DatagramPacket and
DatagramSocket.
10. If end is encountered, exit sending data to server.
11. Stop the program.
OUTPUT
SERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp2sender.java
D:\Chitra\Java>java udp2sender
CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac udp2receiver.java
D:\Chitra\Java>java udp2receiver
Msg Received:GOOD MORNING
Enter a line of text to send:
HAVE A NICE DAY
Msg Received:end
D:\Chitra\Java>
RESULT:
Thus the java program is executed and output is verified successfully.
PICKPAKING COMMAND
AIM
To write a program in java to demonstrate the usage of PICKPAKING command.
ALGORITHM
PINGSERVER
1.
2.
3.
4.
5.
6.
7.
8.
PINGCLIENT
1. Start the program.
2. Import java.net and other necessary packages.
3. Initialize the ping client with both sockets as null value.
4. Start the socket.
5. Get the IP address of the server.
6. Ping the server.
7. At the receiver end, the server is pinged.
8. Stop the program.
PICKPAKING COMMAND
SOURCE CODE
PING SERVER
import java.io.*;
import java.net.*;
public class pingserver
{
public static void main(String a[])
{
String line1,line2;
int i;
ServerSocket es;
DataInputStream di;
PrintStream ps;
Socket csoc;
es=null;
csoc=null;
try
{
es=new ServerSocket(9999);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("ping server");
try
{
csoc=es.accept();
di=new DataInputStream(csoc.getInputStream());
ps=new PrintStream(csoc.getOutputStream());
for(i=0;i<4;i++)
{
line1=di.readLine();
System.out.println("pinged by client");
ps.println(line1+"reply from host:bytes=3<time<1ms TT<=128");
}
di.close();
ps.close(); }
catch(Exception e)
{
System.out.println(e);
}}}
PING CLIENT
import java.io.*;
import java.net.*;
public class pingclient
{
public static void main(String args[])
{
PrintWriter out=null;
int i,j,k;
BufferedReader networkIn=null;
try
{
System.out.println("enter the IP address:");
DataInputStream in = new DataInputStream(System.in);
String ip = in.readLine();
Socket thesocket = new Socket(ip, 9999);
networkIn = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(thesocket.getOutputStream());
System.out.println("\npinging" + ip + "with 32 bytes of data\n");
for (i = 0; i < 4; i++)
{
out.println(ip);
out.flush();
String inp = networkIn.readLine();
if (inp != null)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
}
}
System.out.println("reply from" + inp);
}
else
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 10000; j++)
{
for (k = 0; k < 50000; k++)
{
OUTPUT
PICKPAKING CLIENT
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac pingclient.java
D:\Chitra\Java>java pingclient
enter the IP address:
192.168.1.10
pinging192.168.1.10with 32 bytes of data
5
reply from5
8
reply from8
9
reply from9
4
reply from4
D:\Chitra\Java>
PICKPAKING SERVER
C:\Documents and Settings\Mitstaff>cd\
C:\>cd D:\Chitra\Java
D:\Chitra\Java> set path=c:\program files\java\jdk1.6.0_02\bin
D:\Chitra\Java>javac pingserver.java
D:\Chitra\Java>java pingserver
ping server
pinged by client
pinged by client
pinged by client
pinged by client
D:\Chitra\Java>
RESULT
AIM:
To write a program in java for sliding window protocol.
ALGORITHM:
SERVER:
1. Start the program.
2. Import .net and other necessary packages.
3. Declare objects for input/output and Socket to receive the frame and
Send acknowledgement.
4. Wait for the client to establish connection.
5. Receive the frame one by one from the socket and return the frame number
as acknowledgement within the thread sleep time.
6. send acknowledgement for each receiving frame and continue the process
until acknowledgement for all frames are sent.
7. when acknowledgement the last frame, exit the program.
CLIENT:
1. Start the program.
2. Import .net and other necessary packages.
3. Create objects for Serversocket, socket to send the frames.
4. Display the server address when the server is connected.
5. Get the number of frames to be sent, from the user.
for(int i=1;i<=p;++i)
{
System.out.println("sending frame no"+i);
out.write(i);
out.flush();
System.out.println("waiting for acknowledge");
Thread.sleep(5000);
int a=in.read();
System.out.println("received acknowledge for frame no:"+i+"as"+a);
}
out.flush();
in.close();
out.close();
client.close();
Serversocket.close();
System.out.println("quiting");
}
catch(IOException e)
{
System.out.println(e);
}
catch(InterruptedException e)
{}
}
}
CLIENT:
importjava.lang.System;
import java.net.*;
import java.io.*;
importjava.math.*;
class client
{
public static void main(String a[])
{
try
{
InetAddressaddr=InetAddress.getByName("Localhost");
System.out.println(addr);
Socket connection=new Socket(addr,500);
DataOutputStream out=new DataOutputStream(connection.getOutputStream());
BufferedInputStream in=new
BufferedInputStream(connection.getInputStream());
BufferedInputStream inn=new
BufferedInputStream(connection.getInputStream());
BufferedReaderki=new BufferedReader(new InputStreamReader(System.in));
int flag=0;
System.out.println("connect");
System.out.println("enter the no of frames to be requested to server:");
int c=Integer.parseInt(ki.readLine());
out.write(c);
out.flush();
inti,jj=0;
while (jj<c)
{
i=in.read();
System.out.println("received frame no"+i);
System.out.println("sending acknowledgement for frame no"+i);
out.write(i);
out.flush();
jj++;
}
out.flush();
in.close();
inn.close();
out.close();
System.out.println("quiting");
}
Catch (Exception e)
{
System.out.println(e);}}}
OUTPUT
BITCLIENT
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac bitclient.java
C:\Program Files\Java\jdk1.6.0\bin>java bitclient
Localhost/127.0.0.1
connect
enter the no of frames to be requested to server:
4
received frame no1
sending acknowledgement for frame no1
received frame no2
RESULT:
Thus the java program is executed and output is verified successfully.
SHOREST PATH
AIM:
To implement the shortest path routing (Dijkstras Algorithm) to find the shortest
path between the nodes.
ALGORITHM:
1. Dijkstras shortest path algorithm computes all shortest path from a single node.
2. It can also be used for the all pairs shortest path problem, by the simple
expedient of applying it N times once to each vertex.
3. Get the number of nodes in the network for which the shortest path is to be
calculated.
4. Represent the nodes that are connected by cost value (Number of hopes delay
bandwidth, etc.,) and nodes that are not connected by infinite value in an
adjacent matrix.
5. To find the shortest path between node follow the steps as stated below.
a. Initially=V, where T= set of nodes and V= nodes for which the
shortest path is to be found.
b. At each step of the algorithm the vertex in T with the smaller d value is
removed from T.
OUTPUT
SOURCE CODE:
import java.net.*;
import java.io.*;
class spath
{
public static void main(String args[]) throws IOException
{
int n,s,d,i,j,y=0,sd=100;
int[] in=new int[10];
int[][] m=new int[5][5];
int[] dis=new int[10];
int[] path=new int[10];
DataInputStream a=new DataInputStream(System.in);
System.out.println("Enter the no of vertex:");
n=Integer.parseInt(a.readLine());
System.out.println("Enter the source vertex:");
s=Integer.parseInt(a.readLine());
System.out.println("Enter the destination vertex:");
d=Integer.parseInt(a.readLine());
for(i=1;i<n;i++)
{
j=1;
while(j<n)
{
System.out.println("Enter the distance between"+i+ "and" +(j+1));
m[i][j+1]=Integer.parseInt(a.readLine());
m[j+1][i]=m[i][j+1];
j++;
}
}
for(i=1;i<=n;i++)
{
in[i]=0;
dis[i]=m[s][i];
if(m[s][i]!=0)
path[i]=s;
}
in[s]=1;
dis[s]=0;
for(i=2;i<n;i++)
{
for(j=1;j<=n;j++)
{
if(in[j]==0)
{
if(dis[j]<sd)
{
sd=dis[j];
y=j;
}
}
}
in[y]=1;
for(j=1;j<=n;j++)
{
if((in[j]==0)&&(m[y][j]!=0))
{
if((dis[y]+m[y][j])<dis[j])
{
dis[j]=dis[y]+m[y][j];
path[j]=y;
}
}
}
}
System.out.println(" "+d+"<----");
i=d;
while(path[i]!=s)
{
System.out.println(" "+path[i]+"<----");
i=path[i];
}
System.out.println(" "+s);
System.out.println("Distance is "+dis[d]);
}
}
OUTPUT:
C:\Documents and Settings\LABCSE1>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac spath.java
C:\Program Files\Java\jdk1.6.0\bin>java spath
Enter the no of vertex:
3
Enter the source vertex:
1
Enter the destination vertex:
3
Enter the distance between1and2
2
Enter the distance between1and3
1
Enter the distance between2and2
0
Enter the distance between2and3
0
3<---1
Distance is 1
RESULT:
6. Now add the generator data to the original data and send the string as
transmitter string.
7. In the receiving end, enter the generator code.
8. Using the generator code, to the length of the received string, add 48 to the
number format of the string by character.
9. If the generator string is wrong, display message received with error.
10. If the generator string is correct, perform step 8 and display message
received with no error.
11. End the program.
data=in.readLine();
flen=data.length()-1;
for(i=0;i<data.length();i++)
f[i]=((int)(data.charAt(i)))-48;
System.out.println("Enter the generator:");
data=in.readLine();
glen=data.length()-1;
for(i=1;i<=glen;i++)
f[flen+i]=0;
flen=flen+glen;
for(i=0;i<data.length();i++)
gen[i]=((int)(data.charAt(i)))-48;
p=0;
sum=0;
for(i=flen;i>=0;i--)
{
sum=sum+(f[i]*(int)Math.pow(2,p));
p=p+1;
}
iframe=sum;
p=0;
sum=0;
for(i=glen;i>=0;i--)
{
sum=sum+(gen[i]*(int)Math.pow(2,p));
p=p+1;
}
igen=sum;
irem=iframe%igen;
irem=igen-irem;
iframe+=irem;
i=0;
while(iframe>0)
{
f[i]=iframe%2;
iframe=iframe/2;
i++;
}
if(iframe==1)
f[i]=iframe;
System.out.println("Transmitted string:");
for(i=flen;i>=0;i--)
System.out.print(f[i]);
System.out.println("\n enter the received string:");
data=in.readLine();
flen=data.length()-1;
for(i=0;i<data.length();i++)
f[i]=((int)(data.charAt(i)))-48;
p=0;
sum=0;
for(i=flen;i>=0;i--)
{
sum=sum+(f[i]*(int)Math.pow(2,p));
p=p+1;
}
iframe=sum;
irem=iframe%igen;
if(irem==0)
System.out.println("Message received with no error");
else
System.out.println("Message received with error");
}
}
OUTPUT
C:\Documents and Settings\SEENU.R>cd\
C:\>cd C:\Program Files\Java\jdk1.6.0\bin
C:\Program Files\Java\jdk1.6.0\bin>javac crc.java
C:\Program Files\Java\jdk1.6.0\bin>java crc
Enter the frame:
10101011
Enter the generator:
1011
Transmitted string:
10101011111
Enter the received string:
1011
Message received with no error
C:\Program Files\Java\jdk1.6.0\bin>java crc
Enter the frame:
10101011
Enter the generator:
1011
Transmitted string:
10101011111
Enter the received string:
1001
Message received with error
C:\Program Files\Java\jdk1.6.0\bin>
RESULT: