Komunikator TCP - problem

0

Mam taki komunikator składający się z 2 aplikacji, klienta i serwera:

Klient:

import java.net.*;
import java.io.*;

class Nasluch extends Thread
{
   Socket socket = null;
   String message;
   BufferedReader socketbr;
   
   public Nasluch(Socket skt) throws IOException                                   
   {                                                                               
      socket=skt;                                                                  
      socketbr=new BufferedReader(new InputStreamReader(socket.getInputStream())); 
   }                                                                               
   
   public void run()                              
   {                                              
      try                                         
      {  
      	 message = "elo";
      	 while(true){
      	 	if (message.equalsIgnoreCase("zegnaj")){
      	 		break;
      	 	}
      	 	message = socketbr.readLine();
	        System.out.println(socketbr.readLine()); 
	        continue;
      	 }                                        
         socketbr.close();                        
         socket.close();                          
         System.exit(0);                          
      }                                           
      catch(IOException e){
      	System.exit(1);     
      }  
      catch(NullPointerException e){
      	System.exit(1);     
      }
   }                                              
}


public class Klient
{
   public static final int PORT=50000;
   public static final String HOST = "192.168.72.1";
   
   public static void main(String[] args) throws IOException                    
   {                                                                            
      Socket skt=null;                                                          
                                                                                
      try                                                                       
      {                                                                         
         skt=new Socket(HOST,PORT);                                             
      }                                                                         
                                                                                
      catch(UnknownHostException e)                                             
      {                                                                         
         System.err.println("nieznany host: "+HOST);                            
         System.exit(1);                                                        
      }                                                                         
                                                                                
      System.out.println("nawiazalem polaczenie: "+skt);                        
                                                                                
      new Nasluch(skt).start();                                                 
                                                                                
      PrintWriter out=new PrintWriter(skt.getOutputStream());                   
      BufferedReader klaw=new BufferedReader(new InputStreamReader(System.in)); 
                                                                                
      String str;
      while(true){	
      	str =klaw.readLine();                                               
      	if (str.equalsIgnoreCase("zegnaj")){
      		break;
      	}
      	out.println(str);                                                         
      	out.flush();                              
        continue;                                
      }                                                                                
      out.close();                                                              
      klaw.close();                                                             
      skt.close();                                                              
   }                                                                            
}

Serwer:

import java.net.*;
import java.io.*;

class Nasluch extends Thread
{
   Socket socket;
   String message;
   BufferedReader socketbr;
   
   public Nasluch(Socket skt) throws IOException                              
   {                                                                               
      socket=skt;                                                                  
      socketbr=new BufferedReader(new InputStreamReader(socket.getInputStream())); 
   }                                                                               
   
   public void run()                              
   {                                              
      try                                         
      {  
      	 message = "";
      	 while(true){
      	 	if(message.equalsIgnoreCase("zegnaj")){
      	 		break;
      	 	}
      	 	message = socketbr.readLine();
	        System.out.println(message); 
	        continue;
      	 }                                        
         socketbr.close();                        
         socket.close();                          
         System.exit(0);                          
      }                                           
      catch(IOException e){
		System.exit(1);    	
      	}         
      catch(NullPointerException e){                      
      	System.exit(1);    
      }             
   }                                              
}

public class Serwer
{
   public static final int PORT=50000;
   
   public static void main(String[] args) throws IOException                    
   {                                                                            
      ServerSocket s=new ServerSocket(PORT);                                    
      System.out.println("nasluchuje: "+s);                                     
                                                                                
      Socket skt=s.accept();                                                    
      System.out.println("jest polaczenie: "+skt);                                                                               
                                                                                
      new Nasluch(skt).start();                                                 
                                                                                
      PrintWriter out=new PrintWriter(skt.getOutputStream());                   
      BufferedReader klaw=new BufferedReader(new InputStreamReader(System.in)); 
      String str = "elo";
                                                                                
      while(true){	
  	    str =klaw.readLine();                                               
  	    if(str.equalsIgnoreCase("zegnaj")){
  	    	break;
  	    }
    	out.println(str);                                                         
      	out.flush();                                                              
        continue;
      }
      out.close();                                                              
      klaw.close();                                                             
      skt.close();             
      s.close();                                                 
   }                                                                            
}

Gdy wysyłam wiadomość przy pomocy klienta to serwer ją wyświetla bez problemu. Problem natomiast jest w drugą, stronę ponieważ klientowi zdarza się dość często nie wyświetlać wiadomości wysłanych przez serwer. Gdzie tkwi błąd?

Pozdrawiam,

0

Raczej nit nie będzie miał ochoty na czytanie Twojego kodu. Po co pisać wszystko od początku skoro w internecie można znaleźć dobry tutorial.

0

Dostałem takie zadanie, a nie umiem znaleźć tego błędu

0

Jak nie umiesz znaleźć błędu, to może szybciej będzie napisać od podstaw na podstawie chociażby linku, który Ci już podano, poza tym w sieci znajdziesz przykładowe implementacje komunikatorów w Java.

1 użytkowników online, w tym zalogowanych: 0, gości: 1