Advance Networking Lab File
Advance Networking Lab File
AIM:
Write a program to create a client server application in which client sends
some string to server and the server responds by converting it to
uppercase(Remote server n client)
CODING
SERVER PROGRAM
import java.io.*;
import java.net.*;
class Remoteserver
{
public static void main(String args[])throws Exception
{ ServerSocket ss=new ServerSocket(1001);
System.out.println("Listening");
while(true)
{Socket s= ss.accept();
BufferedReader bin=new BufferedReader(new
InputStreamReader(s.getInputStream()));
String s1= bin.readLine();
System.out.println("processing");
d1.writeBytes(s2+'\n');
System.out.println("output");
}
}
}
CLIENT PROGRAM
import java.net.*;
import java.io.*;
class RemoteClient
{
public static void main( String args[]) throws IOException, UnknownHostException
{
String sin,sout;
Socket s = new Socket("10.0.13.112",1001);
BufferedReader bclient = new BufferedReader (new InputStreamReader(System.in));
DataOutputStream dos= new DataOutputStream(s.getOutputStream());
BufferedReader bserver= new BufferedReader( new
InputStreamReader(s.getInputStream()));
System.out.print("enter a string in lower case");
sin = bclient.readLine();
dos.writeBytes(sin+'\n');
sout= bserver.readLine();
System.out.print("the string is:"+sout);
s.close();
}
}
OUTPUT
SERVER OUTPUT
C:\jdk1.3\bin>javac RemoteServer.java
C:\jdk1.3\bin>java RemoteServer
Listening
processing
output
CLIENT OUTPUT
C:\jdk1.6.0_18\bin>javac RemoteClient.java
C:\jdk1.6.0_18\bin>java RemoteClient
enter a string in lower caseahoihod
the string is:AHOIHOD
PRACTICAL 4
AIM:
Develop a client server application to implement one way chatting.
CODING
SERVER PROGRAM
import java.io.*;
import java.net.*;
public class ChatServer
{
public static void main(String args[])throws
IOException,UnknownHostException
{
String S1,S2;
ServerSocket SS=new ServerSocket(1001);
System.out.println("Receiving Chat...");
while(true)
{
Socket S=SS.accept();
BufferedReader bin=new BufferedReader(new
InputStreamReader(S.getInputStream()));
DataOutputStream d1=new DataOutputStream(S.getOutputStream());
S1=bin.readLine();
System.out.println("Received Chat From Client:");
System.out.println(S1);
}
}
}
CLIENT PROGRAM
import java.io.*;
import java.net.*;
class ChatClient
{
public static void main(String args[])throws
UnknownHostException,IOException
{
String sin,sout;
System.out.println("Enter Chat To Be Send: ");
BufferedReader bclient=new BufferedReader(new
InputStreamReader(System.in));
sin=bclient.readLine();
Socket s=new Socket("192.168.1.2",1001);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeBytes(sin+'\n');
s.close();
}
}
SERVER OUTPUT
C:\j2sdk1.4.2_09\bin>javac ChatServer.java
C:\j2sdk1.4.2_09\bin>java ChatServer
Listening
Receiving chat
Received Chat From Client:
hello server
CLIENT OUTPUT
C:\j2sdk1.4.2_09\bin>javac ChatClient.java
C:\j2sdk1.4.2_09\bin>java ChatClient
Enter Chat To Be Send:
hello server
PRACTICAL 5
AIM
Write a program to create a client server application for sharing system date and
time.
CODING
CLIENT SIDE
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class LocalClientTime
{
public static void main(String args[]) throws Exception
{
String str=new String();
String str1=new String();
str="null";
Socket s =new Socket("LocalHost",1001);
System.out.println("Receiving Server Date and Time..");
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
BufferedReader bserver= new BufferedReader(new
InputStreamReader(s.getInputStream()));
str="Send your system Date";
dos.writeBytes(str+'\n');
str1=bserver.readLine();
System.out.println("Server System Date and Time is: "+str1);
}
}
SERVER SIDE
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
public class LocalServerTime
{
public static void main(String args[]) throws Exception
{
String S,S1;
Date d1=new Date();
System.out.println("Listening mode..");
ServerSocket ss=new ServerSocket(1001);
while(true)
{
Socket s2=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader
(s2.getInputStream()));
DataOutputStream d=new
DataOutputStream(s2.getOutputStream());
S=br.readLine();
S1=d1.toString();
d.writeBytes(S1+'\n');
System.out.println("Sent");
}
}
}
OUTPUT
SERVER SIDE
Listening mode..
Sent
CLIENT SIDE
Write a program to create a client server application for sending the encrypted
data from the client end and then decrypting it at the server end.
THEORY
Caesar cipher or Caesar shift is one of the simplest and most widely known encryption
techniques. The Caesar cipher is a substitution cipher: each letter is replaced by
another. The method is named after Julius Caesar, who used this technique to
communicate with his generals. The cipertext alphabets are derived from the plaintext
alphabet by shifting each letter a certain number of spaces. For example, if we are
shifting each alphabet by three then A becomes D, B becomes E, C becomes F, and so
on. For the last letters, we think of the alphabet as a circle and wrap around, W
becomes Z, X becomes A, Y becomes B, and Z becomes C. Then the cipertext is
decrypted into the plaintext by back shifting each letter by that particular number of
spaces it was shifted before for encryption.
CODING
CLIENT SIDE
import java.io.*;
import java.net.*;
class LocalCeaserClient
{
public static void main(String args[])throws
UnknownHostException,IOException
{
String s1="",sin;
char c;
int i;
System.out.println("Enter String ");
BufferedReader bclient=new BufferedReader(new
InputStreamReader(System.in));
sin=bclient.readLine();
for(i=0;i<sin.length();i++)
{
c=(char)(sin.charAt(i)+3);
s1=s1+c;
}
Socket s=new Socket("LocalHost",1001);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
dos.writeBytes(s1+'\n');
s.close();
}
}
SERVER SIDE
import java.io.*;
import java.net.*;
public class LocalCeaserServer
{
public static void main(String args[])throws
IOException,UnknownHostException
{
String s3="",s2;
char c;
int i;
ServerSocket SS=new ServerSocket(1001);
System.out.println("\nListening Mode....");
while(true)
{
Socket S=SS.accept();
BufferedReader bin=new BufferedReader(new
InputStreamReader(S.getInputStream()));
DataOutputStream d1=new
DataOutputStream(S.getOutputStream());
s2=bin.readLine();
System.out.println("Encrypted String is:" +s2);
for(i=0;i<s2.length();i++)
{
c=(char)(s2.charAt(i)-3);
s3=s3+c;
}
System.out.println("Decrypted string is:"+ s3);
}
}
}
OUTPUT
CLIENT SIDE
Enter String
I am Joe
SERVER SIDE
Listening..
Encrypted String is: L#dp#Duslwd#Vulydvwdyd
Original or Decrypted string is: I am Joe
PRACTICAL 1
AIM
CODING
CLIENT SIDE
#include "unp.h"
int main(int argc, char **argv)
{
int sockfd, n;
char recvline[MAXLINE + 1];
struct sockaddr_in servaddr;
if (argc != 2)
err_quit("usage: a.out <IPaddress>");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13); /* daytime server */
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
err_quit("inet_pton error for %s", argv[1]);
exit(0);
}
PRACTICAL 2
AIM
CODING
CLIENT SIDE
#include "unp.h"
#include <time.h>
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(13); /* daytime server */
Listen(listenfd, LISTENQ);
for ( ; ; ) {
connfd = Accept(listenfd, (SA *) NULL, NULL);
ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
Write(connfd, buff, strlen(buff));
Close(connfd);
}
}
PRACTICAL 1
AIM
CODING
CLIENT SIDE
import java.io.*;
import java.net.*;
import java.util.*;
class DateClient
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])throws IOException,UnknownHostException
{
String s1,s2;
Socket s=new Socket("Localhost",1331);
//this line will execute only if there is a socket at client side
CODING
SERVER SIDE
import java.io.*;
import java.net.*;
import java.util.*;
class DateServer
{
static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])throws IOException,UnknownHostException
{
String s1,s2;
ServerSocket ss=new ServerSocket(1331);
Date d1=new Date();
while(true)
{
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream()));
s1=br.readLine(); //to read from client end
p("Input From Client Side :-");
p(s1);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
if(s1.equalsIgnoreCase("DATE"))
{
s2=d1.toString(); //to read from client end
dos.writeBytes(s2+'\n');
}
else
{ s2="Not a Command!!!!!";
dos.writeBytes(s2+'\n');
}
}
}
}