Implementation of Message Broker System
Code:
FormatServer.java
EchoServer.java
EchoClient.java
Output:
Code:
FormatServer.java
import java.sql.*;
public class FormatServer {
public static String formatString(String str,int no)
{
String temp="";
try{
//System.out.println("in format string");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mess");
Statement stat = con.createStatement();
ResultSet rset=stat.executeQuery("select * from formats where cname='"+no+"'");
String tcase="",delim="",end="";
while(rset.next())
{
tcase=rset.getString(2);
delim=rset.getString(3);
end=rset.getString(4);
}
for(int i=0;i<str.length();++i)
{
char ch=str.charAt(i);
if(ch==' ')
temp=temp+delim;
else
temp=temp+ch;
}
temp=temp+end; // End of Statement
if(tcase.equals("upper")) //Changing case
temp=temp.toUpperCase();
else
temp=temp.toLowerCase();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return temp;
}}
public class FormatServer {
public static String formatString(String str,int no)
{
String temp="";
try{
//System.out.println("in format string");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mess");
Statement stat = con.createStatement();
ResultSet rset=stat.executeQuery("select * from formats where cname='"+no+"'");
String tcase="",delim="",end="";
while(rset.next())
{
tcase=rset.getString(2);
delim=rset.getString(3);
end=rset.getString(4);
}
for(int i=0;i<str.length();++i)
{
char ch=str.charAt(i);
if(ch==' ')
temp=temp+delim;
else
temp=temp+ch;
}
temp=temp+end; // End of Statement
if(tcase.equals("upper")) //Changing case
temp=temp.toUpperCase();
else
temp=temp.toLowerCase();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return temp;
}}
EchoServer.java
import java.io.*;
import java.net.*;
public class EchoServer
{
Socket socket=null;
static ServerSocket ss;
static EchoServer arr[] = new EchoServer[4];
static int count=0;
BufferedReader in=null;
PrintStream out=null;
BufferedReader br=null;
EchoServer(Socket newSocket)throws IOException
{
this.socket=newSocket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String args[]) throws IOException
{
ss=new ServerSocket(7000);
System.out.println("Server Started");
while(count<4) // we need 4 clients
{
Socket s = ss.accept();
EchoServer es = new EchoServer(s);
arr[count++]=es;
}
System.out.println("Server ready");
func();
}
public static void func()throws IOException
{
String str="";
while(true)
{
int src=(int)(Math.random()*100);
src=src%4;
//System.out.println(" src "+src);
int dest=(int)(Math.random()*100)%4;
dest=dest%4;
while(true)
{
dest=(int)(Math.random()*100);
dest=dest%4;
if(dest!=src)
break;
}
//System.out.println(" dest "+dest);
arr[src].out.println("10000001"); //sending flag requsting transmission
str=arr[src].in.readLine(); //reading from src client
//System.out.println("Server ready"+str);
str=FormatServer.formatString(str,dest+1);
arr[dest].out.println(str); //writing to dest client
}
}
}
import java.net.*;
public class EchoServer
{
Socket socket=null;
static ServerSocket ss;
static EchoServer arr[] = new EchoServer[4];
static int count=0;
BufferedReader in=null;
PrintStream out=null;
BufferedReader br=null;
EchoServer(Socket newSocket)throws IOException
{
this.socket=newSocket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
br = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String args[]) throws IOException
{
ss=new ServerSocket(7000);
System.out.println("Server Started");
while(count<4) // we need 4 clients
{
Socket s = ss.accept();
EchoServer es = new EchoServer(s);
arr[count++]=es;
}
System.out.println("Server ready");
func();
}
public static void func()throws IOException
{
String str="";
while(true)
{
int src=(int)(Math.random()*100);
src=src%4;
//System.out.println(" src "+src);
int dest=(int)(Math.random()*100)%4;
dest=dest%4;
while(true)
{
dest=(int)(Math.random()*100);
dest=dest%4;
if(dest!=src)
break;
}
//System.out.println(" dest "+dest);
arr[src].out.println("10000001"); //sending flag requsting transmission
str=arr[src].in.readLine(); //reading from src client
//System.out.println("Server ready"+str);
str=FormatServer.formatString(str,dest+1);
arr[dest].out.println(str); //writing to dest client
}
}
}
EchoClient.java
import java.io.*;
import java.net.*;
public class EchoClient
{
public static void main(String args[])throws IOException
{
Socket s=new Socket("localhost",7000);
PrintStream out = new PrintStream(s.getOutputStream());
BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while(true)
{
str=in.readLine();
if(str.equals("10000001")) // flag indicates data to be send
{
System.out.println("Enter Data to send ");
str=br.readLine();
out.println(str);
}
else
System.out.println(str);
}
}
}
import java.net.*;
public class EchoClient
{
public static void main(String args[])throws IOException
{
Socket s=new Socket("localhost",7000);
PrintStream out = new PrintStream(s.getOutputStream());
BufferedReader in = new BufferedReader( new InputStreamReader(s.getInputStream()));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while(true)
{
str=in.readLine();
if(str.equals("10000001")) // flag indicates data to be send
{
System.out.println("Enter Data to send ");
str=br.readLine();
out.println(str);
}
else
System.out.println(str);
}
}
}
Output:
0 comments:
Confused? Feel free to ask
Post a Comment