Jump to content

connexion à un serveur depuis client android en wifi


walslayer
 Share

Recommended Posts

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).

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

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

×   Your previous content has been restored.   Clear editor

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

 Share

×
×
  • Create New...