Java Socket – Client and Server Communication

/*  UDP – User Data Protocol – Client and server communication

A Graphical User Interface Program */

 

import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GUIClient extends JFrame
{

JButton send=new JButton(“Send”);
JTextField msg=new JTextField(20);
TextArea chat=new TextArea(20,20);
DatagramSocket ds=null;
DatagramPacket dp=null;

public class Receiver extends Thread
{
Thread t=null;
public Receiver()
{
t=new Thread(this);
t.start();
}
public void run()
{
String str1=”Server:”;
String str2;
byte data[]=new byte[1024];
try{
do{
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
str2=dp.getData().toString();
chat.append(str1+str2+”\n”);

}while(!str2.equals(“end”));
}catch(Exception e)
{System.out.println(e);}
}
}

public static void main(String s[])
{
JFrame obj=new GUIClient();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setTitle(“CLIENT”);
obj.setVisible(true);
obj.setSize(400,400);
}

public GUIClient()
{
try{
ds=new DatagramSocket(7090);
}
catch(Exception e)
{
System.out.println(e);
}
components();
events();
Receiver r=new Receiver();
}

void components()
{
getContentPane().setLayout(null);
send.setBounds(270,20,80,30);
msg.setBounds(50,20,200,20);
chat.setBounds(20,60,320,300);
getContentPane().add(send);
getContentPane().add(msg);
getContentPane().add(chat);
}

void events()
{
send.addActionListener(new s());
}

public class s implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str3;
byte data2[]=new byte[1024];
try{
InetAddress ia=InetAddress.getByName(“localhost”);// or IP of server
str3=msg.getText();
data2=str3.getBytes();
dp=new DatagramPacket(data2,data2.length,ia,7000);
ds.send(dp);
//ds.close();
}catch(Exception e1)
{
System.out.println(e1);
}
}
}
}

—————————–

import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GUIServer extends JFrame
{

JButton send=new JButton(“Send”);
JTextField msg=new JTextField(20);
TextArea chat=new TextArea(20,20);
DatagramSocket ds=null;
DatagramPacket dp=null;

public class Receiver extends Thread
{
Thread t=null;
public Receiver()
{
t=new Thread(this);
t.start();
}
public void run()
{
String str1=”Client:”;
String str2;
byte data[]=new byte[1024];
try{
do{
DatagramPacket dp=new DatagramPacket(data,1024);
ds.receive(dp);
str2=dp.getData().toString();
chat.append(str1+str2+”\n”);
}while(!str2.equals(“end”));
}catch(Exception e)
{System.out.println(e);}
}
}

public static void main(String s[])
{
JFrame obj=new GUIServer();
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setTitle(“SERVER”);
obj.setVisible(true);
obj.setSize(400,400);
}

public GUIServer()
{
try{
ds=new DatagramSocket(7000);
}
catch(Exception e)
{
System.out.println(e);
}
components();
events();
Receiver r=new Receiver();
}

void components()
{
getContentPane().setLayout(null);
send.setBounds(270,20,80,30);
msg.setBounds(50,20,200,20);
chat.setBounds(20,60,320,300);
getContentPane().add(send);
getContentPane().add(msg);
getContentPane().add(chat);
}

void events()
{
send.addActionListener(new s());
}

public class s implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String str3;
byte data2[]=new byte[1024];
try{
InetAddress ia=InetAddress.getByName(“localhost”);
str3=msg.getText();
data2=str3.getBytes();
dp=new DatagramPacket(data2,data2.length,ia,7090);
ds.send(dp);
//ds.close();
}catch(Exception e1)
{
System.out.println(e1);
}
}
}
}

Advertisements

Dijkstra’s Shortest Path Algorithm Implementation

// Dijkstra’s Algorithm

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
#include<ctype.h>

struct node
{
struct node *llink,*rlink;
int data,wt;
};

typedef struct
{
int wt,v,via;
}tab;

struct node *find(struct node *head,int s)
{
struct node *t;
t=head;
while(t!=NULL)
{
if(t->data==s)
return t;
t=t->rlink;
}
}

struct node * insert(struct node *head,int i)
{
struct node *t,*temp;
temp=head;
t=(struct node*)malloc(sizeof(struct node));
t->llink=NULL;
t->rlink=NULL;
t->data=i;
t->wt=0;
if(head==NULL)
return t;
while(temp->rlink!=NULL)
temp=temp->rlink;
temp->rlink=t;
return head;
}

void insertNode(struct node *head)
{
struct node *t,*temp,*head1=NULL;
char ch;
do
{
temp=head1;
t=(struct node*)malloc(sizeof(struct node));
t->llink=NULL;
t->rlink=NULL;
printf(”
Enter destination :”);
scanf(“%d”,&t->data);
printf(”
Enter Weight :”);
scanf(“%d”,&t->wt);
if(head1==NULL)
{
head->llink=t;
head1=t;
}
else
{
while(temp->rlink!=NULL)
temp=temp->rlink;
temp->rlink=t;
}
printf(”
Do u continue y/n : “);
fflush(stdin);
scanf(“%c”,&ch);
}while(tolower(ch)==’y’);
}

void path(struct node *head,int s,int n)
{
struct node *temp;
tab *t;
int d,k,cost=0,i=2,j=1,s1,f=0,c=0,via=0;
t=(tab *)malloc(n*sizeof(tab));
s1=s;
for(i=1;i<=n;i++)
{
t[i].v=0;
t[i].wt=9999;
t[i].via=0;
}
while(j<n)
{
temp=find(head,s1);
via=temp->data;
temp=temp->llink;
d=temp->data;
while(temp!=NULL)
{
if((t[d].v)==0 && (t[d].wt > (cost+temp->wt)))
{
t[d].wt=cost+temp->wt;
t[d].via=via;
}
temp=temp->rlink;
d=temp->data;
}
cost=9999;
for(i=1;i<=n;i++)
{
if(t[i].v==0 && i!=s)
{
if(t[i].wt<cost)
{
cost=t[i].wt;
k=i;f=1;
}
}
}
if(f==1)
{
t[k].v=1;
c=1;
f=0;
}
j=j+1;
s1=k;
}
printf(”
Sourse via destination Distance”);
for(i=1;i<=n;i++)
{
if(t[i].v==1)
if(t[i].via!=s)
printf(”
%d %d -> %d : %d”,s,t[i].via,i,t[i].wt);
else
printf(”
%d — -> %d : %d”,s,i,t[i].wt);
}
if(c==0)
printf(”
No way to travel from city – %d to other city..”,s);
}

void main()
{
struct node * head;
int i,n,s,t;char ch;
struct node * insert(struct node *head,int);
void insertNode(struct node *head);
struct node *find(struct node *head,int s);
void path(struct node *,int,int);
head=NULL;
clrscr();
do
{
printf(”
1-Insertion”);
printf(”
2-Path”);
printf(”
3-Exit”);
printf(”
Ur Choice:”);
scanf(“%d”,&s);
switch(s)
{
case 1: //Insertion
printf(”
No of CITIES.. :”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
head=insert(head,i);
for(i=1;i<=n;i++)
{
printf(”
Do u create path from city-%d :”,i);
fflush(stdin);
scanf(“%c”,&ch);
if(tolower(ch)==’y’)
insertNode(find(head,i));
}
break;

case 2: //Path define
printf(”
From which city u want to travel :”);
scanf(“%d”,&t);
if(t<=n)
path(head,t,n);
else
printf(”
Enter Correct data….”);
break;

case 3: //exit
exit(0);
}
}while(s<4||s>0);
getch();
}

Linux Signals

Signals

/* Program to  perform operations depending on external signals */

#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<signal.h>
int a,b;
void division(int sig)
{
int c=0;
c = a/b;
printf(“\n\t QUOTIENT = %d”,c);
printf(“\tSignal = %d\n”,sig);
(void)signal(SIGINT,SIG_DFL);
}
void mul(int sig)
{
int c=0;
c = a*b;
printf(“\n\t PRODUCT = %d”,c);
printf(“\tSignal = %d\n”,sig);
(void)signal(SIGINT,division);
}
void sub(int sig)
{
int c=0;
c = a-b;
printf(“\n\t DIFFERENCE = %d”,c);
printf(“\tSignal = %d\n”,sig);
(void)signal(SIGINT,mul);
}

void add(int sig)
{
int c=0;
c= a+b;
printf(“\n\t SUM = %d”,c);
printf(“\tSignal = %d\n”,sig);
(void)signal(SIGINT,sub);
}
int main()
{
printf(“\n\t Enter first no. : “);
scanf(“%d”,&a);
printf(“\n\t Enter second no. : “);
scanf(“%d”,&b);
(void)signal(SIGINT,add);
while(1){sleep(1);}
return 0;
}

Shared Memory – Linux O.S

/* Program using shared memory */

#include <stdio.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main()
{
key_t key1 = 1000,key2 = 2000,key3 = 3000,shmid1,shmid2,shmid3;
int *A,*B,*C,i,j,k,m,n,p,q;

shmid1=shmget(key1,100*sizeof(int),IPC_CREAT);
if(shmid1==-1)
{
printf(“\n\t ERROR : ALLOCATION FAILED \n”);
exit(0);
}

shmid2=shmget(key2,100*sizeof(int),IPC_CREAT);
if(shmid2==-1)
{
printf(“\n\t ERROR : ALLOCATION FAILED \n”);
exit(0);
}

shmid3=shmget(key3,100*sizeof(int),IPC_CREAT);
if(shmid3==-1)
{
printf(“\n\t ERROR : ALLOCATION FAILED \n”);
exit(0);
}

A = shmat(shmid1,NULL,0);
B = shmat(shmid2,NULL,0);
C = shmat(shmid3,NULL,0);

printf(“\n\t ROWS for matrix A : “);
scanf(“%d”,&m);
printf(“\n\t COLUMNS for matrix A : “);
scanf(“%d”,&n);
printf(“\n\t Enter the elements : “);
for(i = 0;i<m;i++)
for(j = 0;j<n;j++)
scanf(“%d”,(A+(n*i)+j));

printf(“\n\t MATRIX A : “);
for(i = 0;i<m;i++)
{
printf(“\n”);
for(j = 0;j<n;j++)
printf(“\t%d”,*(A+(n*i)+j));
}

printf(“\n”);

printf(“\n\t ROWS for matrix B : “);
scanf(“%d”,&p);
printf(“\n\t COLUMNS for matrix B : “);
scanf(“%d”,&q);
printf(“\n\t Enter the elements : “);
for(i = 0;i<p;i++)
for(j = 0;j<q;j++)
scanf(“%d”,(B+(q*i)+j));

printf(“\n\t MATRIX B : “);
for(i = 0;i<p;i++)
{
printf(“\n”);
for(j = 0;j<q;j++)
printf(“\t%d”,*(B+(q*i)+j));
}

if(n != p)
{
printf(“\n\n\t ERROR : MATRICES NOT COMPATIBLE FOR MULTIPLICATION \n”);
printf(“\n\t\t\t PROGRAM TERMINATED \n\n”);
exit(0);
}

for(i = 0;i<m;i++)
{
for(j = 0;j<q;j++)
{
*(C+(q*i)+j) = 0;
for(k = 0;k<n;k++)
*(C+(q*i)+j) = *(C+(q*i)+j) + (*(A+(n*i)+k)) * (*(B+(q*k)+j));
}
}

printf(“\n\t THE PRODUCT OF matrices A & B are : \n”);
for(i = 0;i<m;i++)
{
printf(“\n”);
for(j = 0;j<q;j++)
printf(“\t%d”,*(C+(q*i)+j));
}

printf(“\n\n”);

shmctl(shmid1,IPC_RMID,NULL);
shmctl(shmid2,IPC_RMID,NULL);
shmctl(shmid3,IPC_RMID,NULL);

return 0;
}

Chat program using Java RMI

A Chat program using Remote Method Invocation ( RMI )

import java.rmi.*;
public interface Chat extends java.rmi.Remote
{

public String send()throws RemoteException;
public void receive(String str)throws RemoteException;

}

—–

import java.rmi.*;
import java.rmi.ServerError.*;
import java.io.*;
public class ChatImpl extends java.rmi.server.UnicastRemoteObject implements Chat
{
public ChatImpl() throws java.rmi.RemoteException
{
super();

}
public String send()throws java.rmi.RemoteException

{try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();System.out.println(str);
return str;
}catch(Exception e)
{
String str1=”some error”;
return str1;
}

}
public void receive(String str)throws java.rmi.RemoteException

{
System.out.println(str);
}
}

————

import java.rmi.*;
import java.io.*;
public class ChatClient implements Runnable{
static Chat c;
public static void main(String args[])throws IOException
{System.out.println(“enter static main”);

try
{
c=(Chat)Naming.lookup(“rmi://localhost/Chat”);
System.out.println(“lookupdone”);
}catch(Exception e){System.out.println(e);}
new ChatClient();
}
public ChatClient()
{
run();
}
public void run()
{

try{

String str=send();
System.out.println(“String is “+str);
c.receive(str);
System.out.println(c.send());
run();
}catch(Exception e){}
}
public String send()throws java.rmi.RemoteException

{try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();

return str;
}catch(Exception e)
{
String str1=”some error”;
return str1;
}

}
public void receive(String str)throws java.rmi.RemoteException

{
System.out.println(str);
send();
}
}

—————-

import java.rmi.*;
import java.io.*;
public class ChatServer1
{
public ChatServer1() {

try{
ChatImpl c=new ChatImpl();
Naming.rebind(“//localhost:4444/Chat”,c);
}catch(Exception e){}
}
public static void main(String args[])throws IOException
{
try
{

new ChatServer1();
}catch(Exception e){}
}

}

Epsilon Closure (E-Closure) to Convert NFA to DFA

Epsilon closure and transitions on input symbols for a set of NFA States

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void eclose(int);
void trans(int,int);
struct nfastates
{
int initial;
int final;
char inputsym;
}nfa[50];
int abtrans[50],ai=0,bi=0,ei=0,eclosure[50],queue[50],n,start;

void main()
{
int i;
clrscr();
printf(“\n Enter the no: of states\n”);
scanf(“%d”,&n);

printf(“\n Enter initial state,inputsymbol,finalstate\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&nfa[i].initial);
nfa[i].inputsym= getche();
scanf(“%d”,&nfa[i].final);
}
printf(“\n Enter the start state\n”);
scanf(“%d”,&start);
printf(“\n THE ECLOSURE is\n”);
eclose(start);
for(i=0;i<ei;i++)
{
printf(“%d\t”,eclosure[i]);
}
printf(“\n THE a-transitions are\n”);
trans(1,1);
for(i=0;i<ai;i++)
{
printf(“%d\t”,abtrans[i]);
}
printf(“\n THE b-transitions are\n”);
trans(2,2);
for(i=0;i<bi;i++)
{
printf(“%d\t”,abtrans[i]);
}
getch();
}
void eclose(int start)
{

int f=0,r=0,i;
eclosure[ei]=start;
ei++;
queue[r]=start;
r++;
do
{
start=queue[f];
f++;
for(i=0;i<n;i++)
{
if(nfa[i].initial==start && nfa[i].inputsym==’e’)
{
queue[r]=nfa[i].final;
r++;
eclosure[ei]=nfa[i].final;
ei++;
}
}
}while(f<r);
}
void trans(int a,int flag)
{
int i,f=0,r=0,j;
for(j=0;j<ei;j++)
{
f=0,r=0;
queue[r]=eclosure[j];
r++;
do
{
start=queue[f];
f++;
for(i=0;i<n;i++)
{
if(nfa[i].initial==start && nfa[i].inputsym==’a’)
{
queue[r]=nfa[i].final;
r++;
if(flag==1)
{
abtrans[ai]=nfa[i].final;
ai++;
}
else if(flag==2)
{
abtrans[bi]=nfa[i].final;
bi++;
}
}
}
}while(f<r);
}
}

C++ Constructors

Let us consider an exmaple:
1. Multilevel Inheritance – Order of Constructor invocation
class A
{
public:
A()
{
cout<<“Constructor of base class Invoked “;
}
}
class B: public A
{
public:
B()
{
cout<<“Constructor of derived class Invoked “;
}
}
main()
{
B objB;
}

The questions is wether the constructor of base class will be invoked first or derived class first?
Obviously the base class parameters has to be arranged and organised first, so the base class constructor will be invoked before invoking the constructor of derived class.

2. Mulitple constructors in base class and derived class
class A
{
public:
A()
{
cout<<“Constructor of base class Invoked “;
}
A(int x)
{
cout<<“Argument Constructor of base class Invoked “;
}
}
class B: public A
{
public:
B()
{
cout<<“Constructor of derived class Invoked “;
}
B(int x)
{
cout<<“Argument Constructor of derived class Invoked “;
}
}
main()
{
B objB(5);
}
In this case our questions is , wether the argument constructor of base class will be invoked or not. It has to be understood that the argument constructor of base class is not invoked if it is not explicitly specified as:
B(int x):A(x)
{
cout<<“Argument Constructor of derived class Invoked “;
}