السلام عليكم ....
أنا محتاجة لمساعدو وارجو ان اجدها هنا .. لا ادري ان كان مكان الموضوع مناسب ام لا ؟؟
انا لدي برنامج شات بين كلينت وسيرفر المطلوب هو استخدام تقنية التوقيع الالكتروني بينهم لجعل الاتصال امنا ....
كيف يمكنني عمل شيئ مثل هذا وساضع الكود لتقريب المفهوم اكثر
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.*;
/* class to read a string from the keyboard*/
class MyInput
{
/*readString method*/
public static String readString()
{
//Declare buffer to hold the string
BufferedReader br
= new BufferedReader(new InputStreamReader(System.in), 1);
// Declare and initialize the string
String string = " ";
//Get the string from the keyboard
try
{ //begin of try block
string = br.readLine(); //read the string
} //end of try block
catch (IOException ex)
{ //begin of catch block
System.out.println(ex); //print error msg
} //end of catch block
// Return the string obtained from the keyboard
return string;
}
//end of method readstring
} //end of class MyInput
public class Client extends JFrame implements ActionListener
{
private JTextField t=new JTextField(10);
//create textfield with lenght 10
private JTextArea a= new JTextArea(20,40);
//create textarea with row 20 and column 40
private JButton b= new JButton("send");
//create button send
private JButton b1=new JButton("Exit");
//create button exit
String data;
//Declare the string to be sent
String st;
BufferedReader in;
//for read and resive
PrintWriter out;
//for write and send
Container pp=getContentPane();
Client()
{
super("Client");// title of the frame
JPanel p= new JPanel();
//create panel p to hold text area
p.setLayout(new BorderLayout());
//to arrange componant in the panel
p.add(a,BorderLayout.CENTER);
//add textarea in the panel and arrange it in
//the center
JPanel p1= new JPanel();
//add panel p1 to hold buttons, textfield and
//label
p1.setLayout(new GridLayout(2,1));
//layout to arrange componant
p1.add(new Label("Enter Message"));
//creat and add label to the panel p1
p1.add(t);
//add text field to the panel p1
p1.add(b,BorderLayout.SOUTH);
//add button to the panel
p1.add(b1,BorderLayout.SOUTH );
//add button to the panel
pp.add(p,BorderLayout.CENTER);
//add panel p to the panel pp
pp.add(p1,BorderLayout.SOUTH);
//add panel p1 to the panel pp
b.addActionListener(this);
//add addActionListener(this) to make event
b1.addActionListener(this);
//add addActionListener(this) to make event
}
public void actionPerformed(ActionEvent event)
{
//cheak if it button send
if(event.getSource()==b)
{
out.println(t.getText());
//write in the file(buffer)
a.append("client says : "+t.getText()+"\n");
//display what client say in the text area
}
//cheak if it button exit
if(event.getSource()==b1)
{
System.exit(0);//exit
}
}
public static void main(String args[])
{
Client d=new Client();
d.pack();//for sutible size to display
d.setVisible(true); //to be display
try { //begin of try block
//create asocket to connect to the server
Socket skt = new Socket("localhost", 1234);
//create abuffer input stream to recieve data from the server
d.in= new BufferedReader(new InputStreamReader(skt.getInputStream()));
//create abuffer output stream to send data to the server
d.out = new PrintWriter(skt.getOutputStream(),true);
//continuous send and recieve data from the server
while(true)
{
d.st=d.in.readLine(); //create string and read aline
d.a.append("Server says : "+d.st+"\n");
}//end of while loop
} //end of try block
//catch exception block
catch(Exception e) {
System.out.print("connection failed.. try again!\n"); //output error msg to screen
} //end of catch block
}
}import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;
/* class to read a string from the keyboard*/
class MyInput
{
/*readString method*/
public static String readString()
{
//Declare buffer to hold the string
BufferedReader br
= new BufferedReader(new InputStreamReader(System.in), 1);
// Declare and initialize the string
String string = " ";
//Get the string from the keyboard
try
{ //begin of try block
string = br.readLine(); //read the string
} //end of try block
catch (IOException ex)
{ //begin of catch block
System.out.println(ex); //print error msg
} //end of catch block
// Return the string obtained from the keyboard
return string;
}
//end of method readstring
} //end of class MyInput
public class Server extends JFrame implements ActionListener
{
private JTextField t=new JTextField(10);
//create textfield with lenght 10
private JTextArea a= new JTextArea(20,40);
//create textarea with row 20 and column 40
private JButton b= new JButton("send");
//create button send
private JButton b1=new JButton("Exit");
//create button exit
String data;
//Declare the string to be sent
String st;
BufferedReader in;
//buferr for read and resive
PrintWriter out;
//buferr use for write and send
//
Container pp=getContentPane();
//container
Server()//constructor
{//begin block
super("server");// title of the frame
JPanel p= new JPanel();
//create panel p to hold text area
p.setLayout(new BorderLayout());
//to arrange componant in the panel
p.add(a,BorderLayout.CENTER);
//add textarea in the panel and arrange it in
//the center
JPanel p1= new JPanel();
//add panel p1 to hold buttons, textfield and
//label
p1.setLayout(new GridLayout(2,1));
//layout to arrange componant
p1.add(new Label("Enter Message"));
//creat and add label to the panel p1
p1.add(t);
//add text field to the panel p1
p1.add(b,BorderLayout.SOUTH);
//add button to the panel
p1.add(b1,BorderLayout.SOUTH );
//add button to the panel
pp.add(p,BorderLayout.CENTER);
//add panel p to the panel pp
pp.add(p1,BorderLayout.SOUTH);
//add panel p1 to the panel pp
b.addActionListener(this);
//add addActionListener(this) to make event
b1.addActionListener(this);
//add addActionListener(this) to make event
}//end block
public void actionPerformed(ActionEvent event)
{
//cheak if it button send
if(event.getSource()==b)
{
out.println(t.getText());
//write in the file(buffer)
a.append("Server says : "+t.getText()+"\n");
//display what server say in the text area
}
//cheak if it button exit
if(event.getSource()==b1)
{
System.exit(0);//exit
}
}
public static void main(String args[])
{
Server d=new Server();
d.pack();//for sutible size to display
d.setVisible(true);//to be display
try { //begin of try block
ServerSocket srvr = new ServerSocket(1234); //create aserver socket
//start listening for connections on the server socket
Socket skt = srvr.accept();
//create abuffer reader stream to get data from the client
d.in= new BufferedReader(new InputStreamReader(skt.getInputStream()));
//create buffer writer stream to send data to the client
d.out = new PrintWriter(skt.getOutputStream(),true);
//continuous read and send data from the client
while(true)
{
//create string and read aline
d.st=d.in.readLine();
d.a.append("client says : "+d.st+"\n");
}//end of while loop
} //end of try block
//catch exception block
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n"); //output error msg to screen
} //end of catch block
}
}أتمنى ان تساعدوني باقرب فرصة ممكنة ....