0 Replies - 282 Views - Last Post: 29 November 2017 - 01:49 PM Rate Topic: -----

#1 tibou   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-November 17

code/Java

Posted 29 November 2017 - 01:49 PM

Hello, I'm working on an internet application project in Java. it is an advertisement application (Shop, The server sends to the client all the classifieds received.
as much as possible, only send announcements from connected clients and send
not the obsolete ads. The customer chooses a small advert (s) from
proposed and enters into direct communication with the proposing user
this advert.
I would like some help
my problem is that the client does not receive a response from the server.
here is the code:
//////////////////////////////////////////////////////////////
Server:

import java.net.ServerSocket;  
import java.net.Socket;  
  
public class Serveurclient {  
    public static void main(String[] args) throws Exception{  
          
        ServerSocket server = new ServerSocket(2050);  
        Socket client = null;  
        boolean flag = true;  
        while(flag){  
              
            client = server.accept();  
            new Thread(new EchoThread(client) ).start(); 
               
        }  
        server.close();  
    }  
} 

//////////////////////////////////////////
Client
import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.io.PrintStream;  
import java.net.Socket;   
  
public class ClientServeur {  
    public static void main(String[] args) throws IOException {  
               
            Socket client = new Socket("172.28.175.160", 2050);   
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));                
            PrintStream out = new PrintStream(client.getOutputStream());                
            BufferedReader buf =  new BufferedReader(new InputStreamReader(client.getInputStream()));  
            new Thread(new ClientRecois(buf)).start(); 
            boolean flag = true;  
            while(flag){  
	                  
	                String s = input.readLine();  	                   
	                out.println(s); 
			
	                
            }
            input.close();
            out.close();
 	    	client.close(); 
	}
}  

//////////////////
ClientRecoi

import java.io.BufferedReader;
import java.io.IOException;
    
public class ClientRecois implements Runnable {   
	
    BufferedReader buf;   
    public ClientRecois(BufferedReader buf){  
         this.buf = buf;
    }  
      
    
    public void run() {              
            boolean flag =true;
            while(flag){  

            	String str;
				try {
					str = buf.readLine();
					System.out.println(str); 
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
            	 					
            }
    } 
}

////////////////////////////////////
 EchoThread

import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.io.PrintStream;  
import java.net.Socket;
  
  
public class EchoThread implements Runnable {  
  
    private Socket client = null;   
    
    public EchoThread(Socket client){  
        this.client = client;  
    }       
    
     
    public void run() {  
        try{  
            PrintStream out = new PrintStream(client.getOutputStream());              
            BufferedReader buf = new BufferedReader(new InputStreamReader(client.getInputStream()));  
            Utilisateur utilisateur = new Utilisateur();
            
            boolean flag1 =true;
            boolean flag2 =true;
            while(flag1){  
                  
                String str =  buf.readLine();
                String[] strs = str.split("\\ ");

            	if(strs[0].equals("I'm")) {
            		String username = strs[1];
            		utilisateur = newUtilisateur(username);
            		System.out.println(" connected success "+ strs[1]);
            		while(flag2) {

            			String str1 =  buf.readLine();	
		                String[] str2 = str1.split("\\ ");
				
		                if(str2[0].equals("ANNONCE")) {
		                	ajouteAnn(utilisateur.getUser_name(),str1);
		                }else if(str2[0].equals("MESSAGE")) {
		                	ajouteMessage(utilisateur.getUser_name(),str1);
		                	
		                }else if(strs[0].equals("TOUSANNS")) {
					
		                	out.println(getToutAnnonces());
		                }else {
		                	
		                	System.out.println(str1);
		                	//out.println("I can't understand");
		                }
	            		
            		}
            	}else {
            		out.println("The format is : 'I'm username'");
            	}
            }
            out.close();
            client.close();
        }catch(Exception e){  
            e.printStackTrace();  
        }
       
    }
    
    
    public Utilisateur newUtilisateur(String nom) {
    	
    	Utilisateur utilisateur = new Utilisateur();
    	utilisateur.setUser_name(nom);
    	Contenant .getUtilisateurs().add(utilisateur);
    	
    	return utilisateur;
    }

    public String getToutAnnonces() {
    	String all ="";
	
    	for(int i=0;i<Contenant .getAnns().size();i++) {   		
    		all+=((Annonce) Contenant .getAnns().get(i)).getUser_name() +","+((Annonce) Contenant .getAnns().get(i)).getDate_article()+","+((Annonce) Contenant .getAnns().get(i)).getName_article()+","+((Annonce) Contenant .getAnns().get(i)).getPrix_article();
    		if(i!=Contenant .getAnns().size()-1)
    			all+=" | ";
    	}
	
    	return all;
    }    

    
    
    public void ajouteAnn(String username,String str) {
    	
    	Annonce ann = new Annonce();
    	String[] strs = str.split("\\ ");
    	ann.setUser_name(username);
    	ann.setName_article(strs[1]);
    	ann.setPrix_article(strs[2]);
    	ann.setDate_article(strs[1]);
    	Contenant .getAnns().add(ann);
    }
    
    public void ajouteMessage(String fromName , String str) {   	   	
    	
    	String[] strs = str.split("\\ ");

    	Message message = new Message();
    	message.setFromName(fromName);
    	message.setToName(strs[1]);
    	message.setText(strs[2]);
    	Contenant .getMes().add(message);
    }

}

/////////////////////////////////////////
Contenant

import java.util.ArrayList;
import java.util.List;

public class Contenant  {

    private static List<Utilisateur> utilisateurs = new ArrayList<Utilisateur>();
    private static List<Annonce> anns = new ArrayList<Annonce>();
    private static List<Message> mes = new ArrayList<Message>();
       
	public static List<Utilisateur> getUtilisateurs() {
		return utilisateurs;
	}
	public static void setutilisateurs(List<Utilisateur> utilisateurs) {
		Contenant .utilisateurs = utilisateurs;
	}
	public static List<Annonce> getAnns() {
		return anns;
	}
	public static void setAdvs(List<Annonce> anns) {
		Contenant .anns = anns;
	}
	public static List<Message> getMes() {
		return mes;
	}
	public static void setMes(List<Message> mes) {
		Contenant .mes = mes;
	}    
}

/////////////////////////////////
message

public class Message{
	
	private String id_message;
	private String fromName;
	private String toName;
	private String text;
	public String getId_message() {
		return id_message;
	}
	public void setId_message(String id_message) {
		this.id_message = id_message;
	}
	public String getFromName() {
		return fromName;
	}
	public void setFromName(String fromName) {
		this.fromName = fromName;
	}
	public String getToName() {
		return toName;
	}
	public void setToName(String toName) {
		this.toName = toName;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	
	
	
}

///////////////////////////////////
Utilisateur

public class Utilisateur {
	private String id_utilisateur;
	private String user_name;
	public String getId_utilisateur() {
		return id_utilisateur;
	}
	public void setId_utilisateur(String id_utilisateur) {
		this.id_utilisateur = id_utilisateur;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

}

////////////////////////////////
 Annonce

public class Annonce {
	private String id_name;
	private String user_name;
	private String name_article;
	private String prix_article;
	private String date_article;
	public String getId_name() {
		return id_name;
	}
	public void setId_name(String id_name) {
		this.id_name = id_name;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getName_article() {
		return name_article;
	}
	public void setName_article(String name_article) {
		this.name_article = name_article;
	}
	public String getPrix_article() {
		return prix_article;
	}
	public void setPrix_article(String prix_article) {
		this.prix_article = prix_article;
	}
	public String getDate_article() {
		return date_article;
	}
	public void setDate_article(String date_article) {
		this.date_article = date_article;
	}

}
:code:

This post has been edited by modi123_1: 29 November 2017 - 01:55 PM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Page 1 of 1