import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class KServer extends Frame
{
static Frame f;
public static String LocalIP="";
Label lblServIP=new Label("Server IP Address & Port ID ");
Label lblOne = new Label("Logged Client IP-Addresses");
Label lblTwo = new Label("R.S.A Public Keys");
public static List lstClientIP=new List();
public static List lstRSAPubKey=new List();
public static List lstNval=new List();
public static TextField txtServIP=new TextField("127.0.0.1");
public static DatagramSocket ds;
public static int buffer_size=1024;
public static DatagramPacket p;
public static byte buffer[]=new byte[buffer_size];
//---------------------------------------------------------------------------------
public KServer()
{
setLayout(new FlowLayout());
addWindowListener(new WindowEventListener());
add(lblServIP);
add(lblOne);
add(lblTwo);
add(lstClientIP);
add(lstRSAPubKey);
add(lstNval);
add(txtServIP);
lblServIP .setBounds(18, 69 , 175, 19);
txtServIP .setBounds(195, 67 , 88 , 22);
lstClientIP .setBounds(10 , 147, 121, 251);
lstRSAPubKey .setBounds(132, 147, 150, 251);
lstNval .setBounds(302, 147, 70 ,251);
try{
InetAddress in=InetAddress.getLocalHost();
txtServIP.setText(in.getHostAddress());
LocalIP=txtServIP.getText();
}catch(UnknownHostException e){}
}
//---------------------------------------------------------------------------------
public static void Send(String Text,String IP_addr,int PortID) //Server_IP,Server_Port
{
try {
InetAddress iaddress = InetAddress.getByName(IP_addr);
int Len = Text.length();
byte[] message = new byte[Len];
Text.getBytes(0, Len, message, 0); // Initilize the packet with data and address
DatagramPacket packet = new DatagramPacket(message, Len, iaddress, PortID);
DatagramSocket socket = new DatagramSocket();// Create a socket, and send the packet through it.
socket.send(packet);
}catch(Exception e){};
}
//---------------------------------------------------------------------------------
public static void SendConnectedUsersInfo() //Sending the IP-Addresses of Users who are connected to the server
{
int i,j;
for(i=0;i<lstClientIP.countItems();i++)
{
Send("l ",lstClientIP.getItem(i),666); //Asking Client to clear the list
for(j=0;j<lstClientIP.countItems();j++)
{
Send("L "+lstClientIP.getItem(j),lstClientIP.getItem(i),666); //Add Data to List
}
}
}
//---------------------------------------------------------------------------------
public static void SendPubKey(String Sender,String Receiver)
{
int i;
for(i=0;i<lstClientIP.countItems();i++)
{
if(Receiver.equals(lstClientIP.getItem(i)))
{
System.out.print("["+Receiver+"]");
System.out.println("<"+lstClientIP.getItem(i)+">");
Send("K "+lstRSAPubKey.getItem(i),Sender,666);
Send("n "+lstNval.getItem(i),Sender,666);
return;
}
}
}
//---------------------------------------------------------------------------------
public static void CLogout(String msg)
{
int i;
for(i=0;i<lstClientIP.countItems();i++)
{
if(lstClientIP.getItem(i).equals(msg))
{
lstClientIP.remove(i);
lstRSAPubKey.remove(i);
lstNval.remove(i);
}
}
SendConnectedUsersInfo();
}
//---------------------------------------------------------------------------------
class WindowEventListener extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
//---------------------------------------------------------------------------------
public static void Receive() throws Exception //Receiving data from Clients
{
int k=0,l=0;
String msg,msg1,Nonce="";
String SenderIP="",ReceiverIP="";
int D=0;
int c_Port=666;
char Cmsg[]=new char[25];
while(true)
{
p = new DatagramPacket(buffer,buffer.length);
ds.receive(p);
msg=(new String(p.getData(),0,p.getLength()));
msg1=msg.substring(2);
Cmsg=msg.toCharArray();
System.out.println(">>"+msg);
switch(Cmsg[0])
{
case 'D': CLogout(msg1); SendConnectedUsersInfo(); break; //Client Request for DISCONNECT
case 'C': System.out.println(msg1); lstClientIP.add(msg1);
SendConnectedUsersInfo(); break; //Client Reaquest for CONNECT
case 'c': System.out.println(msg1); lstRSAPubKey.add(msg1); break;//Receiving RSA Public Key of Newly Connected Client
case 'n': System.out.println(msg1); lstNval.add(msg1); break;
case 'k': SenderIP=msg1; break;
case 'K': ReceiverIP=msg1; SendPubKey(SenderIP,ReceiverIP); break;
} //switch statement ends here
} //while ends here
}
/*-------------------------------------------------------*/
public static void main(String args[]) throws Exception
{
f=new KServer();
f.setTitle("\"D I G I T A L E N V E L O P E\" S E R V E R");//Setting Form Title
f.setLocation(240,60);
f.setSize(390,460);
f.setLayout(null); //Setting Layout of Frame
f.setVisible(true);
int LocalPort=777;
ds=new DatagramSocket(LocalPort);
Receive();
}
}