walslayer Posted January 9, 2012 Share Posted January 9, 2012 Bonjour, je viens vous voir pour avoir quelques informations concernant la connexion entre un serveur et un client android en wifi. Mon problème est que je n'arrive pas à me connecter au serveur. J'ai pourtant utilisé la bonne adresse IP et le bon port de connexion. Les permissions pour le wifi sont mises. Je met le code du client pour voir vos réactions. Merci d'avance de votre aide. private Button connexion, avancer; private String serverIpAddress = ""; private boolean connected = false, passe=false; byte[] buf=new byte[2]; byte[] buf2=new byte[7]; byte comg, comd; OutputStream nos; Socket socket; PrintWriter out; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.client); //serverIp = (EditText) findViewById(R.id.serve); connexion = (Button) findViewById(R.id.connexion); avancer = (Button) findViewById(R.id.avancer); avancer.setOnClickListener(this); connexion.setOnClickListener(this); } public class TCPClient extends Thread{ public void run() { System.out.println("dedans"); try{ Log.d("ClientActivity", "C: Connecting..."); //socket = new Socket(serverAddr, 1500); InetAddress serverAddr = InetAddress.getByName("192.168.1.129"); try { socket= new Socket(serverAddr,1500); System.out.println("socket = "+socket); } catch (IOException e) { e.printStackTrace(); } //Toast.makeText(client.this, "Etat : "+socket.isConnected(), Toast.LENGTH_SHORT).show(); if (socket.isConnected()) { //try { //Toast.makeText(client.this, "nikel", Toast.LENGTH_SHORT).show(); connected = true; Log.d("ClientActivity", "C: Sending command."); try { out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); } catch (IOException e) { e.printStackTrace(); } // where you issue the commands passe=true; //out.println("Hey Server!"); Log.d("ClientActivity", "C: Sent."); } //socket.close(); Log.d("ClientActivity", "C: Closed."); } catch (Exception e) { Log.e("ClientActivity", "C: Error", e); connected = false; } } } public void onClick(View v) { // TODO Auto-generated method stub if(v.getId()==connexion.getId()) { Toast.makeText(this, "Lancement de la connexion", Toast.LENGTH_SHORT).show(); if (!connected) { //serverIpAddress = serverIp.getText().toString(); Thread TCPClient = new Thread(); TCPClient.start(); } Toast.makeText(this, "passer :"+passe, Toast.LENGTH_SHORT).show(); } if(v.getId()==avancer.getId()) { Toast.makeText(this, "On avance", Toast.LENGTH_SHORT).show(); if(avancerRobot(124,124)) { Toast.makeText(this, "pas d'erreur", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "erreur", Toast.LENGTH_SHORT).show(); } } } public boolean avancerRobot(int cmg, int cmd) { try { nos = socket.getOutputStream(); buf[0]=(byte)(cmg); buf[1]=(byte)(cmd); nos.write(buf); return true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } PS : Le serveur est en réalité un robot wifi (marque wifibot). Quote Link to comment Share on other sites More sharing options...
Philosopheur Posted March 7, 2012 Share Posted March 7, 2012 Bonjour, Je suis à la recherche de ce type de code pour me connecter au client WIFIRemote de MediaPortal. as tu résolu ton problème ? si oui, peux tu nous en faire profiter STP? Merci de ton retour. Quote Link to comment Share on other sites More sharing options...
JCC78 Posted March 9, 2012 Share Posted March 9, 2012 Si je comprends bien tu veux faire un peu la même chose que cette appli? http://tuto.vdlys.com/?ID=VDLysPDA_Radr Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.