logo

آلة حاسبة بسيطة باستخدام TCP في جافا

المتطلب السابق: برمجة المقبس في جافا لا تنتهي الشبكات باتصال أحادي الاتجاه بين العميل والخادم. على سبيل المثال، خذ بعين الاعتبار خادم معرفة الوقت الذي يستمع إلى طلب العملاء ويستجيب بالوقت الحالي للعميل. عادةً ما تتبع تطبيقات الوقت الفعلي نموذج الطلب والاستجابة للاتصال. يرسل العميل عادةً كائن الطلب إلى الخادم والذي بعد معالجة الطلب يرسل الاستجابة مرة أخرى إلى العميل. بعبارات بسيطة، يطلب العميل موردًا معينًا متاحًا على الخادم ويستجيب الخادم له إذا كان يمكنه التحقق من الطلب. على سبيل المثال، عند الضغط على زر الإدخال بعد إدخال عنوان URL المطلوب، يتم إرسال طلب إلى الخادم المقابل والذي يقوم بعد ذلك بالرد عن طريق إرسال الاستجابة في شكل صفحة ويب تكون المتصفحات قادرة على عرضها. في هذه المقالة، يتم تطبيق تطبيق آلة حاسبة بسيط حيث يرسل العميل طلبات إلى الخادم في شكل معادلات حسابية بسيطة وسيقوم الخادم بالرد بالإجابة على المعادلة.

البرمجة من جانب العميل

الخطوات المتبعة من جانب العميل هي كما يلي-
  1. افتح اتصال المقبس
  2. تواصل:في جزء الاتصالات هناك تغيير طفيف. يكمن الاختلاف مع المقالة السابقة في استخدام كل من تدفقات الإدخال والإخراج لإرسال المعادلات واستقبال النتائج من وإلى الخادم على التوالي. DataInputStream و DataOutputStream يتم استخدامها بدلاً من InputStream وOutputStream الأساسيين لجعلها مستقلة عن الآلة. يتم استخدام المنشئات التالية -
      DataInputStream العام (InputStream in)
        Syntax:   public DataInputStream(InputStream in)   Parameters:   in - The underlying InputStream. Creates a DataInputStream that uses the specified underlying InputStream.
      DataOutputStream العامة (InputStream in)
        Syntax:   public DataOutputStream(OutputStream out)   Parameters:   out - The underlying OutputStream. Creates a DataOutputStream that uses the specified underlying OutputStream.
    بعد إنشاء تدفقات الإدخال والإخراج، نستخدم readUTF وwriteUTF لطرق التدفقات التي تم إنشاؤها لتلقي الرسالة وإرسالها على التوالي.
      السلسلة النهائية العامة readUTF() تطرح IOException
      Reads the string encoded using UTF8 encoding.   Throws:   IOException - the stream has been closed and the contained input stream does not support reading after close or another I/O error occurs 
      السلسلة النهائية العامة writeUTF() تطرح IOException
      Writes the string encoded using UTF8 encoding.   Throws:   IOException - the stream has been closed and the contained input stream does not support reading after close or another I/O error occurs 
  3. إغلاق الاتصال.

التنفيذ من جانب العميل

Java
// Java program to illustrate Client Side Programming // for Simple Calculator using TCP import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Calc_Client {  public static void main(String[] args) throws IOException  {  InetAddress ip = InetAddress.getLocalHost();  int port = 4444;  Scanner sc = new Scanner(System.in);  // Step 1: Open the socket connection.  Socket s = new Socket(ip port);  // Step 2: Communication-get the input and output stream  DataInputStream dis = new DataInputStream(s.getInputStream());  DataOutputStream dos = new DataOutputStream(s.getOutputStream());  while (true)  {  // Enter the equation in the form-  // 'operand1 operation operand2'  System.out.print('Enter the equation in the form: ');  System.out.println(''operand operator operand'');  String inp = sc.nextLine();  if (inp.equals('bye'))  break;  // send the equation to server  dos.writeUTF(inp);  // wait till request is processed and sent back to client  String ans = dis.readUTF();  System.out.println('Answer=' + ans);  }  } } 
الإخراج
Enter the equation in the form: 'operand operator operand' 5 * 6 Answer=30 Enter the equation in the form: 'operand operator operand' 5 + 6 Answer=11 Enter the equation in the form: 'operand operator operand' 9 / 3 Answer=3 

البرمجة من جانب الخادم



الخطوات المتبعة من جانب الخادم هي كما يلي-
  1. إنشاء اتصال مأخذ التوصيل.
  2. معالجة المعادلات القادمة من العميل:في جانب الخادم أيضًا نفتح كلا من inputStream وoutputStream. بعد استلام المعادلة نقوم بمعالجتها وإرجاع النتيجة مرة أخرى إلى العميل عن طريق الكتابة على مخرجات المقبس.
  3. أغلق الاتصال.

التنفيذ من جانب الخادم

مثال على القائمة في جافا
Java
// Java program to illustrate Server Side Programming // for Simple Calculator using TCP import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.StringTokenizer; public class Calc_Server {  public static void main(String args[]) throws IOException  {  // Step 1: Establish the socket connection.  ServerSocket ss = new ServerSocket(4444);  Socket s = ss.accept();  // Step 2: Processing the request.  DataInputStream dis = new DataInputStream(s.getInputStream());  DataOutputStream dos = new DataOutputStream(s.getOutputStream());  while (true)  {  // wait for input  String input = dis.readUTF();  if(input.equals('bye'))  break;  System.out.println('Equation received:-' + input);  int result;  // Use StringTokenizer to break the equation into operand and  // operation  StringTokenizer st = new StringTokenizer(input);  int oprnd1 = Integer.parseInt(st.nextToken());  String operation = st.nextToken();  int oprnd2 = Integer.parseInt(st.nextToken());  // perform the required operation.  if (operation.equals('+'))  {  result = oprnd1 + oprnd2;  }  else if (operation.equals('-'))  {  result = oprnd1 - oprnd2;  }  else if (operation.equals('*'))  {  result = oprnd1 * oprnd2;  }  else  {  result = oprnd1 / oprnd2;  }  System.out.println('Sending the result...');  // send the result back to the client.  dos.writeUTF(Integer.toString(result));  }  } } 
الإخراج:
Equation received:-5 * 6 Sending the result... Equation received:-5 + 6 Sending the result... Equation received:-9 / 3 Sending the result... 
Note: In order to test the above programs on the system please make sure that you run the server program first and then the client one. Make sure you are in the client console and from there enter the equation in the format-'operand1 operator operand2' and press Enter. Answer to the requested equation will be shown in the client console only. Finally to terminate the communication type 'bye' (without quotes) and hit enter. مقالة ذات صلة: آلة حاسبة بسيطة باستخدام UDP في جافا إنشاء اختبار