اذهب إلى المحتوى
  • 0

مشكلة في Client-Server بلغة جافا

احمد صوالحة

السؤال

لدي برنامج مكون من قسمين:

  • قسم الـserver : يقوم بإستقبال عدد ما من الـclient ثم يعيد له مربع العدد.
  • قسم الـclient يقوم بإرسال عدد ما إلى الـserver ويستقبل النتيجة.

المشكلة هي أن السيرفر يستقبل العدد مرة واحدة وبعد ذلك لا يستقبل أي شيء!

الكود الخاص بالـclient:

public class TestClient{
     public static void main(String args[]){
          try{
           String host=""+args[0];
           int port=Integer.parseInt(args[1]);
          BufferedReader input=new BufferedReader(new InputStreamReader(System.in));          
          String lettura=input.readLine();
          while(!lettura.equals("good")){
          Socket s=new Socket(host,port);
          PrintWriter output=new PrintWriter(s.getOutputStream());   
          output.println(lettura);
          output.flush();
          BufferedReader insok = new BufferedReader(new InputStreamReader(s.getInputStream()));    
          String lettsock=insok.readLine(); 
          System.out.println(lettsock);
          lettura=input.readLine();       
         }
          input.close();  
       }
         catch(Exception ex){
            System.out.println(ex);
            System.exit(3);
         }  
    }
} 

الكود الخاص بالـserver:

public class TestServer{
   static final int PORT = 1024;
    public static void main(String args[]){
     try{
         ServerSocket ss=new ServerSocket(PORT);
          while(true) {
               Socket ns = ss.accept();
                BufferedReader networkIn = new BufferedReader(new InputStreamReader(ns.getInputStream()));
                PrintWriter networkOut=new PrintWriter(ns.getOutputStream()); 
                String line;
                while ((line = networkIn.readLine()) != null) {
                  System.out.println(": " + line);
                  try{
                     int number=Integer.parseInt(line);
                     number=number*number;
                     networkOut.println(number);
                     networkOut.flush();
                   }
                  catch(Exception ex){
                  } 
                }
              ns.close();
            }
     }
    catch(Exception ex){
        System.out.println(ex);
        System.exit(3);
     }
   }
}

 

تم التعديل في بواسطة احمد صوالحة
رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

المشكلة تحدث بسبب كتابتك للأكواد الخاصة بإتصال الـclient في داخل الـloop وبعد ذلك لم تقم بإغلاق الإتصال من خلال close.

لحل المشكلة يرجى نقل الكود التالي في قسم الـclient إلى خارج الـwhile loop:

Socket s=new Socket(host,port);
PrintWriter output=new PrintWriter(s.getOutputStream()); 
BufferedReader insok = new BufferedReader(new InputStreamReader(s.getInputStream())); 

ليكون الكود الكامل لقسم الـclient كالتالي:

public class TestClient{
     public static void main(String args[]){
          try{
           String host=""+args[0];
           int port=Integer.parseInt(args[1]);
           BufferedReader input=new BufferedReader(new InputStreamReader(System.in));          
           String lettura=input.readLine();
           Socket s=new Socket(host,port);
           PrintWriter output=new PrintWriter(s.getOutputStream());   
		   BufferedReader insok = new BufferedReader(new InputStreamReader(s.getInputStream()));    
          while(!lettura.equals("good")){
          output.println(lettura);
          output.flush();
          String lettsock=insok.readLine(); 
          System.out.println(lettsock);
          lettura=input.readLine();       
         }
          input.close();  
       }
         catch(Exception ex){
            System.out.println(ex);
            System.exit(3);
         }  
    }
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...