SOS les amis,je veux créer un petit exemple de chat avec les sockets sur android:l'idée c'est que le serveur(classe java) quand il reçoit un message il doit le diffuser à toute les clients(android) connectés mon exemple ne marche pas comme prévu prière de me donner un coup de pouce
voici le code serveur:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
 
package Demo;
 
//Chat Server runs at port no. 9999
import java.io.*;
import java.util.*;
import java.net.*;
import static java.lang.System.out;
 
public class  ChatServer {
private static final int PORT_NUMBER = 4444;
 
Vector<HandleClient> clients = new Vector<HandleClient>();
 
public void process() throws Exception  {
   ServerSocket server = new ServerSocket(PORT_NUMBER);
   out.println("Server Started...");
   while( true) {
		 Socket client = server.accept();
		 System.out.println("client connecte");
 
 
		 HandleClient c = new HandleClient(client);
		 clients.add(c);
  }  // end of while
}
public static void main(String ... args) throws Exception {
   new ChatServer().process();
} // end of main
 
public void boradcast(String message)  {
	    // send message to all connected users
	    for ( HandleClient c : clients ){
	       //if ( ! c.getUserName().equals(user) )
	          c.sendMessage(message);
	    }
}
 
class  HandleClient extends Thread {
 
	BufferedReader input;
	PrintStream output;
 
	public HandleClient(Socket  client) throws Exception {
      // get input and output streams
	 input = new BufferedReader( new InputStreamReader( client.getInputStream())) ;
	 output = new PrintStream(client.getOutputStream());
	 // read name
 
	 start();
     }
 
     public void sendMessage(String  msg)  {
	    output.println("message:" + msg);
	    System.out.println("message   :"+msg);
	}
 
 
     public void run()  {
 	     String line;
	     try    {
             while(true)   {
		 line = input.readLine();
		 if ( line.equals("end") ) {
		   clients.remove(this);
 
		   break;
              }
		 boradcast(line); // method  of outer class - send messages to all
	       } // end of while
	     } // try
	     catch(Exception ex) {
	       System.out.println(ex.getMessage());
	     }
     } // end of run()
} // end of inner class
 
} // end of Server
et voici le client

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 
package com.example.envoyer;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity {
	Socket socket;
	PrintStream pw;
    BufferedReader br;
    EditText History;
    EditText message;
    Button send;
   final String serverAdress="10.0.2.2";
 final private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			String message=(String)msg.obj;
			History.append(message);
 
		};
 
	};
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialogue);
		if (android.os.Build.VERSION.SDK_INT > 9) {
		    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		    StrictMode.setThreadPolicy(policy);
		}
		History=(EditText)findViewById(R.id.messageHistory);
		message=(EditText)findViewById(R.id.message);
		send=(Button)findViewById(R.id.sendMessageButton);
 
		/*                                */
		try {
			socket=new Socket(serverAdress, 4444);
			 br = new BufferedReader( new InputStreamReader( socket.getInputStream()) ) ;
 
 
		} catch (UnknownHostException e) {
 
			e.printStackTrace();
		} catch (IOException e) {
 
			e.printStackTrace();
		}
 
		Thread lireMessage=new Thread(new Runnable() {
 
			@Override
		public void run() {
	            String line;
	            try {
	                while(true) {
	                    line = br.readLine();
	                    Message msg=handler.obtainMessage();
	                    msg.obj=line;
	                    handler.sendMessage(msg);
	                } // end of while
	            } catch(Exception ex) {}
	        }
 
	    });
		lireMessage.start();
 
		/*                    */
 
		send.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				 try {
					pw = new PrintStream(socket.getOutputStream());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String msg=message.getText().toString();
				pw.print(msg);
				pw.flush();
				//Message mssg=handler.obtainMessage();
				//mssg.obj=msg;
				//handler.sendMessage(mssg);
				message.setText("");
			}
		});
 
	}
 
 
 
 
}