السلام عليكم ورحمة الله تعالي وبركاتة
ماذا اذا كنت في حاجه الي حلول لامر استعصي عليك
قدم المعلومة واحصل علي الحلول
هذا هو العرض الاول
برمجة ال Client Server في لغة الجافا تعد من الامور السهلة والمبسطة في بدايتها ولكن سرعان ما يتعقد الامر
لدينا كود يقوم بالاتي :
من قبل ServerThread
- فتح منفذ port علي الجهاز في انتظار اتصال من ال Clients
- يستلم رسالة نصية بعد قبول الاتصال ويفتح قناة اتصال لنفس Client الذي ارسل الرسالة
- يدعم ال MultiClientlk
وهذا هو الكود البرمجي :
package client_server;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public static void main(String arg[]){
try{
int clientid=1;
ServerSocket ss =new ServerSocket(7000,10);
while(true){
Socket s=ss.accept();
System.out.println("conection stablish from clinet id:"+clientid);
InetAddress clientaddress=s.getInetAddress();
System.out.println("client ip address : "+clientaddress.getHostAddress());
clientthread ct=new clientthread(s,clientid);
ct.start();
clientid++;
}}
catch(IOException e){
}}}
class clientthread extends Thread{
Scanner in=new Scanner(System.in);
Socket s;
int clientid;
public clientthread(Socket s1,int id){
s=s1;
clientid=id;
}
@Override
public void run(){
try{
DataInputStream fromclient=new DataInputStream(s.getInputStream());
DataOutputStream toclient=new DataOutputStream(s.getOutputStream());
while(true){
String num=fromclient.readUTF();
System.out.println("Student "+num+"\n");
System.out.println("write number to send to client ip :"+s.getInetAddress()+" :\n" );
String numroot=in.nextLine();
toclient.writeUTF(numroot);
toclient.flush();
System.out.print("\n");
}}
catch(IOException e){
}}}من قبل ال Client
بعد عملية الاتصال
يقوم باستلام الرسالة من الserver
ثم يقوم بقوم بعدة عمليات حيث يقوم بمطابقة الرسالة مع قيم بعض المتغيرات المعرفه في الكلاس مثلا اذا قام السيرفر بارسال الرسالة shutdown فبعد عملية المقارنة نجد ان النص يعني تنفيذ الدالة SYScontrol.shutdown() التي تقوم باغلاق الجهاز فوريا
كود
String sr=fromserver.readUTF();
int result = sr.compareTo( str2 );
if(result==0){
SYScontrol.shutdown();
}حيث ان قيمة المتغير str2="shutdown"
,وهكذا طريقة التحكم في اكواد البرنامج عن طريق المطابقة النصية للاوامر المرسلة من السيرفر
يتم التحكم في الجهاز بارسال الاوامر من الكلاس SYScontrol عبر تنفيذ اجراء محدد بالدالة التالية
Process p=Runtime.getRuntime().exec("cmd /c shutdown -s -t 90");مثلا الدالة shutdwon هي من class SYScontrol اقوم بتنفيذ امر الاغلاق علي الجهاز
كود :
//this Function to Shutdon Client pc
public static void shutdown() throws IOException, InterruptedException{
Process p=Runtime.getRuntime().exec("cmd /c shutdown -s -t 9000");
p.waitFor();
}حيث تقوم باغلق الجهاز بعد 15 دقيقة
وايضا اخذ صورة من الجهاز تتم بالصورة التالية :
- يقوم السيرفر بارسال رسال نصية تحتوي علي الامر ScreenCaptur
- وكما ذكرنا سابقا يحلل الclient الرساله ويجد انها تتطابق قيمة المتغير str3
- نتيجة للمطابقة يستدعي الclient الدالة CapturScrean
- تقوم الداه باخذ صوره من الشاشة وحفظها في الجهاز
كود :
public static void ScreanCaptur() throws AWTException, UnsupportedFlavorException, IOException{
try {
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_PRINTSCREEN);
robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
robot.keyRelease(KeyEvent.VK_ALT);
try {
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
RenderedImage image = (RenderedImage) t.getTransferData(DataFlavor.imageFlavor);
boolean isSuccess = ImageIO.write(image, "png", new File("altScreen.png"));ملاحظة :
يتم اخذ الصورة للنافذة النشطة فقط في هذا الكود
ويمكنك اخذ صورة لكامل الشاشه بحذف الاتي :
robot.keyPress(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_ALT);حيث يقوم بالضغط علي المفتاح PRINTSCREEN فقط
يتم حفظ الصورة في مسار البرنامج باسم altScreen وياخذ المتغير المنظقي isSuccess القيمة true الذي يستخدم لمعرفة نجاح العملية لاستخدامة في ما بعد
وهذا هو الكلاس client
كود:
//mosap hommeda
/* this code write by csa6 Group
*
*/
public class Client {
public static void main(String arg[]) throws AWTException, UnsupportedFlavorException, InterruptedException{
try{
String Nikname,str2="shutdown",str3="ScreenCaptur";
Socket s=new Socket("localhost",7000);
PrintWriter out = new PrintWriter( new BufferedWriter(new OutputStreamWriter (s.getOutputStream() ) ), true);
Scanner n=new Scanner(System.in);
SYScontrol SYS=new SYScontrol();
DataInputStream fromserver=new DataInputStream(s.getInputStream());
DataOutputStream toserver=new DataOutputStream(s.getOutputStream());
System.out.println("please Enter Your Name\n");
Nikname=n.nextLine();
for(int i=0;i<10;i++){
System.out.print("\n Write Message here to send to Tachear\n");
String QU=n.nextLine();
toserver.writeUTF(Nikname+" :"+QU);
toserver.flush();
String sr=fromserver.readUTF();
int result = sr.compareTo( str2 );
if(result==0){
SYScontrol.shutdown();
}else {
result =sr.compareTo( str3 );
if(result==0){
SYScontrol.ScreanCaptur();
}
System.out.println("\n teacher send = : "+sr+"\n");
}
}
}
catch(IOException e){
}}
}وهذا هو كلاس SYScontrol
كود :
import java.awt.AWTException
public class SYScontrol {
//this Function to Shutdon Client pc
public static void shutdown() throws IOException, InterruptedException{
Process p=Runtime.getRuntime().exec("cmd /c shutdown -s -t 9000");
p.waitFor();
}
//this Function to Restart Client pc
public static void restart() throws IOException, InterruptedException{
Process p=Runtime.getRuntime().exec("cmd /c shutdown -r -t 9000");
p.waitFor();
}
//this Function to Captur Photo from Screan Client pc
public static void ScreanCaptur() throws AWTException, UnsupportedFlavorException, IOException{
try {
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_PRINTSCREEN);
robot.keyRelease(KeyEvent.VK_PRINTSCREEN);
robot.keyRelease(KeyEvent.VK_ALT);
try {
Thread.sleep(1000 * 2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
RenderedImage image = (RenderedImage) t.getTransferData(DataFlavor.imageFlavor);
boolean isSuccess = ImageIO.write(image, "png", new File("altScreen.png"));
JFrame frame = new JFrame("Display image");
Panel panel = new VeidoShow();
frame.getContentPane().add(panel);
frame.setSize(1366,768);
frame.setVisible(true);
System.out.println(isSuccess);
}}
class VeidoShow extends Panel {
BufferedImage image;
public VeidoShow() {
try {
//System.out.println("Enter image name\n");
BufferedReader bf=new BufferedReader(new
InputStreamReader(System.in));
//String imageName=bf.readLine();
File input = new File("altScreen.png");
image = ImageIO.read(input);
} catch (IOException ie) {
System.out.println("Error:"+ie.getMessage());
}
}
@Override
public void paint(Graphics g) {
g.drawImage( image, 0, 0, null);
}
يتم عرض الصورة في جهاز الclient وهو ليس المطلوب ولكن تم ذالك لمحاولة توضيع عمل الدالة
من خلال هذا المشروع ارغب في عمل نظام يستطيع التحكم الكامل بالجهاز .... ليس جهاز واحد وانما عدة اجهزة ..
قمت بكتابة العديد من الاكواد الاخري مثل استطاعة السيرفر ارسال واستقبال الملفات الي الاجهزة عن طريق برتكول الUDP واكواد اخري عديدة سوف اعرضها عند الحاجة اليها
ما اود معرفتة هو هل يبدو هذا المشروع في خط السير الصحيح مع العلم انني قمت بعملية تحليل كامل للنظام ووجد الاشادة من بعض ذوي الخبرة والان ارغب في جمع جميع الاكواد اللازمة قبل البدء في التصميم بالواجهات الرسومية
إن شاء الله بعد عرض البداية اليكم سوف اقوم بإنزال الاكواد الاخري للتحكم في الجهاز وانتظار ارائكم المهمة جدا بالنسبة لي
في انتظار ارائكم وتوجيهاتكم ايها الاعضاء الكرام
حاولت تبسيط فكرة الموضوع والاكواد بقدر الامكان حتي تصل الفكرة الي أعضاء المنتدي الكرام
اعتذر عن اخطاء قد اقع بها فانا لم اعتاد بعد علي الكتابة في هذا المنتدي ......
ولكم مني جزيل الشكر مقدما ...